fix(sio-client): allow to manually stop the reconnection loop

```js
socket.io.on("reconnect_attempt", () => {
  socket.io.reconnection(false); // will now work properly
});
```

Related: https://github.com/socketio/socket.io/issues/5126
This commit is contained in:
Damien Arrachequesne
2024-09-18 10:50:56 +02:00
parent 8adcfbfde5
commit 13c6d2e89d
2 changed files with 25 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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) {