add disconnect and improved error handling

This commit is contained in:
Robin Appelman 2015-12-24 21:32:47 +01:00
commit b77b780331
3 changed files with 21 additions and 0 deletions

View file

@ -5,6 +5,9 @@
"license": "MIT",
"version": "0.1.1",
"main": "lib/rcon.js",
"scripts": {
"build": "babel src --out-dir lib"
},
"repository": {
"type": "git",
"url": "https://github.com/spiretf/webrcon"

View file

@ -11,10 +11,15 @@ export default class Connection {
constructor (host, password) {
this.host = host;
this.password = password;
this.errorListeners = [];
this.init();
}
onError (listener) {
this.errorListeners.push(listener);
}
buildRcon () {
if (this.rcon) {
return;
@ -45,6 +50,11 @@ export default class Connection {
}, 100);
});
this.rcon.on('error', (e) => {
if (e == 'not authenticated') {
for (let listener of this.errorListeners) {
listener(e);
}
}
this.errorCount++;
console.log('failed to connect ' + this.errorCount + ' times (' + e + ')');
Promise.delay(2500).then(() => {

View file

@ -9,6 +9,14 @@ export default class Rcon {
this.connection = new Connection(host, password);
}
disconnect () {
this.connection.disconnect();
}
onError (listener) {
this.connection.onError(listener);
}
sendString (command) {
return this.connection.sendString(command);
}