Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97c6568

Browse files
committed
Merge pull request socketio#1966 from cha0s/clients
Suggestion for implementation of clients API
2 parents 58eaeca + 8814825 commit 97c6568

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

Readme.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,40 @@ server.listen(3000);
240240
Hash of `Socket` objects that are connected to this namespace indexed
241241
by `id`.
242242

243+
### Namespace#clients(fn:Function)
244+
245+
Gets a list of client IDs connected to this namespace (across all nodes if applicable).
246+
247+
An example to get all clients in a namespace:
248+
249+
```js
250+
var io = require('socket.io')();
251+
io.of('/chat').clients(function(error, clients){
252+
if (error) throw error;
253+
console.log(clients); // => [PZDoMHjiu8PYfRiKAAAF, Anw2LatarvGVVXEIAAAD]
254+
});
255+
```
256+
257+
An example to get all clients in namespace's room:
258+
259+
```js
260+
var io = require('socket.io')();
261+
io.of('/chat').in('general').clients(function(error, clients){
262+
if (error) throw error;
263+
console.log(clients); // => [Anw2LatarvGVVXEIAAAD]
264+
});
265+
```
266+
267+
As with broadcasting, the default is all clients from the default namespace ('/'):
268+
269+
```js
270+
var io = require('socket.io')();
271+
io.clients(function(error, clients){
272+
if (error) throw error;
273+
console.log(clients); // => [6em3d4TJP8Et9EMNAAAA, G5p55dHhGgUnLUctAAAB]
274+
});
275+
```
276+
243277
### Namespace#use(fn:Function):Namespace
244278

245279
Registers a middleware, which is a function that gets executed for

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ Server.prototype.close = function(){
361361
* Expose main namespace (/).
362362
*/
363363

364-
['on', 'to', 'in', 'use', 'emit', 'send', 'write'].forEach(function(fn){
364+
['on', 'to', 'in', 'use', 'emit', 'send', 'write', 'clients'].forEach(function(fn){
365365
Server.prototype[fn] = function(){
366366
var nsp = this.sockets[fn];
367367
return nsp.apply(this.sockets, arguments);

lib/namespace.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,15 @@ Namespace.prototype.write = function(){
240240
this.emit.apply(this, args);
241241
return this;
242242
};
243+
244+
/**
245+
* Gets a list of clients.
246+
*
247+
* @return {Namespace} self
248+
* @api public
249+
*/
250+
251+
Namespace.prototype.clients = function(fn){
252+
this.adapter.clients(this.rooms, fn);
253+
return this;
254+
};

test/socket.io.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,122 @@ describe('socket.io', function(){
614614
});
615615
});
616616
});
617+
618+
it('should find all clients in a namespace', function(done){
619+
var srv = http();
620+
var sio = io(srv);
621+
var chatSids = [];
622+
var otherSid = null;
623+
srv.listen(function(){
624+
var c1 = client(srv, '/chat');
625+
var c2 = client(srv, '/chat', {forceNew: true});
626+
var c3 = client(srv, '/other', {forceNew: true});
627+
var total = 3;
628+
sio.of('/chat').on('connection', function(socket){
629+
chatSids.push(socket.id);
630+
--total || getClients();
631+
});
632+
sio.of('/other').on('connection', function(socket){
633+
otherSid = socket.id;
634+
--total || getClients();
635+
});
636+
});
637+
function getClients() {
638+
sio.of('/chat').clients(function(error, sids) {
639+
expect(error).to.be.undefined;
640+
expect(sids).to.contain(chatSids[0]);
641+
expect(sids).to.contain(chatSids[1]);
642+
expect(sids).to.not.contain(otherSid);
643+
done();
644+
});
645+
}
646+
});
647+
648+
it('should find all clients in a namespace room', function(done){
649+
var srv = http();
650+
var sio = io(srv);
651+
var chatFooSid = null;
652+
var chatBarSid = null;
653+
var otherSid = null;
654+
srv.listen(function(){
655+
var c1 = client(srv, '/chat');
656+
var c2 = client(srv, '/chat', {forceNew: true});
657+
var c3 = client(srv, '/other', {forceNew: true});
658+
var chatIndex = 0;
659+
var total = 3;
660+
sio.of('/chat').on('connection', function(socket){
661+
if (chatIndex++) {
662+
socket.join('foo', function() {
663+
chatFooSid = socket.id;
664+
--total || getClients();
665+
});
666+
} else {
667+
socket.join('bar', function() {
668+
chatBarSid = socket.id;
669+
--total || getClients();
670+
});
671+
}
672+
});
673+
sio.of('/other').on('connection', function(socket){
674+
socket.join('foo', function() {
675+
otherSid = socket.id;
676+
--total || getClients();
677+
});
678+
});
679+
});
680+
function getClients() {
681+
sio.of('/chat').in('foo').clients(function(error, sids) {
682+
expect(error).to.be.undefined;
683+
expect(sids).to.contain(chatFooSid);
684+
expect(sids).to.not.contain(chatBarSid);
685+
expect(sids).to.not.contain(otherSid);
686+
done();
687+
});
688+
}
689+
});
690+
691+
it('should find all clients across namespace rooms', function(done){
692+
var srv = http();
693+
var sio = io(srv);
694+
var chatFooSid = null;
695+
var chatBarSid = null;
696+
var otherSid = null;
697+
srv.listen(function(){
698+
var c1 = client(srv, '/chat');
699+
var c2 = client(srv, '/chat', {forceNew: true});
700+
var c3 = client(srv, '/other', {forceNew: true});
701+
var chatIndex = 0;
702+
var total = 3;
703+
sio.of('/chat').on('connection', function(socket){
704+
if (chatIndex++) {
705+
socket.join('foo', function() {
706+
chatFooSid = socket.id;
707+
--total || getClients();
708+
});
709+
} else {
710+
socket.join('bar', function() {
711+
chatBarSid = socket.id;
712+
--total || getClients();
713+
});
714+
}
715+
});
716+
sio.of('/other').on('connection', function(socket){
717+
socket.join('foo', function() {
718+
otherSid = socket.id;
719+
--total || getClients();
720+
});
721+
});
722+
});
723+
function getClients() {
724+
sio.of('/chat').clients(function(error, sids) {
725+
expect(error).to.be.undefined;
726+
expect(sids).to.contain(chatFooSid);
727+
expect(sids).to.contain(chatBarSid);
728+
expect(sids).to.not.contain(otherSid);
729+
done();
730+
});
731+
}
732+
});
617733
});
618734

619735
describe('socket', function(){

0 commit comments

Comments
 (0)