From dd0fd539e68b5851a2ea31c751a8b2074a7805f7 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Sun, 23 Dec 2012 23:19:05 -0300 Subject: [PATCH] namespace: fix `emit` --- lib/namespace.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/namespace.js b/lib/namespace.js index 4524b4bff1..047482132d 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -187,18 +187,21 @@ Namespace.prototype.emit = function(ev){ if (~exports.events.indexOf(ev)) { emit.apply(this, arguments); } else { - for (var i = 0; i < this.sockets.length; i++) { - var socket = this.sockets[i]; - this.rooms.forEach(function(room){ - socket.to(room); - }); - this.flags.forEach(function(flag){ - socket = socket[flag]; - }); - socket.emit.apply(socket, arguments); + // set up packet object + var args = Array.prototype.slice.call(arguments); + var packet = { type: parser.EVENT, data: args }; + + if ('function' == typeof args[args.length - 1]) { + throw new Error('Callbacks are not supported when broadcasting'); } - if (this.flags.length) this.flags = []; - if (this.rooms.lenth) this.rooms = []; + + this.adapter.broadcast(packet, { + rooms: this.rooms, + flags: this.flags + }); + + delete this.rooms; + delete this.flags; } return this; };