Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/socket.io-client/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions packages/socket.io-client/test/connection-state-recovery.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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);
});
});