Skip to content

Commit

Permalink
fix: fix reconnection after opening socket asynchronously (#1253)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolney committed Sep 30, 2020
1 parent b570025 commit 050108b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions test/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 050108b

Please sign in to comment.