From c6b4f502d6a8ff9338739ff7aaca18a13d339819 Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Wed, 3 Jun 2015 09:53:33 +0200 Subject: [PATCH 1/4] Use much faster pop. --- lib/client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index 471aedc4ac..49f85946b6 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); } From 0c396899ac1038b3b5cc80a5f81200a3fa4f5770 Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Wed, 3 Jun 2015 09:54:21 +0200 Subject: [PATCH 2/4] Remove tabs. --- lib/index.js | 2 +- test/socket.io.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 27634923c5..90b29171fe 100644 --- a/lib/index.js +++ b/lib/index.js @@ -329,7 +329,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); diff --git a/test/socket.io.js b/test/socket.io.js index e8e28989c9..a1eacb4632 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -603,7 +603,7 @@ describe('socket.io', function(){ }); }); }); - + it('should not reuse same-namespace connections', function(done){ var srv = http(); var sio = io(srv); From 2a1e29f76116150c8b6dd38feb5bf61ccdcbbbb0 Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Wed, 3 Jun 2015 09:55:49 +0200 Subject: [PATCH 3/4] Use a hasharray for sockets in namespace. --- lib/index.js | 9 ++++++--- lib/namespace.js | 9 ++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index 90b29171fe..e00e8c4ce0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -346,9 +346,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 1d96a4b109..e5f411d56e 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -51,7 +51,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; @@ -161,7 +161,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 @@ -188,9 +188,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 (this.sockets.hasOwnProperty(socket.id)) { + delete this.sockets[socket.id]; } else { debug('ignoring remove for %s', socket.id); } From 911c884acebb059d66a0ef8f0dd01d91a11afdb4 Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Wed, 3 Jun 2015 09:56:10 +0200 Subject: [PATCH 4/4] Update tests according to new data structure. --- test/socket.io.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/socket.io.js b/test/socket.io.js index a1eacb4632..30364931a7 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -342,12 +342,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(); }); @@ -369,12 +369,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(); });