diff --git a/README.md b/README.md index d3df5fd..e0b7c7c 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,6 @@ The constructor takes 3 parameters. If you want to make use of the `connect` opt **INFO**: The raw socket can be accessed via the instance's `sock` property. - ## Sending Commands #### TeamspeakQuery.send(cmd, params?, ...flags?) Sends a command to the server and returns a Promise that resolves the response or rejects if something went wrong. @@ -46,6 +45,14 @@ You can also use it to set flags, e.g. `query.send('clientlist', '-uid')`. If you want your response to be an array, e.g. for commands like `clientlist`, take a look at [Issue #3](https://github.com/schroffl/teamspeak-query/issues/3#issuecomment-359252099). +## Keep-Alive +A keep-alive mechanism is implemented to prevent the server from closing the connection after inactivity. It basically just sends a `version` command every few minutes (This doesn't require authentication and has a very small overhead). +If you want to tune its parameters, you can access the `keepalive` property of your `TeamspeakQuery` instance: +```javascript +query.keepalive.enable(true); // true => enable, false => disable, (default: true) +query.keepalive.duration = 30000; // Send the command every 30 seconds, (default: 5 minutes) +``` + ## Throttling Commands are being throttled by default if the host is not set to the local machine (`127.0.0.1` or `localhost`) in order to prevent a ban for flooding (see [Whitelisting and Blacklisting](http://media.teamspeak.com/ts3_literature/TeamSpeak%203%20Server%20Query%20Manual.pdf?#page=6) on page 6 in the specs). The instance of [lib/throttle.js](lib/throttle.js) can be accessed via `TeamspeakQuery.throttle`. diff --git a/index.js b/index.js index bb4827a..1be3c19 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const type = require('type-of'); const carrier = require('carrier'); const Throttle = require('./lib/throttle'); +const KeepAlive = require('./lib/keepalive'); class TeamspeakQuery extends EventEmitter { @@ -41,9 +42,14 @@ class TeamspeakQuery extends EventEmitter { 'enable': connectOptions.host !== '127.0.0.1' && connectOptions.host !== 'localhost' }); + this.keepalive = new KeepAlive(() => this.send('version')); + this.keepalive.enable(false); + sock.connect(connectOptions); sock.on('connect', () => { + this.keepalive.enable(true); + this.carrier = carrier.carry(sock); this.carrier.on('line', this.handleLine.bind(this)); }); @@ -73,6 +79,8 @@ class TeamspeakQuery extends EventEmitter { checkQueue() { this.throttle.run(() => { if(!this._current && this.queue.length) { + this.keepalive.interrupt(); + this._current = this.queue.shift(); this.sock.write(this._current.cmd + '\n'); }