Skip to content

Commit 13c6d2e

Browse files
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: #5126
1 parent 8adcfbf commit 13c6d2e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

packages/socket.io-client/lib/manager.ts

+3
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ export class Manager<
201201
public reconnection(v?: boolean): this | boolean {
202202
if (!arguments.length) return this._reconnection;
203203
this._reconnection = !!v;
204+
if (!v) {
205+
this.skipReconnect = true;
206+
}
204207
return this;
205208
}
206209

packages/socket.io-client/test/connection.ts

+22
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,28 @@ describe("connection", () => {
520520
});
521521
});
522522

523+
it("should stop trying to reconnect", () => {
524+
return wrap((done) => {
525+
const manager = new Manager("http://localhost:9823", {
526+
reconnectionDelay: 10,
527+
});
528+
529+
manager.on("reconnect_error", () => {
530+
// disable current reconnection loop
531+
manager.reconnection(false);
532+
533+
manager.on("reconnect_attempt", () => {
534+
done(new Error("should not happen"));
535+
});
536+
537+
setTimeout(() => {
538+
manager._close();
539+
done();
540+
}, 100);
541+
});
542+
});
543+
});
544+
523545
// Ignore incorrect connection test for old IE due to no support for
524546
// `script.onerror` (see: http://requirejs.org/docs/api.html#ieloadfail)
525547
if (!global.document || hasCORS) {

0 commit comments

Comments
 (0)