diff --git a/packages/socket.io-client/lib/socket.ts b/packages/socket.io-client/lib/socket.ts index 13c0ed97c..67059f537 100644 --- a/packages/socket.io-client/lib/socket.ts +++ b/packages/socket.io-client/lib/socket.ts @@ -833,7 +833,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.recovered = !!pid && this._pid === pid; this._pid = pid; // defined only if connection state recovery is enabled this.connected = true; this.emitBuffered(); diff --git a/packages/socket.io-client/test/connection-state-recovery.ts b/packages/socket.io-client/test/connection-state-recovery.ts index 42c826b0e..2e33100b8 100644 --- a/packages/socket.io-client/test/connection-state-recovery.ts +++ b/packages/socket.io-client/test/connection-state-recovery.ts @@ -1,5 +1,6 @@ import expect = require("expect.js"); import { io } from ".."; +import { PacketType } from "socket.io-parser"; import { wrap, BASE_URL, success } from "./support/util"; describe("connection state recovery", () => { @@ -27,4 +28,19 @@ describe("connection state recovery", () => { }); }); }); + + it("should not be recovered when the server does not use connection state recovery", () => { + const socket = io(BASE_URL, { forceNew: true, autoConnect: false }); + + // the CONNECT packet of a server without connection state recovery has no "pid" attribute + // @ts-ignore method 'onpacket' is private + socket.onpacket({ + type: PacketType.CONNECT, + nsp: "/", + data: { sid: "abc" }, + }); + + expect(socket.connected).to.be(true); + expect(socket.recovered).to.be(false); + }); });