diff --git a/lib/manager.ts b/lib/manager.ts index e42378bf..09bf5efd 100644 --- a/lib/manager.ts +++ b/lib/manager.ts @@ -520,13 +520,7 @@ export class Manager< debug("disconnect"); this.skipReconnect = true; this._reconnecting = false; - if ("opening" === this._readyState) { - // `onclose` will not fire because - // an open event never happened - this.cleanup(); - } - this.backoff.reset(); - this._readyState = "closed"; + this.onclose("forced close"); if (this.engine) this.engine.close(); } @@ -545,7 +539,7 @@ export class Manager< * @private */ private onclose(reason: string): void { - debug("onclose"); + debug("closed due to %s", reason); this.cleanup(); this.backoff.reset(); diff --git a/test/socket.ts b/test/socket.ts index 47a65a7f..0c1f0031 100644 --- a/test/socket.ts +++ b/test/socket.ts @@ -231,6 +231,25 @@ describe("socket", function () { }, 100); }); + it("should properly disconnect then reconnect", (done) => { + const socket = io("/", { forceNew: true, transports: ["websocket"] }); + + let count = 0; + + socket.once("connect", () => { + socket.disconnect().connect(); + }); + + socket.on("disconnect", () => { + count++; + }); + + setTimeout(() => { + expect(count).to.eql(1); + success(done, socket); + }, 200); + }); + it("should throw on reserved event", () => { const socket = io("/no", { forceNew: true });