From c56a29499477917ad66fecb47f3ea958f0d0073a Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Thu, 23 Apr 2015 00:20:09 +0200 Subject: [PATCH 01/10] Use a hash array for sockets in namespace. --- lib/index.js | 9 +++++---- lib/namespace.js | 15 +++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/index.js b/lib/index.js index 64de3b880a..6bae670024 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,10 @@ 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) { + sockets[id].onclose(); + } this.engine.close(); diff --git a/lib/namespace.js b/lib/namespace.js index 4ae0b154a5..fe31e57a2d 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; @@ -153,12 +153,13 @@ Namespace.prototype.add = function(client, fn){ var socket = new Socket(this, client); var self = this; this.run(socket, function(err){ - process.nextTick(function(){ + setImmediate(function(){ if ('open' == client.conn.readyState) { if (err) return socket.error(err.data || err.message); // track socket - self.sockets.push(socket); + self.sockets[socket.id] = socket; + // self.sockets.push(socket); // it's paramount that the internal `onconnect` logic // fires before user-set events to prevent state order @@ -185,9 +186,11 @@ 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]; + // var i = this.sockets.indexOf(socket); + // if (~i) { + // this.sockets.splice(i, 1); } else { debug('ignoring remove for %s', socket.id); } From 0c96efa0fd8f052ae7fd562c0288ae0d0ffd2ca4 Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Thu, 23 Apr 2015 00:20:37 +0200 Subject: [PATCH 02/10] Update test according to new data structure. Conflicts: test/socket.io.js --- 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 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(); }); From 54817502d36dece7bd3c5cc8ddf3b8cb3526908d Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Thu, 23 Apr 2015 00:21:35 +0200 Subject: [PATCH 03/10] Use improved socket.io-adapter. Conflicts: package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2caae9ccc6..3bcca28e0c 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "engine.io": "1.5.1", "socket.io-parser": "2.2.4", "socket.io-client": "1.3.5", - "socket.io-adapter": "0.3.1", + "socket.io-adapter": "https://github.com/manubb/socket.io-adapter/archive/efficient.tar.gz", "has-binary-data": "0.1.3", "debug": "2.1.0" }, From 693cfde1b018aa013c156d6d5c2e48a53d4d51ec Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Thu, 23 Apr 2015 00:50:28 +0200 Subject: [PATCH 04/10] Remove comments. --- lib/namespace.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/namespace.js b/lib/namespace.js index fe31e57a2d..63a72c5b94 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -159,7 +159,6 @@ Namespace.prototype.add = function(client, fn){ // track socket self.sockets[socket.id] = socket; - // self.sockets.push(socket); // it's paramount that the internal `onconnect` logic // fires before user-set events to prevent state order @@ -188,9 +187,6 @@ Namespace.prototype.add = function(client, fn){ Namespace.prototype.remove = function(socket){ if (socket.id in this.sockets) { delete this.sockets[socket.id]; - // var i = this.sockets.indexOf(socket); - // if (~i) { - // this.sockets.splice(i, 1); } else { debug('ignoring remove for %s', socket.id); } From 3922bbe5644a6f594f2b0612a4b856251efcc1cc Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Thu, 23 Apr 2015 01:05:37 +0200 Subject: [PATCH 05/10] 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 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); } From d2c1d3c2f7da2eb7ce6d4d2b7b8674aa26095024 Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Mon, 27 Apr 2015 12:20:39 +0200 Subject: [PATCH 06/10] Set correct adapter version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3bcca28e0c..4762007a54 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "engine.io": "1.5.1", "socket.io-parser": "2.2.4", "socket.io-client": "1.3.5", - "socket.io-adapter": "https://github.com/manubb/socket.io-adapter/archive/efficient.tar.gz", + "socket.io-adapter": "https://github.com/manubb/socket.io-adapter/archive/0.3.1-efficient.tar.gz", "has-binary-data": "0.1.3", "debug": "2.1.0" }, From af52b800360e056cda8f20d7a3d11950b4cbde6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lal?= Date: Tue, 24 Mar 2015 14:54:52 +0100 Subject: [PATCH 07/10] Let adapter.add find connected socket When a socket connects, it joins its own room, resulting in a call to adapter.add. The adapter in turn should be able to find the socket by id. --- lib/socket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; }; /** From 31e898e95e0adc872f96a0148fc63ecaa6bc1f09 Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Mon, 18 May 2015 11:39:36 +0200 Subject: [PATCH 08/10] Go back to process.nextTick in replacement of setImmediate. --- lib/namespace.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/namespace.js b/lib/namespace.js index 63a72c5b94..92bbb6edf0 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -153,7 +153,7 @@ Namespace.prototype.add = function(client, fn){ var socket = new Socket(this, client); var self = this; this.run(socket, function(err){ - setImmediate(function(){ + process.nextTick(function(){ if ('open' == client.conn.readyState) { if (err) return socket.error(err.data || err.message); From e0de897453eb711d229026a68c0d377ca6458203 Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Mon, 18 May 2015 11:40:54 +0200 Subject: [PATCH 09/10] Add a hasOwnProperty check. --- lib/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 6bae670024..2dbbfb573a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -345,7 +345,9 @@ Server.prototype.of = function(name, fn){ Server.prototype.close = function(){ var sockets = this.nsps['/'].sockets; for (var id in sockets) { - sockets[id].onclose(); + if (sockets.hasOwnProperty(id)) { + sockets[id].onclose(); + } } this.engine.close(); From 0fa530d0df7d02f10f7bbdaea99c27e1c9d84114 Mon Sep 17 00:00:00 2001 From: Manuel Baclet Date: Mon, 18 May 2015 15:00:09 +0200 Subject: [PATCH 10/10] Reset socket.io-adapter version to 0.3.1. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4762007a54..2caae9ccc6 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "engine.io": "1.5.1", "socket.io-parser": "2.2.4", "socket.io-client": "1.3.5", - "socket.io-adapter": "https://github.com/manubb/socket.io-adapter/archive/0.3.1-efficient.tar.gz", + "socket.io-adapter": "0.3.1", "has-binary-data": "0.1.3", "debug": "2.1.0" },