diff --git a/lib/socket.ts b/lib/socket.ts index f4aa567f..1a92f073 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -543,15 +543,17 @@ export class Socket< /** * Send the first packet of the queue, and wait for an acknowledgement from the server. + * @param force - whether to resend a packet that has not been acknowledged yet + * * @private */ - private _drainQueue() { + private _drainQueue(force = false) { debug("draining queue"); - if (this._queue.length === 0) { + if (!this.connected || this._queue.length === 0) { return; } const packet = this._queue[0]; - if (packet.pending) { + if (packet.pending && !force) { debug( "packet [%d] has already been sent and is waiting for an ack", packet.id @@ -776,6 +778,7 @@ export class Socket< this.connected = true; this.emitBuffered(); this.emitReserved("connect"); + this._drainQueue(true); } /** diff --git a/test/retry.ts b/test/retry.ts index 0bcece5c..e356b589 100644 --- a/test/retry.ts +++ b/test/retry.ts @@ -89,4 +89,25 @@ describe("retry", () => { }); }); }); + + it("should not drain the queue while the socket is disconnected", () => { + return wrap((done) => { + const socket = io(BASE_URL, { + forceNew: true, + autoConnect: false, + retries: 3, + ackTimeout: 10, + }); + + socket.emit("echo", 1, (err) => { + expect(err).to.be(null); + + success(done, socket); + }); + + setTimeout(() => { + socket.connect(); + }, 100); + }); + }); });