diff --git a/lib/client.js b/lib/client.js index 440c75edda..3121b09ffc 100644 --- a/lib/client.js +++ b/lib/client.js @@ -92,7 +92,7 @@ Client.prototype.disconnect = function(){ var socket; // we don't use a for loop because the length of // `sockets` changes upon each iteration - while (socket = this.sockets.shift()) { + while (socket = this.sockets.pop()) { socket.disconnect(); } this.close(); @@ -226,7 +226,7 @@ Client.prototype.onclose = function(reason){ // `nsps` and `sockets` are cleaned up seamlessly var socket; - while (socket = this.sockets.shift()) { + while (socket = this.sockets.pop()) { socket.onclose(reason); } diff --git a/lib/index.js b/lib/index.js index 64de3b880a..2dbbfb573a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -326,7 +326,7 @@ Server.prototype.onconnection = function(conn){ Server.prototype.of = function(name, fn){ if (String(name)[0] !== '/') name = '/' + name; - + if (!this.nsps[name]) { debug('initializing namespace %s', name); var nsp = new Namespace(this, name); @@ -343,9 +343,12 @@ Server.prototype.of = function(name, fn){ */ Server.prototype.close = function(){ - this.nsps['/'].sockets.forEach(function(socket){ - socket.onclose(); - }); + var sockets = this.nsps['/'].sockets; + for (var id in sockets) { + if (sockets.hasOwnProperty(id)) { + sockets[id].onclose(); + } + } this.engine.close(); diff --git a/lib/namespace.js b/lib/namespace.js index 4ae0b154a5..92bbb6edf0 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -48,7 +48,7 @@ var emit = Emitter.prototype.emit; function Namespace(server, name){ this.name = name; this.server = server; - this.sockets = []; + this.sockets = {}; this.connected = {}; this.fns = []; this.ids = 0; @@ -158,7 +158,7 @@ Namespace.prototype.add = function(client, fn){ if (err) return socket.error(err.data || err.message); // track socket - self.sockets.push(socket); + self.sockets[socket.id] = socket; // it's paramount that the internal `onconnect` logic // fires before user-set events to prevent state order @@ -185,9 +185,8 @@ Namespace.prototype.add = function(client, fn){ */ Namespace.prototype.remove = function(socket){ - var i = this.sockets.indexOf(socket); - if (~i) { - this.sockets.splice(i, 1); + if (socket.id in this.sockets) { + delete this.sockets[socket.id]; } else { debug('ignoring remove for %s', socket.id); } diff --git a/lib/socket.js b/lib/socket.js index 1de3f3fca6..a2beb58c79 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -271,9 +271,9 @@ Socket.prototype.leaveAll = function(){ Socket.prototype.onconnect = function(){ debug('socket connected - writing packet'); + this.nsp.connected[this.id] = this; this.join(this.id); this.packet({ type: parser.CONNECT }); - this.nsp.connected[this.id] = this; }; /** diff --git a/test/socket.io.js b/test/socket.io.js index a04b53c901..9aeb36477a 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -331,12 +331,12 @@ describe('socket.io', function(){ var clientSocket = client(srv, { reconnection: false }); clientSocket.on('disconnect', function init() { - expect(sio.nsps['/'].sockets.length).to.equal(0); + expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(0); server.listen(PORT); }); clientSocket.on('connect', function init() { - expect(sio.nsps['/'].sockets.length).to.equal(1); + expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1); sio.close(); }); @@ -358,12 +358,12 @@ describe('socket.io', function(){ var clientSocket = ioc('ws://0.0.0.0:' + PORT); clientSocket.on('disconnect', function init() { - expect(sio.nsps['/'].sockets.length).to.equal(0); + expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(0); server.listen(PORT); }); clientSocket.on('connect', function init() { - expect(sio.nsps['/'].sockets.length).to.equal(1); + expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1); sio.close(); });