From 32233374f552ee5d9b061049b6701dc9b5c3a1f2 Mon Sep 17 00:00:00 2001 From: Kristijan Trajkovski Date: Tue, 17 Jun 2014 10:18:50 +0200 Subject: [PATCH 1/6] Added clients function --- index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/index.js b/index.js index 61138df..95f263f 100644 --- a/index.js +++ b/index.js @@ -83,6 +83,21 @@ Adapter.prototype.delAll = function(id, fn){ delete this.sids[id]; }; + +/** + * Get all clients in room. + * + * @param {String} room id + * @api public + */ +Adapter.prototype.clients = function(room, fn){ + if(!fn){ + return; + } + process.nextTick(fn.bind(null, null, Object.keys(this.rooms[room] || []))); +}; + + /** * Broadcasts a packet. * From 1da7c8cb456c64187c3fd84e6cbe9c9cdc561fd1 Mon Sep 17 00:00:00 2001 From: Kristijan Trajkovski Date: Tue, 17 Jun 2014 16:04:59 +0200 Subject: [PATCH 2/6] Added tests --- test/index.js | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 test/index.js diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..d075161 --- /dev/null +++ b/test/index.js @@ -0,0 +1,91 @@ +var expect = require('expect.js'); +var async = require('async'); +var Adapter = require('../index.js'); + +describe('socket.io-adapter', function(){ + var adapter; + describe('Adapter', function(){ + it('should construct the adapter', function(){ + adapter = new Adapter('test'); + expect(adapter).to.be.an(Adapter); + }); + }); + describe('add', function(){ + it('should add a socket to a room', function(){ + var room = 'a'; + var sid = '1'; + adapter.add(sid, room, function(){ + expect(adapter.sids).to.be.an('object'); + expect(adapter.sids[sid]).to.be.an('object'); + expect(adapter.sids[sid][room]).to.equal(true); + expect(adapter.rooms[room]).to.be.an('object'); + expect(adapter.rooms[room][sid]).to.equal(true); + }); + }); + }); + describe('del', function(){ + it('should remove a socket from aroom', function(){ + var room = 'a'; + var sid = '1'; + adapter.del(sid, room); + expect(adapter.sids[sid]).to.not.contain(room); + expect(adapter.rooms[room]).to.not.contain(room); + }); + }); + describe('delAll', function(){ + var sid = 'a'; + var rooms = []; + + before(function(done){ + //Clear rooms and sockets + adapter.sids = {}; + adapter.rooms = {}; + + //Add the socket to multiple rooms + async.times(10, function(n, next){ + rooms.push(n); + adapter.add(sid, n, function(){ + next(); + }); + }, done); + }); + + it('should remove a socket from all rooms', function(done){ + var rooms = Object.keys(adapter.rooms); + var i; + for(i=0; i Date: Thu, 19 Jun 2014 10:42:54 +0200 Subject: [PATCH 3/6] Added clients call without room parameter, added mocha tests for broadcast function --- index.js | 17 +++++++- test/index.js | 117 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 122 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 95f263f..74477b6 100644 --- a/index.js +++ b/index.js @@ -91,10 +91,23 @@ Adapter.prototype.delAll = function(id, fn){ * @api public */ Adapter.prototype.clients = function(room, fn){ + // One argument if(!fn){ - return; + if(typeof(room) !== 'function'){ + return; + } + fn = room; + room = null; + } + + var result; + if(room === null){ + result = Object.keys(this.sids || []); + } + else{ + result = Object.keys(this.rooms[room] || []); } - process.nextTick(fn.bind(null, null, Object.keys(this.rooms[room] || []))); + process.nextTick(fn.bind(null, null, result)); }; diff --git a/test/index.js b/test/index.js index d075161..904a87b 100644 --- a/test/index.js +++ b/test/index.js @@ -4,9 +4,21 @@ var Adapter = require('../index.js'); describe('socket.io-adapter', function(){ var adapter; + var fakensp; + before(function(done){ + fakensp = { + name: 'test', + connected: [] + }; + async.times(20, function(n, next){ + fakensp.connected[n.toString()] = {}; + next(); + }, + done); + }); describe('Adapter', function(){ it('should construct the adapter', function(){ - adapter = new Adapter('test'); + adapter = new Adapter(fakensp); expect(adapter).to.be.an(Adapter); }); }); @@ -65,6 +77,8 @@ describe('socket.io-adapter', function(){ }); describe('clients', function(){ var room = 'a'; + var otherroom = 'b'; + var roomsids = []; var sids = []; before(function(done){ @@ -72,20 +86,103 @@ describe('socket.io-adapter', function(){ adapter.sids = {}; adapter.rooms = {}; - //Add the socket to multiple rooms - async.times(10, function(n, next){ - n = n.toString(); - sids.push(n); - adapter.add(n, room, function(){ - next(); - }); - }, done); + async.parallel([ + function(callback){ + async.times(10, function(n, next){ + n = n.toString(); + sids.push(n); + roomsids.push(n); + adapter.add(n, room, function(){ + next(); + }); + }, callback); + }, + function(callback){ + async.times(10, function(n, next){ + n+=10; + n = n.toString(); + sids.push(n); + adapter.add(n, otherroom, function(){ + next(); + }); + }, callback); + }, + ], function(){ + done(); + }); }); - it('should get all clients in a room', function(done){ + it('should get all clients in a given room', function(done){ adapter.clients(room, function(err, clients){ + expect(clients).to.only.have.keys(roomsids); + done(); + }); + }); + it('should get all connected clients', function(done){ + adapter.clients(function(err, clients){ expect(clients).to.only.have.keys(sids); done(); }); }); }); + describe('broadcast', function(){ + var room = 'a'; + var otherroom = 'b'; + var roomsids = []; + var sids = []; + + before(function(done){ + //Clear rooms and sockets + adapter.sids = {}; + adapter.rooms = {}; + + async.parallel([ + function(callback){ + async.times(10, function(n, next){ + n = n.toString(); + sids.push(n); + roomsids.push(n); + adapter.add(n, room, function(){ + next(); + }); + }, callback); + }, + function(callback){ + async.times(10, function(n, next){ + n+=10; + n = n.toString(); + sids.push(n); + adapter.add(n, otherroom, function(){ + next(); + }); + }, callback); + }, + ], function(){ + done(); + }); + }); + it('should broadcast to all clients', function(done){ + var packetCallsCount = 0; + var incCountFn = function(){ + ++packetCallsCount; + }; + for(var i=0; i Date: Thu, 19 Jun 2014 12:58:56 +0200 Subject: [PATCH 4/6] Changed to standards-recommended prototype inheritance --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 74477b6..ee7b154 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,7 @@ function Adapter(nsp){ * Inherits from `EventEmitter`. */ -Adapter.prototype.__proto__ = Emitter.prototype; +Adapter.prototype = Object.create(Emitter.prototype); /** * Adds a socket from a room. From ff6fcb609844451200d53e8d18d5f4bbe4d4884e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 21 Nov 2014 12:41:02 -0500 Subject: [PATCH 5/6] added roomClients method returns rooms the socket is in returns all rooms if no socket id is passed in --- index.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/index.js b/index.js index ee7b154..4734686 100644 --- a/index.js +++ b/index.js @@ -110,6 +110,30 @@ Adapter.prototype.clients = function(room, fn){ process.nextTick(fn.bind(null, null, result)); }; +/** + * Get all rooms the client is in. + * + * @param {String} client id + * @api public + */ +Adapter.prototype.roomClients = function(id, fn){ + // One argument + if(!fn){ + if(typeof(id) !== 'function'){ + return; + } + fn = id; + id = null; + } + var result; + if(id === null){ + result = Object.keys(this.rooms || []); + } + else{ + result = Object.keys(this.sids[id] || []); + } + process.nextTick(fn.bind(null, null, result)); +}; /** * Broadcasts a packet. From f5dbe40db4a56aadd1759eef490a4055d0b11f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 21 Nov 2014 18:10:05 -0500 Subject: [PATCH 6/6] made id param required --- index.js | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/index.js b/index.js index 4734686..890331f 100644 --- a/index.js +++ b/index.js @@ -117,21 +117,7 @@ Adapter.prototype.clients = function(room, fn){ * @api public */ Adapter.prototype.roomClients = function(id, fn){ - // One argument - if(!fn){ - if(typeof(id) !== 'function'){ - return; - } - fn = id; - id = null; - } - var result; - if(id === null){ - result = Object.keys(this.rooms || []); - } - else{ - result = Object.keys(this.sids[id] || []); - } + var result = Object.keys(this.sids[id] || []); process.nextTick(fn.bind(null, null, result)); };