From 604c07f24c94dbe18cc631485a22623e364d1585 Mon Sep 17 00:00:00 2001 From: Luke Olney Date: Fri, 14 Dec 2018 18:37:57 -0800 Subject: [PATCH 1/2] Fix reconnection after opening socket asynchronously https://github.com/socketio/socket.io/issues/3358 --- lib/socket.js | 2 +- test/connection.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/socket.js b/lib/socket.js index cf441c3fa..a0e1d1320 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -104,7 +104,7 @@ Socket.prototype.connect = function () { if (this.connected) return this; this.subEvents(); - this.io.open(); // ensure open + if (!this.io.reconnecting) this.io.open(); // ensure open if ('open' === this.io.readyState) this.onopen(); this.emit('connecting'); return this; diff --git a/test/connection.js b/test/connection.js index be49f21b4..07b38eb60 100644 --- a/test/connection.js +++ b/test/connection.js @@ -433,6 +433,37 @@ describe('connection', function () { var socket = manager.socket('/invalid'); }); + + it('should still try to reconnect twice after opening another socket asynchronously', function (done) { + var manager = io.Manager( + `http://localhost:9823`, + { reconnect: true, reconnectionAttempts: 2 } + ); + var delay = Math.floor(manager.reconnectionDelay() * manager.randomizationFactor() * 0.5); + delay = Math.max(delay, 10); + + var reconnects = 0; + var cb = function () { + reconnects++; + }; + + manager.on('reconnect_attempt', cb); + + manager.on('reconnect_failed', function () { + expect(reconnects).to.be(2); + socket.disconnect(); + manager.close(); + done(); + }); + + var socket = manager.socket('/room1'); + + setTimeout(() => { + manager.socket( + `/room2`, + ); + }, delay); + }); } it('should emit date as string', function (done) { From 545564580c4d2946fad709a0ef0898937c0aa49e Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 30 Sep 2020 15:52:21 +0200 Subject: [PATCH 2/2] minor fix --- test/connection.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/connection.js b/test/connection.js index 07b38eb60..0a9edae2a 100644 --- a/test/connection.js +++ b/test/connection.js @@ -459,9 +459,7 @@ describe('connection', function () { var socket = manager.socket('/room1'); setTimeout(() => { - manager.socket( - `/room2`, - ); + manager.socket('/room2'); }, delay); }); }