From 53c73749a829b2c98d9a5e45c48f0ae5a22c056c Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 5 Jan 2021 10:38:54 +0100 Subject: [PATCH] fix: emit a connect_error event upon connection failure Related: https://github.com/socketio/socket.io/issues/3734 --- lib/socket.ts | 13 +++++++++++++ test/socket.ts | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/socket.ts b/lib/socket.ts index 3deb8c413..afa567e3a 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -84,6 +84,7 @@ export class Socket extends Emitter { this.subs = [ on(io, "open", this.onopen.bind(this)), on(io, "packet", this.onpacket.bind(this)), + on(io, "error", this.onerror.bind(this)), on(io, "close", this.onclose.bind(this)), ]; } @@ -204,6 +205,18 @@ export class Socket extends Emitter { } } + /** + * Called upon engine or manager `error`. + * + * @param err + * @private + */ + private onerror(err: Error) { + if (!this.connected) { + super.emit("connect_error", err); + } + } + /** * Called upon engine `close`. * diff --git a/test/socket.ts b/test/socket.ts index b27be71d5..263404143 100644 --- a/test/socket.ts +++ b/test/socket.ts @@ -50,6 +50,31 @@ describe("socket", function () { }, 300); }); + it("fire a connect_error event when the connection cannot be established", (done) => { + const socket = io("http://localhost:9823", { + forceNew: true, + timeout: 100, + }); + socket.on("connect_error", () => { + socket.close(); + done(); + }); + }); + + it("doesn't fire a connect_error event when the connection is already established", (done) => { + const socket = io({ forceNew: true }); + socket.on("connect", () => { + socket.io.engine.close(true); + }); + socket.on("connect_error", () => { + done(new Error("should not happen")); + }); + setTimeout(() => { + socket.close(); + done(); + }, 300); + }); + it("should change socket.id upon reconnection", (done) => { const socket = io({ forceNew: true }); socket.on("connect", () => {