From 8a6fb8859f91f95fabd7d58c3231d3e617bad945 Mon Sep 17 00:00:00 2001 From: Vlad Stirbu Date: Mon, 23 Feb 2015 11:48:59 +0100 Subject: [PATCH 1/2] added clientEvents to server configuration for getting lifecycle changes about client connection state --- src/server/cloak/index.js | 6 +++++ test/clientEvents.js | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 test/clientEvents.js diff --git a/src/server/cloak/index.js b/src/server/cloak/index.js index 569257b..82e1afe 100644 --- a/src/server/cloak/index.js +++ b/src/server/cloak/index.js @@ -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); }); @@ -136,6 +139,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 { diff --git a/test/clientEvents.js b/test/clientEvents.js new file mode 100644 index 0000000..de4b04e --- /dev/null +++ b/test/clientEvents.js @@ -0,0 +1,53 @@ +/* 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(4); + + var server = this.server; + var client = suite.createClient(); + var detected; + + server.configure({ + port: this.port, + clientEvents: { + disconnect: function (user) { + if (!detected) { + test.ok(true, 'disconnect event happened'); + test.equal(user.id, client.currentUser()); + client._connect(); + detected = true; + } + }, + resume: function (user) { + test.ok(true, 'resume event happened'); + test.equal(user.id, client.currentUser()); + setTimeout(function () { + test.done(); + }); + } + } + }); + + client.configure({ + serverEvents: { + begin: function() { + client._disconnect(); + } + } + }); + + server.run(); + client.run(this.host); + + } + +}); From aff69b32e58fa73c8be8cd4787b253d193d6c987 Mon Sep 17 00:00:00 2001 From: Vlad Stirbu Date: Mon, 23 Feb 2015 21:33:51 +0100 Subject: [PATCH 2/2] added begin event --- src/server/cloak/index.js | 3 +++ test/clientEvents.js | 20 ++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/server/cloak/index.js b/src/server/cloak/index.js index 82e1afe..38282c7 100644 --- a/src/server/cloak/index.js +++ b/src/server/cloak/index.js @@ -125,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) { diff --git a/test/clientEvents.js b/test/clientEvents.js index de4b04e..fb0ea23 100644 --- a/test/clientEvents.js +++ b/test/clientEvents.js @@ -10,29 +10,41 @@ module.exports = _.extend(suite, { clientEvents: function(test) { - test.expect(4); + 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()); - setTimeout(function () { - test.done(); - }); + test.equal(user.id, currentUser); + test.deepEqual(eventsOrder, ['begin', 'disconnect', 'resume']); + setTimeout(function () { + test.done(); + }); } } });