Skip to content

Commit 050108b

Browse files
authored
fix: fix reconnection after opening socket asynchronously (#1253)
Closes socketio/socket.io#3358
1 parent b570025 commit 050108b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/socket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Socket.prototype.connect = function () {
104104
if (this.connected) return this;
105105

106106
this.subEvents();
107-
this.io.open(); // ensure open
107+
if (!this.io.reconnecting) this.io.open(); // ensure open
108108
if ('open' === this.io.readyState) this.onopen();
109109
this.emit('connecting');
110110
return this;

test/connection.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,35 @@ describe('connection', function () {
433433

434434
var socket = manager.socket('/invalid');
435435
});
436+
437+
it('should still try to reconnect twice after opening another socket asynchronously', function (done) {
438+
var manager = io.Manager(
439+
`http://localhost:9823`,
440+
{ reconnect: true, reconnectionAttempts: 2 }
441+
);
442+
var delay = Math.floor(manager.reconnectionDelay() * manager.randomizationFactor() * 0.5);
443+
delay = Math.max(delay, 10);
444+
445+
var reconnects = 0;
446+
var cb = function () {
447+
reconnects++;
448+
};
449+
450+
manager.on('reconnect_attempt', cb);
451+
452+
manager.on('reconnect_failed', function () {
453+
expect(reconnects).to.be(2);
454+
socket.disconnect();
455+
manager.close();
456+
done();
457+
});
458+
459+
var socket = manager.socket('/room1');
460+
461+
setTimeout(() => {
462+
manager.socket('/room2');
463+
}, delay);
464+
});
436465
}
437466

438467
it('should emit date as string', function (done) {

0 commit comments

Comments
 (0)