Skip to content

Commit

Permalink
refactor: add recovered flag after a successful recovery
Browse files Browse the repository at this point in the history
Following b4e20c5
  • Loading branch information
darrachequesne committed Jan 30, 2023
1 parent 47b979d commit f27cba5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion test/connection-state-recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
});
});
Expand Down

0 comments on commit f27cba5

Please sign in to comment.