Skip to content

Commit

Permalink
Merge pull request #85 from vstirbu/master
Browse files Browse the repository at this point in the history
added clientEvents to server configuration for getting lifecycle changes...
  • Loading branch information
Greg committed Feb 24, 2015
2 parents ab06f36 + aff69b3 commit 96cd820
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/server/cloak/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ module.exports = (function() {
}
user.disconnectedSince = new Date().getTime();
delete socketIdToUserId[socket.id];
if (config.clientEvents && config.clientEvents.disconnect) {
config.clientEvents.disconnect(user);
}
console.log((cloak._host(socket) + ' disconnects').info);
});

Expand All @@ -122,6 +125,9 @@ module.exports = (function() {
if (config.autoJoinLobby) {
lobby.addMember(user);
}
if (config.clientEvents && config.clientEvents.begin) {
config.clientEvents.begin(user);
}
});

socket.on('cloak-resume', function(data) {
Expand All @@ -136,6 +142,9 @@ module.exports = (function() {
valid: true,
config: config
});
if (config.clientEvents && config.clientEvents.resume) {
config.clientEvents.resume(user);
}
console.log((cloak._host(socket) + ' resumes').info);
}
else {
Expand Down
65 changes: 65 additions & 0 deletions test/clientEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* jshint node:true */

// clientEvents tests

var _ = require('underscore');

var suite = Object.create(require('./lib/superSuite.js'));

module.exports = _.extend(suite, {

clientEvents: function(test) {

test.expect(8);

var server = this.server;
var client = suite.createClient();
var detected;
var eventsOrder = [];
var currentUser;

server.configure({
port: this.port,
clientEvents: {
begin: function (user) {
currentUser = user.id;
eventsOrder.push('begin');
test.ok(true, 'begin event happened');
},
disconnect: function (user) {
if (!detected) {
eventsOrder.push('disconnect');
test.ok(true, 'disconnect event happened');
test.equal(user.id, client.currentUser());
test.equal(user.id, currentUser);
client._connect();
detected = true;
}
},
resume: function (user) {
test.ok(true, 'resume event happened');
eventsOrder.push('resume');
test.equal(user.id, client.currentUser());
test.equal(user.id, currentUser);
test.deepEqual(eventsOrder, ['begin', 'disconnect', 'resume']);
setTimeout(function () {
test.done();
});
}
}
});

client.configure({
serverEvents: {
begin: function() {
client._disconnect();
}
}
});

server.run();
client.run(this.host);

}

});

0 comments on commit 96cd820

Please sign in to comment.