Skip to content

Commit

Permalink
fix(sio-client): allow to manually stop the reconnection loop
Browse files Browse the repository at this point in the history
```js
socket.io.on("reconnect_attempt", () => {
  socket.io.reconnection(false); // will now work properly
});
```

Related: #5126
  • Loading branch information
darrachequesne committed Sep 18, 2024
1 parent 8adcfbf commit 13c6d2e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/socket.io-client/lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ export class Manager<
public reconnection(v?: boolean): this | boolean {
if (!arguments.length) return this._reconnection;
this._reconnection = !!v;
if (!v) {
this.skipReconnect = true;
}
return this;
}

Expand Down
22 changes: 22 additions & 0 deletions packages/socket.io-client/test/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,28 @@ describe("connection", () => {
});
});

it("should stop trying to reconnect", () => {
return wrap((done) => {
const manager = new Manager("http://localhost:9823", {
reconnectionDelay: 10,
});

manager.on("reconnect_error", () => {
// disable current reconnection loop
manager.reconnection(false);

manager.on("reconnect_attempt", () => {
done(new Error("should not happen"));
});

setTimeout(() => {
manager._close();
done();
}, 100);
});
});
});

// Ignore incorrect connection test for old IE due to no support for
// `script.onerror` (see: http://requirejs.org/docs/api.html#ieloadfail)
if (!global.document || hasCORS) {
Expand Down

0 comments on commit 13c6d2e

Please sign in to comment.