From 57a9269a60fa42e4aaf5c209375a22541ba72279 Mon Sep 17 00:00:00 2001 From: Kristijan Trajkovski Date: Tue, 17 Jun 2014 10:23:46 +0200 Subject: [PATCH 1/8] Added clients function --- lib/namespace.js | 12 ++++++++++++ package.json | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/namespace.js b/lib/namespace.js index 4ae0b154a5..69e07ab419 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -193,6 +193,18 @@ Namespace.prototype.remove = function(socket){ } }; +/** + * Gets all clients in room + * + * @param {String} room id + * @param {Function} callback, callback function + * @api public + */ + +Namespace.prototype.clients = function(name, fn){ + this.adapter(name, fn); +}; + /** * Emits to all clients. * diff --git a/package.json b/package.json index 518fc468d3..d42f1a12b5 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "engine.io": "1.2.2", "socket.io-parser": "2.2.0", "socket.io-client": "Automattic/socket.io-client#095acb", - "socket.io-adapter": "0.2.0", + "socket.io-adapter": "git+ssh://git@github.com:FREEZX/socket.io-adapter.git", "has-binary-data": "0.1.1", "debug": "0.7.4" }, From 4c6ec375a43266e536175fa902f17346d57e97f5 Mon Sep 17 00:00:00 2001 From: Kristijan Trajkovski Date: Tue, 17 Jun 2014 14:14:20 +0200 Subject: [PATCH 2/8] clients function fix --- lib/namespace.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/namespace.js b/lib/namespace.js index 69e07ab419..6a41af04a2 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -202,7 +202,7 @@ Namespace.prototype.remove = function(socket){ */ Namespace.prototype.clients = function(name, fn){ - this.adapter(name, fn); + this.adapter.clients(name, fn); }; /** From c21671e826e39ab86472fd1e977ae0ad1c73cd92 Mon Sep 17 00:00:00 2001 From: Kristijan Trajkovski Date: Thu, 19 Jun 2014 10:54:14 +0200 Subject: [PATCH 3/8] Attempt to pass Travis build --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 83e025be11..19afd2ea76 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "engine.io": "1.3.0", "socket.io-parser": "2.2.0", "socket.io-client": "Automattic/socket.io-client#3f4ae6", - "socket.io-adapter": "git+ssh://git@github.com:FREEZX/socket.io-adapter.git", + "socket.io-adapter": "FREEZX/socket.io-adapter", "has-binary-data": "0.1.1", "debug": "0.7.4" }, From 3628a2effe8221258cf8ec81e0b357181f47c226 Mon Sep 17 00:00:00 2001 From: Kristijan Trajkovski Date: Thu, 19 Jun 2014 12:30:02 +0200 Subject: [PATCH 4/8] Added tests for namespace clients function --- test/socket.io.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/socket.io.js b/test/socket.io.js index 6b33569be1..383defd83a 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -422,6 +422,39 @@ describe('socket.io', function(){ } }); }); + + it('should be able to retreive all clients in the adapter', function(done){ + var srv = http(); + var sio = io(srv); + srv.listen(function(){ + var clientSocket = client(srv, { reconnection: false }); + clientSocket.on('connect', function init() { + sio.sockets.clients(function(err, data){ + expect(data).to.only.contain(clientSocket.io.engine.id); + done(); + }); + }); + }); + }); + + it('should be able to retreive all clients in the adapter that have joined a certain room', function(done){ + var srv = http(); + var sio = io(srv); + var room = 'room'; + srv.listen(function(){ + sio.on('connection', function(socket){ + sio.sockets.clients(room, function(err, data){ + expect(data).to.be.empty(); + socket.join(room); + sio.sockets.clients(room, function(err, data){ + expect(data).to.only.contain(socket.id); + done(); + }); + }); + }); + var clientSocket = client(srv, { reconnection: false }); + }); + }); }); }); From b12d7cdf50c178cab116235e7ea8476093e11c21 Mon Sep 17 00:00:00 2001 From: Kristijan Trajkovski Date: Mon, 23 Jun 2014 09:42:58 +0200 Subject: [PATCH 5/8] Changed __proto__ to Object.create --- lib/namespace.js | 2 +- test/socket.io.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/namespace.js b/lib/namespace.js index 6a41af04a2..057c156a9d 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -60,7 +60,7 @@ function Namespace(server, name){ * Inherits from `EventEmitter`. */ -Namespace.prototype.__proto__ = Emitter.prototype; +Namespace.prototype = Object.create(Emitter.prototype); /** * Apply flags from `Socket`. diff --git a/test/socket.io.js b/test/socket.io.js index 383defd83a..8f080c13db 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -1188,10 +1188,13 @@ describe('socket.io', function(){ sio.on('connection', function(s){ s.join('a', function(){ expect(s.rooms).to.eql([s.id, 'a']); + expect(sio.nsps['/'].adapter.rooms.a).to.only.have.keys([s.id]); s.join('b', function(){ expect(s.rooms).to.eql([s.id, 'a', 'b']); + expect(sio.nsps['/'].adapter.rooms.b).to.only.have.keys([s.id]); s.leave('b', function(){ expect(s.rooms).to.eql([s.id, 'a']); + expect(sio.nsps['/'].adapter.rooms.b).to.be.empty(); done(); }); }); From 522e8ea6f7d6e376ccf9697ef9c4fbf18eb806a5 Mon Sep 17 00:00:00 2001 From: Kristijan Trajkovski Date: Mon, 23 Jun 2014 09:48:16 +0200 Subject: [PATCH 6/8] Upped client version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2f1b3e6bb9..cceda8367d 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "dependencies": { "engine.io": "1.3.1", "socket.io-parser": "2.2.1", - "socket.io-client": "Automattic/socket.io-client#3f4ae6", + "socket.io-client": "Automattic/socket.io-client#05c9632", "socket.io-adapter": "FREEZX/socket.io-adapter", "has-binary-data": "0.1.1", "debug": "0.7.4" From 7dfc12e5caebba4b81aa3429828b7d1ca088c700 Mon Sep 17 00:00:00 2001 From: Damien Brugne Date: Mon, 15 Sep 2014 11:34:15 +0200 Subject: [PATCH 7/8] Describe the new clients() method --- Readme.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Readme.md b/Readme.md index 674b263677..64b0b2971c 100644 --- a/Readme.md +++ b/Readme.md @@ -184,6 +184,18 @@ server.listen(3000); ``` For other available methods, see `Namespace` below. + +### Server#clients(name:String, fn:Function) + + Get the room socket IDs list from adapter and fire *fn* callback: + + ```js + var io = require('socket.io')(); + io.clients('room', function(err, clients){ + if (!err) + console.log(clients); // => [PZDoMHjiu8PYfRiKAAAF, d9rZTYbZfNhVf9j9AAAE] + }); + ``` ### Server#use From 1668d2321fa53e35eb940cc237078596f6a96d76 Mon Sep 17 00:00:00 2001 From: Damien Brugne Date: Mon, 15 Sep 2014 11:39:33 +0200 Subject: [PATCH 8/8] Update Readme.md --- Readme.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Readme.md b/Readme.md index 64b0b2971c..3c47440507 100644 --- a/Readme.md +++ b/Readme.md @@ -196,6 +196,17 @@ server.listen(3000); console.log(clients); // => [PZDoMHjiu8PYfRiKAAAF, d9rZTYbZfNhVf9j9AAAE] }); ``` + + As *clients* is a Namespace prototype method it could be called on server instance or socket instance: + + ```js + var io = require('socket.io')(); + io.on('connection', function(socket){ + socket.clients('room', function(err, clients){ + // ... + }); + }); + ``` ### Server#use