Skip to content

Commit

Permalink
Fix ping pong after cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed Aug 17, 2024
1 parent d9053f2 commit b0e7790
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,16 @@ class TuyaDevice extends EventEmitter {
// Check for response
const now = new Date();

clearTimeout(this._pingPongTimeout);
this._pingPongTimeout = setTimeout(() => {
if (this._lastPingAt < now) {
this.disconnect();
}
}, this._responseTimeout * 1000);
if (this._pingPongTimeout === null) {
// If we do not expect a pong from a former ping, we need to set a timeout
this._pingPongTimeout = setTimeout(() => {
if (this._lastPingAt < now) {
this.disconnect();
}
}, this._responseTimeout * 1000);
} else {
debug('There was no response to the last ping.');
}

// Send ping
this.client.write(buffer);
Expand Down Expand Up @@ -789,6 +793,8 @@ class TuyaDevice extends EventEmitter {
*/
this.emit('heartbeat');

clearTimeout(this._pingPongTimeout);
this._pingPongTimeout = null;
this._lastPingAt = new Date();

return;
Expand Down

0 comments on commit b0e7790

Please sign in to comment.