Skip to content

Commit f2dd9fe

Browse files
[fix] receive a message only once per-emit (not per-joined rooms) (#151)
1 parent 298dff2 commit f2dd9fe

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

index.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,8 @@ function adapter(uri, opts){
281281
if (!(remote || (opts && opts.flags && opts.flags.local))) {
282282
var self = this;
283283
var msg = msgpack.encode([uid, packet, opts]);
284-
if (self.withChannelMultiplexing && opts.rooms) {
285-
opts.rooms.forEach(function(room) {
286-
var chnRoom = self.channel + room + '#';
287-
pub.publish(chnRoom, msg);
288-
});
284+
if (self.withChannelMultiplexing && opts.rooms && opts.rooms.length === 1) {
285+
pub.publish(self.channel + opts.rooms[0] + '#', msg);
289286
} else {
290287
pub.publish(self.channel, msg);
291288
}

test/index.js

+45
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,51 @@ var adapter = require('../');
143143
});
144144
});
145145

146+
it('broadcasts to multiple rooms at a time', function(done){
147+
create(function(server1, client1){
148+
create(function(server2, client2){
149+
create(function(server3, client3){
150+
server1.on('connection', function(c1){
151+
c1.join('foo');
152+
c1.join('bar');
153+
});
154+
155+
server2.on('connection', function(c2){
156+
// does not join, performs broadcast
157+
c2.on('do broadcast', function(){
158+
c2.broadcast.to('foo').to('bar').emit('broadcast');
159+
});
160+
});
161+
162+
server3.on('connection', function(c3){
163+
// does not join, signals broadcast
164+
client2.emit('do broadcast');
165+
});
166+
167+
var called = false;
168+
client1.on('broadcast', function(){
169+
if (called) return done(new Error('Called more than once'))
170+
called = true;
171+
setTimeout(function () {
172+
client1.disconnect();
173+
client2.disconnect();
174+
client3.disconnect();
175+
done();
176+
}, 100);
177+
});
178+
179+
client2.on('broadcast', function(){
180+
throw new Error('Not in room');
181+
});
182+
183+
client3.on('broadcast', function(){
184+
throw new Error('Not in room');
185+
});
186+
});
187+
});
188+
});
189+
});
190+
146191
it('doesn\'t broadcast when using the local flag', function(done){
147192
create(function(server1, client1){
148193
create(function(server2, client2){

0 commit comments

Comments
 (0)