Skip to content

Commit db0c699

Browse files
[fix] Reset rooms object before broadcasting (#2970)
It seems packets could be delivered to wrong room in some case, if the _rooms array was not reset before the next emit.
1 parent 94df7bc commit db0c699

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

lib/socket.js

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -139,38 +139,42 @@ Socket.prototype.buildHandshake = function(query){
139139
Socket.prototype.emit = function(ev){
140140
if (~exports.events.indexOf(ev)) {
141141
emit.apply(this, arguments);
142-
} else {
143-
var args = Array.prototype.slice.call(arguments);
144-
var packet = {
145-
type: parser.EVENT,
146-
data: args
147-
};
148-
149-
// access last argument to see if it's an ACK callback
150-
if (typeof args[args.length - 1] === 'function') {
151-
if (this._rooms.length || this.flags.broadcast) {
152-
throw new Error('Callbacks are not supported when broadcasting');
153-
}
142+
return this;
143+
}
154144

155-
debug('emitting packet with ack id %d', this.nsp.ids);
156-
this.acks[this.nsp.ids] = args.pop();
157-
packet.id = this.nsp.ids++;
158-
}
145+
var args = Array.prototype.slice.call(arguments);
146+
var packet = {
147+
type: parser.EVENT,
148+
data: args
149+
};
159150

151+
// access last argument to see if it's an ACK callback
152+
if (typeof args[args.length - 1] === 'function') {
160153
if (this._rooms.length || this.flags.broadcast) {
161-
this.adapter.broadcast(packet, {
162-
except: [this.id],
163-
rooms: this._rooms,
164-
flags: this.flags
165-
});
166-
} else {
167-
// dispatch packet
168-
this.packet(packet, this.flags);
154+
throw new Error('Callbacks are not supported when broadcasting');
169155
}
170156

171-
// reset flags
172-
this._rooms = [];
173-
this.flags = {};
157+
debug('emitting packet with ack id %d', this.nsp.ids);
158+
this.acks[this.nsp.ids] = args.pop();
159+
packet.id = this.nsp.ids++;
160+
}
161+
162+
var rooms = this._rooms.slice(0);
163+
var flags = assign({}, this.flags);
164+
165+
// reset flags
166+
this._rooms = [];
167+
this.flags = {};
168+
169+
if (rooms.length || flags.broadcast) {
170+
this.adapter.broadcast(packet, {
171+
except: [this.id],
172+
rooms: rooms,
173+
flags: flags
174+
});
175+
} else {
176+
// dispatch packet
177+
this.packet(packet, flags);
174178
}
175179
return this;
176180
};

0 commit comments

Comments
 (0)