Skip to content

Commit

Permalink
Merge pull request #11 from fwdcp/master
Browse files Browse the repository at this point in the history
Add Disconnect Method
  • Loading branch information
randunel committed Feb 19, 2016
2 parents 38ee51f + 9482d5c commit 600e27c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ rcon.connect().then(() => {
() => rcon.command('cvarlist').then(cvarlist => console.log(`cvarlist is \n${cvarlist}`))
).then(
() => rcon.command('changelevel de_dust2').then(() => console.log('changed map'))
).then(
() => rcon.disconnect()
).catch(err => {
console.log('caught', err);
console.log(err.stack);
Expand All @@ -69,6 +71,12 @@ rcon.connect().then(() => {
rcon.command('cvarlist', 1000).then(console.log, console.error);
```

#### Disconnect once finished

``` javascript
rcon.disconnect();
```

## Errors

Some errors may contain partial command output. That indicates that the command was run, but reply packets have been lost.
Expand Down
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ module.exports = params => {

return Object.freeze({
connect: connect,
command: command
command: command,
disconnect: disconnect
});

function connect() {
let connection = Connection(address);
return connection.create().then(() => _auth(connection));
}

function disconnect() {
return _connection.destroy().then(() => {
_connection = undefined;
});
}

function _auth(connection) {
let buf = packet.request({
id: 1,
Expand Down Expand Up @@ -64,6 +71,10 @@ module.exports = params => {
function command(text, timeout) {
return Promise.race([
new Promise((resolve, reject) => {
if (!_connection) {
reject(new Error('not connected'));
}

let unexpectedPackets;

let responseData = new Buffer(0);
Expand Down Expand Up @@ -144,4 +155,3 @@ module.exports = params => {
]);
}
};

27 changes: 25 additions & 2 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ module.exports = address => {
return Object.freeze({
create: create,
send: send,
getData: getData
getData: getData,
destroy: destroy
});

function create() {
return _createConnection().then(newConnection => {
connection = newConnection;

connection.on('close', _disconnectHandler);
});
}

function destroy() {
return _destroyConnection();
}

function _createConnection() {
return new Promise((resolve, reject) => {
let host = address.split(':')[0];
Expand All @@ -38,6 +45,23 @@ module.exports = address => {
});
}

function _destroyConnection() {
return new Promise((resolve, reject) => {
if (connection) {
connection.end();

connection.on('close', resolve);
}
else {
resolve();
}
});
}

function _disconnectHandler() {
connection = undefined;
}

function getData(cbSync) {
return new Promise((resolve, reject) => {
connection.on('error', errorHandler);
Expand Down Expand Up @@ -66,4 +90,3 @@ module.exports = address => {
connection.write(buffer);
}
};

0 comments on commit 600e27c

Please sign in to comment.