diff --git a/packages/socket.io-client/lib/socket.ts b/packages/socket.io-client/lib/socket.ts index 87ffa4bec1..ec646fad4e 100644 --- a/packages/socket.io-client/lib/socket.ts +++ b/packages/socket.io-client/lib/socket.ts @@ -182,6 +182,20 @@ export class Socket< */ private _lastOffset: string; + /** + * Whether a CONNECT packet was sent and the server has not answered it yet. + * + * @private + */ + private _connectPending = false; + + /** + * Removes the listener added by {@link disconnect()} during the handshake, if any. + * + * @private + */ + private _cancelPendingDisconnect?: () => void; + /** * Whether the socket is currently connected to the server. * @@ -360,6 +374,15 @@ export class Socket< public connect(): this { if (this.connected) return this; + if (this._cancelPendingDisconnect) { + // the server has not answered the previous CONNECT packet yet: wait for that answer instead of sending a new one + debug("reconnect during the handshake (%s)", this.nsp); + this._cancelPendingDisconnect(); + this._connectPending = true; + this.subEvents(); + return this; + } + this.subEvents(); if (!this.io["_reconnecting"]) this.io.open(); // ensure open if ("open" === this.io._readyState) this.onopen(); @@ -637,6 +660,7 @@ export class Socket< * @private */ private _sendConnectPacket(data: Record) { + this._connectPending = true; this.packet({ type: PacketType.CONNECT, data: this._pid @@ -670,6 +694,7 @@ export class Socket< ): void { debug("close (%s)", reason); this.connected = false; + this._connectPending = false; delete this.id; this.emitReserved("disconnect", reason, description); this._clearAcks(); @@ -738,6 +763,7 @@ export class Socket< break; case PacketType.CONNECT_ERROR: + this._connectPending = false; this.destroy(); const err = new Error(packet.data.message) as ExtendedError; err.data = packet.data.data; @@ -839,6 +865,7 @@ export class Socket< this.id = id; this.recovered = pid && this._pid === pid; this._pid = pid; // defined only if connection state recovery is enabled + this._connectPending = false; this.connected = true; this.emitBuffered(); this._drainQueue(true); @@ -908,6 +935,8 @@ export class Socket< if (this.connected) { debug("performing disconnect (%s)", this.nsp); this.packet({ type: PacketType.DISCONNECT }); + } else if (this._connectPending && this.active) { + this._disconnectUponConnect(); } // remove socket from pool @@ -920,6 +949,41 @@ export class Socket< return this; } + /** + * Called when the socket is disconnected while the server has not answered the CONNECT packet yet. The DISCONNECT + * packet cannot be sent right away (the server would close the whole connection, since it has no socket for this + * namespace yet), so it is sent once the server has answered. + * + * @private + */ + private _disconnectUponConnect(): void { + debug("disconnect during the handshake (%s)", this.nsp); + this._connectPending = false; + const cleanup = () => { + subs.forEach((subDestroy) => subDestroy()); + this._cancelPendingDisconnect = undefined; + }; + const subs = [ + on(this.io, "packet", (packet: Packet) => { + if (packet.nsp !== this.nsp) return; + if (packet.type === PacketType.CONNECT) { + cleanup(); + if (packet.data && packet.data.sid) { + debug( + "server accepted the connection, now disconnecting (%s)", + this.nsp, + ); + this.packet({ type: PacketType.DISCONNECT }); + } + } else if (packet.type === PacketType.CONNECT_ERROR) { + cleanup(); + } + }), + on(this.io, "close", cleanup), + ]; + this._cancelPendingDisconnect = cleanup; + } + /** * Alias for {@link disconnect()}. * diff --git a/packages/socket.io-client/test/socket.ts b/packages/socket.io-client/test/socket.ts index 0149ad64b6..737810f84d 100644 --- a/packages/socket.io-client/test/socket.ts +++ b/packages/socket.io-client/test/socket.ts @@ -167,6 +167,75 @@ describe("socket", () => { }); }); + describe("disconnect during the handshake", () => { + it("should disconnect on the server side once the server answers the CONNECT packet", () => { + return wrap((done) => { + const socket = io(BASE_URL, { forceNew: true }); + + socket.on("connect", () => { + const slow = socket.io.socket("/slow-handshake"); + slow.on("connect", () => { + done(new Error("should not happen")); + }); + // the server answers the CONNECT packet after 100 ms + slow.disconnect(); + expect(slow.connected).to.be(false); + + setTimeout(() => { + socket.emit("getSlowHandshakeSocketIds", (ids) => { + expect(ids).to.eql([]); + expect(socket.connected).to.be(true); + socket.disconnect(); + done(); + }); + }, 300); + }); + }); + }); + + it("should connect once when reconnected before the server answers the CONNECT packet", () => { + return wrap((done) => { + const socket = io(BASE_URL, { forceNew: true }); + + socket.on("connect", () => { + const slow = socket.io.socket("/slow-handshake"); + let connectCount = 0; + slow.on("connect", () => { + connectCount++; + }); + slow.disconnect(); + slow.connect(); + + setTimeout(() => { + expect(connectCount).to.eql(1); + expect(slow.connected).to.be(true); + socket.emit("getSlowHandshakeSocketIds", (ids) => { + expect(ids).to.eql([slow.id]); + slow.disconnect(); + socket.disconnect(); + done(); + }); + }, 300); + }); + }); + }); + + it("should close the connection when the socket was the only one", () => { + return wrap((done) => { + const socket = io(BASE_URL + "/slow-handshake", { forceNew: true }); + + socket.io.engine.on("open", () => { + socket.disconnect(); + }); + socket.io.on("close", (reason) => { + expect(reason).to.eql("forced close"); + expect(socket.connected).to.be(false); + done(); + }); + }); + }); + }); + describe("query option", () => { it("should accept an object (default namespace)", () => { return wrap((done) => { diff --git a/packages/socket.io-client/test/support/server.ts b/packages/socket.io-client/test/support/server.ts index 52a7d18f6a..f8bed707f9 100644 --- a/packages/socket.io-client/test/support/server.ts +++ b/packages/socket.io-client/test/support/server.ts @@ -29,6 +29,16 @@ export function createServer() { // register namespace }); + server + .of("/slow-handshake") + .use((socket, next) => { + // delay the answer to the CONNECT packet + setTimeout(next, 100); + }) + .on("connection", () => { + // register namespace + }); + server.of("/abc").on("connection", (socket) => { socket.emit("handshake", socket.handshake); }); @@ -60,6 +70,10 @@ export function createServer() { cb(arg); }); + socket.on("getSlowHandshakeSocketIds", (cb) => { + cb([...server.of("/slow-handshake").sockets.keys()]); + }); + // ack tests socket.on("ack", () => { socket.emit("ack", (a, b) => {