From 9aee198d4088d6987cb39a3ae4036d0b91e75ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=A0ev=C4=8D=C3=ADk?= Date: Tue, 23 May 2023 15:50:05 +0200 Subject: [PATCH] Make client keepAlive use interval to ping server properly --- src/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 45d99c5..c27d431 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,7 @@ export class ElectrumClient extends Client { private timeLastCall: number private persistencePolicy: Required private electrumConfig: ElectrumConfig | null - private timeout: NodeJS.Timeout | null + private pingInterval: NodeJS.Timer | null versionInfo: [string, string] constructor(port: number, host: string, protocol: Protocol, callbacks?: Callbacks) { @@ -36,7 +36,7 @@ export class ElectrumClient extends Client { callback: null, } this.electrumConfig = null - this.timeout = null + this.pingInterval = null this.versionInfo = ['', ''] } @@ -117,8 +117,8 @@ export class ElectrumClient extends Client { // ElectrumX persistancy private keepAlive(): void { - if (this.timeout != null) { - clearTimeout(this.timeout) + if (this.pingInterval != null) { + clearInterval(this.pingInterval) } let pingPeriod = 120000 @@ -126,7 +126,7 @@ export class ElectrumClient extends Client { pingPeriod = this.persistencePolicy.pingPeriod } - this.timeout = setTimeout(() => { + this.pingInterval = setInterval(() => { if (this.timeLastCall !== 0 && Date.now() > this.timeLastCall + pingPeriod) { this.server_ping().catch((error) => { this.log(`Keep-Alive ping failed: ${error}`) @@ -138,8 +138,8 @@ export class ElectrumClient extends Client { close(): void { super.close() - if (this.timeout != null) { - clearTimeout(this.timeout) + if (this.pingInterval != null) { + clearInterval(this.pingInterval) } // eslint-disable-next-line no-multi-assign