Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for getting clients in a given room #5

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -83,6 +83,44 @@ 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){
// One argument
if(!fn){
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, result));
};

/**
* Get all rooms the client is in.
*
* @param {String} client id
* @api public
*/
Adapter.prototype.roomClients = function(id, fn){
var result = Object.keys(this.sids[id] || []);
process.nextTick(fn.bind(null, null, result));
};

/**
* Broadcasts a packet.
*
Expand Down
188 changes: 188 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
var expect = require('expect.js');
var async = require('async');
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(fakensp);
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<rooms.length; ++i){
expect(adapter.rooms[rooms[i]]).to.have.key(sid);
}
adapter.delAll(sid);
for(i=0; i<rooms.length; ++i){
expect(adapter.rooms[rooms[i]]).to.not.have.key(sid);
}
done();
});
});
describe('clients', 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 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<fakensp.connected.length; ++i){
fakensp.connected[i.toString()].packet = incCountFn;
}
adapter.broadcast({name: 'asd'}, {});
expect(packetCallsCount).to.equal(20);
done();
});
it('should broadcast to all clients in a room', function(done){
var packetCallsCount = 0;
var incCountFn = function(){
++packetCallsCount;
};
for(var i=0; i<fakensp.connected.length; ++i){
fakensp.connected[i.toString()].packet = incCountFn;
}
adapter.broadcast({name: 'asd'}, {rooms: ['a']});
expect(packetCallsCount).to.equal(10);
done();
});
});
});