diff --git a/packages/socket.io-client/lib/socket.ts b/packages/socket.io-client/lib/socket.ts index 973bafb3..a1713a18 100644 --- a/packages/socket.io-client/lib/socket.ts +++ b/packages/socket.io-client/lib/socket.ts @@ -542,8 +542,7 @@ export class Socket< args.push((err, ...responseArgs) => { if (packet !== this._queue[0]) { - // the packet has already been acknowledged - return; + return debug("packet [%d] already acknowledged", packet.id); } const hasError = err !== null; if (hasError) { @@ -834,8 +833,8 @@ export class Socket< this._pid = pid; // defined only if connection state recovery is enabled this.connected = true; this.emitBuffered(); - this.emitReserved("connect"); this._drainQueue(true); + this.emitReserved("connect"); } /** diff --git a/packages/socket.io-client/test/retry.ts b/packages/socket.io-client/test/retry.ts index 9d81f01e..ba1f72e3 100644 --- a/packages/socket.io-client/test/retry.ts +++ b/packages/socket.io-client/test/retry.ts @@ -110,4 +110,40 @@ describe("retry", () => { }, 100); }); }); + + it.only("should not emit a packet twice in the 'connect' handler", () => { + return wrap((done) => { + const socket = io(BASE_URL, { + forceNew: true, + retries: 3, + }); + + const received: string[] = []; + const sent: string[] = []; + + socket.io.engine.on("packetCreate", ({ data }) => { + sent.push(data); + }); + + socket.io.engine.on("packet", ({ data }) => { + received.push(data); + }); + + socket.on("connect", () => { + socket.emit("echo", null); + }); + + setTimeout(() => { + expect(sent).to.eql(["0", '20["echo",null]']); + + expect(received.length).to.eql(3); + // 1: engine.io OPEN packet + // 2: socket.io CONNECT packet + // 3: ack packet + expect(received[2]).to.eql("30[null]"); + + success(done, socket); + }, 100); + }); + }); });