Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reconnection after opening socket asynchronously #1253

Merged
merged 2 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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