Skip to content

Commit

Permalink
[fix] receive a message only once per-emit (not per-joined rooms) (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne authored Nov 28, 2016
1 parent 298dff2 commit f2dd9fe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
7 changes: 2 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,8 @@ function adapter(uri, opts){
if (!(remote || (opts && opts.flags && opts.flags.local))) {
var self = this;
var msg = msgpack.encode([uid, packet, opts]);
if (self.withChannelMultiplexing && opts.rooms) {
opts.rooms.forEach(function(room) {
var chnRoom = self.channel + room + '#';
pub.publish(chnRoom, msg);
});
if (self.withChannelMultiplexing && opts.rooms && opts.rooms.length === 1) {
pub.publish(self.channel + opts.rooms[0] + '#', msg);
} else {
pub.publish(self.channel, msg);
}
Expand Down
45 changes: 45 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,51 @@ var adapter = require('../');
});
});

it('broadcasts to multiple rooms at a time', function(done){
create(function(server1, client1){
create(function(server2, client2){
create(function(server3, client3){
server1.on('connection', function(c1){
c1.join('foo');
c1.join('bar');
});

server2.on('connection', function(c2){
// does not join, performs broadcast
c2.on('do broadcast', function(){
c2.broadcast.to('foo').to('bar').emit('broadcast');
});
});

server3.on('connection', function(c3){
// does not join, signals broadcast
client2.emit('do broadcast');
});

var called = false;
client1.on('broadcast', function(){
if (called) return done(new Error('Called more than once'))
called = true;
setTimeout(function () {
client1.disconnect();
client2.disconnect();
client3.disconnect();
done();
}, 100);
});

client2.on('broadcast', function(){
throw new Error('Not in room');
});

client3.on('broadcast', function(){
throw new Error('Not in room');
});
});
});
});
});

it('doesn\'t broadcast when using the local flag', function(done){
create(function(server1, client1){
create(function(server2, client2){
Expand Down

0 comments on commit f2dd9fe

Please sign in to comment.