Skip to content

Commit

Permalink
Merge pull request #24 from cha0s/clients
Browse files Browse the repository at this point in the history
Suggestion for implementation of clients API
  • Loading branch information
rauchg committed Jan 31, 2015
2 parents 304a93d + cba037f commit cb6e0d8
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,44 @@ Adapter.prototype.broadcast = function(packet, opts){
}
});
};

/**
* Gets a list of clients by sid.
*
* @param {Array} explicit set of rooms to check.
* @api public
*/

Adapter.prototype.clients = function(rooms, fn){
var ids = {};
var self = this;
var sids = [];
var socket;

rooms = rooms || [];
if (rooms.length) {
for (var i = 0; i < rooms.length; i++) {
var room = self.rooms[rooms[i]];
if (!room) continue;
for (var id in room) {
if (room.hasOwnProperty(id)) {
if (ids[id]) continue;
socket = self.nsp.connected[id];
if (socket) {
sids.push(id);
ids[id] = true;
}
}
}
}
} else {
for (var id in self.sids) {
if (self.sids.hasOwnProperty(id)) {
socket = self.nsp.connected[id];
if (socket) sids.push(id);
}
}
}

if (fn) process.nextTick(fn.bind(null, null, sids));
};

0 comments on commit cb6e0d8

Please sign in to comment.