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", "license": "MIT",
"version": "0.1.1", "version": "0.1.1",
"main": "lib/rcon.js", "main": "lib/rcon.js",
"scripts": {
"build": "babel src --out-dir lib"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/spiretf/webrcon" "url": "https://github.com/spiretf/webrcon"

View file

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

View file

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