diff --git a/index.js b/index.js index 9938fda..844f4fd 100644 --- a/index.js +++ b/index.js @@ -75,6 +75,7 @@ function Emitter(redis, prefix, nsp){ this._rooms = []; this._flags = {}; + this._except = []; } /** @@ -104,6 +105,14 @@ Emitter.prototype.to = function(room){ return this; }; +Emitter.prototype.except = function(room) { + if (!~this._except.indexOf(room)) { + debug('except %s', room); + this._except.push(room); + } + return this; +}; + /** * Return a new emitter for the given namespace. * @@ -127,7 +136,8 @@ Emitter.prototype.emit = function(){ var opts = { rooms: this._rooms, - flags: this._flags + flags: this._flags, + except: this._except }; var msg = msgpack.encode([uid, packet, opts]); @@ -141,6 +151,7 @@ Emitter.prototype.emit = function(){ // reset state this._rooms = []; this._flags = {}; + this._except = []; return this; }; diff --git a/test/index.js b/test/index.js index 4914497..02d16b4 100644 --- a/test/index.js +++ b/test/index.js @@ -214,5 +214,46 @@ describe('emitter', function() { }); }); }); + + it('should be able to exclude a socket by id', function(done) { + var pub = redis.createClient(); + var sub = redis.createClient(null, null, {return_buffers: true}); + srv = http(); + var sio = io(srv, {adapter: redisAdapter({pubClient: pub, subClient: sub})}); + + var firstId = false; + srv.listen(function() { + sio.on('connection', function(socket) { + if (firstId === false) { + firstId = socket.id; + } + }); + }); + + var a = client(srv, { forceNew: true }); + var b; + a.on('connect', function() { + b = client(srv, { forceNew: true }); + b.on('connect', function() { + + var calls = 0; + a.on('except event', function() { + calls++; + expect().fail(); + }); + b.on('except event', function() { + calls++; + setTimeout(function() { + expect(calls).to.be(1); + done(); + }, 1); + }); + + var emitter = ioe({ host: 'localhost', port: '6379' }); + emitter.except(firstId).emit('except event'); + + }); + }); + }); }); });