Skip to content

Commit

Permalink
refactor: simplify the heartbeat code
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jun 18, 2024
1 parent 5359bae commit f521cba
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ export class Socket extends EventEmitter {

if (this.protocol === 3) {
// in protocol v3, the client sends a ping, and the server answers with a pong
this.resetPingTimeout(
this.server.opts.pingInterval + this.server.opts.pingTimeout
);
this.resetPingTimeout();
} else {
// in protocol v4, the server sends a ping, and the client answers with a pong
this.schedulePing();
Expand Down Expand Up @@ -193,7 +191,7 @@ export class Socket extends EventEmitter {
this.server.opts.pingTimeout
);
this.sendPacket("ping");
this.resetPingTimeout(this.server.opts.pingTimeout);
this.resetPingTimeout();
}, this.server.opts.pingInterval);
}

Expand All @@ -202,12 +200,17 @@ export class Socket extends EventEmitter {
*
* @api private
*/
private resetPingTimeout(timeout) {
private resetPingTimeout() {
clearTimeout(this.pingTimeoutTimer);
this.pingTimeoutTimer = setTimeout(() => {
if (this.readyState === "closed") return;
this.onClose("ping timeout");
}, timeout);
this.pingTimeoutTimer = setTimeout(
() => {
if (this.readyState === "closed") return;
this.onClose("ping timeout");
},
this.protocol === 3
? this.server.opts.pingInterval + this.server.opts.pingTimeout
: this.server.opts.pingTimeout
);
}

/**
Expand Down

0 comments on commit f521cba

Please sign in to comment.