diff --git a/Readme.md b/Readme.md index 674b263677..3c47440507 100644 --- a/Readme.md +++ b/Readme.md @@ -184,6 +184,29 @@ 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] + }); + ``` + + 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 diff --git a/lib/namespace.js b/lib/namespace.js index 4ae0b154a5..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`. @@ -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.clients(name, fn); +}; + /** * Emits to all clients. * diff --git a/package.json b/package.json index 9f74c4fda9..cceda8367d 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "engine.io": "1.3.1", "socket.io-parser": "2.2.1", "socket.io-client": "Automattic/socket.io-client#05c9632", - "socket.io-adapter": "0.2.0", + "socket.io-adapter": "FREEZX/socket.io-adapter", "has-binary-data": "0.1.1", "debug": "0.7.4" }, diff --git a/test/socket.io.js b/test/socket.io.js index 8622134ba9..9c99c22158 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 }); + }); + }); }); }); @@ -1170,10 +1203,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(); }); });