Skip to content

Commit

Permalink
Add client level command listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
twisterghost committed Sep 12, 2020
1 parent 87dacc0 commit b0c301b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
4 changes: 4 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ gameManager.on('clientAdded', (client) => {
});

const server = new GMServer(function (client) {
client.addCommandListener('register', () => {
console.log('Client is registering.');
});

gameManager.addClient(client);
});

Expand Down
37 changes: 35 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
const _ = require('lodash');
const TERMINATING_CHARACTER = '\0';
const DEBUG_MODE = process.env.GM_SERVER_DEBUG === 'true';
const TERM_STR = '^X|X^';
const TERM_STR = '\n\t\n';

class Client {
constructor (socket) {
this.socket = socket;
this.dataHandlers = [];
this.commandHandlers = {};
this.clientId = _.uniqueId();
this.created = Date.now();
this.data = {};
Expand All @@ -22,13 +23,20 @@ class Client {
console.info(this.clientId, ' received: ', JSON.stringify(dataAsObject));
}

// Low level handlers
this.dataHandlers.forEach(handler => {
handler(dataAsObject);
});

// Client level command handlers
if (this.commandHandlers[dataAsObject.command]) {
this.commandHandlers[dataAsObject.command].forEach(handler => {
handler(dataAsObject);
});
}
});

socket.on('error', error => {
console.error('oops');
console.error(error);
});
}
Expand Down Expand Up @@ -105,6 +113,31 @@ class Client {
this.dataHandlers.push(handler);
}

/**
* Registers a callback function for when a given command is sent in by this client
* @param {string} command The command to listen for
* @param {function} handler A callback to run when the command is received
*/
addCommandListener (command, handler) {
// If there is a command listener for this command already, push.
if (this.commandHandlers[command]) {
this.commandHandlers[command].push(handler);
} else {
this.commandHandlers[command] = [handler];
}
}

/**
* Removes a registered callback function for when a given command is sent in by this client
* @param {string} command The command to listen for
* @param {function} handler The function to remove
*/
removeCommandListener (command, handler) {
if (this.commandHandlers[command]) {
this.commandHandlers[command] = this.commandHandlers[command].filter(fn => fn !== handler);
}
}

/**
* Sets tick mode on or off.
* @param {boolean} onOff
Expand Down
11 changes: 11 additions & 0 deletions lib/clientManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ class ClientManager {
}
}

/**
* Removes a registered callback function for when a given command is sent in by a client
* @param {string} command The command to listen for
* @param {function} handler The function to remove
*/
removeCommandListener (command, handler) {
if (this.commandHandlers[command]) {
this.commandHandlers[command] = this.commandHandlers[command].filter(fn => fn !== handler);
}
}

/**
* Registers an event handler for ClientManager events
* @param {string} eventName The event to listen for
Expand Down
11 changes: 11 additions & 0 deletions test/clientManagerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ describe('Client Manager', function () {
});
});

describe('.removeCommandListener()', function () {
it('removes registered commands', function () {
const handlerStub = sinon.stub();
clientManager.addCommandListener('test', handlerStub);
clientManager.removeCommandListener('test', handlerStub);
clientManager.handleIncomingCommand(getFakeClient(), { command: 'test' });
clientManager.handleIncomingCommand(getFakeClient(), { command: 'doNotRun' });
assert(!handlerStub.called, 'The registered command handler was called');
});
});

describe('.broadcast()', function () {
it('can broadcast to all clients', function () {
const fakeSocket = getFakeClient();
Expand Down
2 changes: 1 addition & 1 deletion test/clientTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Client = require('../lib/client.js');
const _ = require('lodash');
let client;
let fakeSocket;
const TERM_STR = '^X|X^';
const TERM_STR = '\n\t\n';

function getFakeNetSocket () {
return {
Expand Down

0 comments on commit b0c301b

Please sign in to comment.