Skip to content

Commit

Permalink
Close #9 Implement keep-alive mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
schroffl committed Apr 10, 2018
1 parent f168077 commit 1c31e9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`.
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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));
});
Expand Down Expand Up @@ -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');
}
Expand Down

0 comments on commit 1c31e9d

Please sign in to comment.