diff --git a/lib/socket.ts b/lib/socket.ts index 1689f4ac..a8f189f4 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -167,7 +167,11 @@ export class Socket< * }); */ public connected: boolean = false; - + /** + * Whether the connection state was recovered after a temporary disconnection. In that case, any missed packets will + * be transmitted by the server. + */ + public recovered: boolean = false; /** * Credentials that are sent when accessing a namespace. * @@ -651,6 +655,7 @@ export class Socket< private onconnect(id: string, pid: string) { debug("socket connected with id %s", id); this.id = id; + this.recovered = pid && this._pid === pid; this._pid = pid; // defined only if connection state recovery is enabled this.connected = true; this.emitBuffered(); diff --git a/test/connection-state-recovery.ts b/test/connection-state-recovery.ts index 8aa9ee41..3567b0de 100644 --- a/test/connection-state-recovery.ts +++ b/test/connection-state-recovery.ts @@ -9,7 +9,9 @@ describe("connection state recovery", () => { forceNew: true, }); - socket.emit("hi"); + expect(socket.recovered).to.eql(false); + + socket.emit("hi"); // init the offset socket.on("hi", () => { const id = socket.id; @@ -18,6 +20,7 @@ describe("connection state recovery", () => { socket.on("connect", () => { expect(socket.id).to.eql(id); // means that the reconnection was successful + expect(socket.recovered).to.eql(true); // means that the reconnection was successful done(); }); });