forked from garrettjoecox/scriptserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRcon.js
57 lines (45 loc) · 1.05 KB
/
Rcon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const SimpleRcon = require('simple-rcon');
const states = {
CONNECTED: 'connected',
DISCONNECTED: 'disconnected',
};
class Rcon {
constructor(config = {}) {
this.config = config;
this.state = states.DISCONNECTED;
this.queue = [];
this.rcon = new SimpleRcon({
host: this.config.host,
port: this.config.port,
password: this.config.password,
timeout: 0,
});
this.rcon.on('authenticated', () => {
this.state = states.CONNECTED;
});
this.rcon.on('disconnected', () => {
this.state = states.DISCONNECTED;
});
this.tick();
}
connect() {
this.rcon.connect();
}
disconnect() {
this.rcon.close();
}
tick() {
if (this.state === states.CONNECTED && this.queue.length > 0) {
const item = this.queue.shift();
this.rcon.exec(item.command, ({ body }) => item.callback(body));
}
setTimeout(() => this.tick(), this.config.buffer);
}
exec(command, callback) {
this.queue.push({
command,
callback,
});
}
}
module.exports = Rcon;