From 050108b2281effda086b197cf174ee2e8e1aad79 Mon Sep 17 00:00:00 2001 From: Luke Olney Date: Wed, 30 Sep 2020 10:01:15 -0400 Subject: [PATCH] fix: fix reconnection after opening socket asynchronously (#1253) Closes https://github.com/socketio/socket.io/issues/3358 --- lib/socket.js | 2 +- test/connection.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 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..0a9edae2a 100644 --- a/test/connection.js +++ b/test/connection.js @@ -433,6 +433,35 @@ 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) {