Skip to content

Commit

Permalink
[fix] Reset rooms object before broadcasting (#2970)
Browse files Browse the repository at this point in the history
It seems packets could be delivered to wrong room in some case, if the
_rooms array was not reset before the next emit.
  • Loading branch information
darrachequesne authored Jun 12, 2017
1 parent 94df7bc commit db0c699
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,38 +139,42 @@ Socket.prototype.buildHandshake = function(query){
Socket.prototype.emit = function(ev){
if (~exports.events.indexOf(ev)) {
emit.apply(this, arguments);
} else {
var args = Array.prototype.slice.call(arguments);
var packet = {
type: parser.EVENT,
data: args
};

// access last argument to see if it's an ACK callback
if (typeof args[args.length - 1] === 'function') {
if (this._rooms.length || this.flags.broadcast) {
throw new Error('Callbacks are not supported when broadcasting');
}
return this;
}

debug('emitting packet with ack id %d', this.nsp.ids);
this.acks[this.nsp.ids] = args.pop();
packet.id = this.nsp.ids++;
}
var args = Array.prototype.slice.call(arguments);
var packet = {
type: parser.EVENT,
data: args
};

// access last argument to see if it's an ACK callback
if (typeof args[args.length - 1] === 'function') {
if (this._rooms.length || this.flags.broadcast) {
this.adapter.broadcast(packet, {
except: [this.id],
rooms: this._rooms,
flags: this.flags
});
} else {
// dispatch packet
this.packet(packet, this.flags);
throw new Error('Callbacks are not supported when broadcasting');
}

// reset flags
this._rooms = [];
this.flags = {};
debug('emitting packet with ack id %d', this.nsp.ids);
this.acks[this.nsp.ids] = args.pop();
packet.id = this.nsp.ids++;
}

var rooms = this._rooms.slice(0);
var flags = assign({}, this.flags);

// reset flags
this._rooms = [];
this.flags = {};

if (rooms.length || flags.broadcast) {
this.adapter.broadcast(packet, {
except: [this.id],
rooms: rooms,
flags: flags
});
} else {
// dispatch packet
this.packet(packet, flags);
}
return this;
};
Expand Down

0 comments on commit db0c699

Please sign in to comment.