mirror of
https://codeberg.org/spire/webrcon.git
synced 2026-06-03 09:14:06 +02:00
add disconnect and improved error handling
This commit is contained in:
parent
937a328b6b
commit
b77b780331
3 changed files with 21 additions and 0 deletions
|
|
@ -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"
|
||||||
|
|
|
||||||
|
|
@ -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(() => {
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue