diff --git a/abstract-relay.ts b/abstract-relay.ts index 6fc6cbb..84696be 100644 --- a/abstract-relay.ts +++ b/abstract-relay.ts @@ -45,7 +45,7 @@ export class AbstractRelay { public enableReconnect: boolean private connectionTimeoutHandle: ReturnType | undefined private reconnectTimeoutHandle: ReturnType | undefined - private pingTimeoutHandle: ReturnType | undefined + private pingIntervalHandle: ReturnType | undefined private reconnectAttempts: number = 0 private closedIntentionally: boolean = false @@ -111,9 +111,9 @@ export class AbstractRelay { } private handleHardClose(reason: string) { - if (this.pingTimeoutHandle) { - clearTimeout(this.pingTimeoutHandle) - this.pingTimeoutHandle = undefined + if (this.pingIntervalHandle) { + clearInterval(this.pingIntervalHandle) + this.pingIntervalHandle = undefined } this._connected = false @@ -177,7 +177,7 @@ export class AbstractRelay { } if (this.enablePing) { - this.pingpong() + this.pingIntervalHandle = setInterval(() => this.pingpong(), this.pingFrequency) } resolve() } @@ -247,10 +247,8 @@ export class AbstractRelay { this.ws && this.ws.ping && (this.ws as any).once ? this.waitForPingPong() : this.waitForDummyReq(), new Promise(res => setTimeout(() => res(false), this.pingTimeout)), ]) - if (result) { - // schedule another pingpong - this.pingTimeoutHandle = setTimeout(() => this.pingpong(), this.pingFrequency) - } else { + + if (!result) { // pingpong closing socket if (this.ws?.readyState === this._WebSocket.OPEN) { this.ws?.close() @@ -458,9 +456,9 @@ export class AbstractRelay { clearTimeout(this.reconnectTimeoutHandle) this.reconnectTimeoutHandle = undefined } - if (this.pingTimeoutHandle) { - clearTimeout(this.pingTimeoutHandle) - this.pingTimeoutHandle = undefined + if (this.pingIntervalHandle) { + clearInterval(this.pingIntervalHandle) + this.pingIntervalHandle = undefined } this.closeAllSubscriptions('relay connection closed by us') this._connected = false diff --git a/jsr.json b/jsr.json index 6615697..8d4aa78 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@nostr/tools", - "version": "2.19.1", + "version": "2.19.2", "exports": { ".": "./index.ts", "./core": "./core.ts", diff --git a/package.json b/package.json index e79107d..e7e784d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "nostr-tools", - "version": "2.19.1", + "version": "2.19.2", "description": "Tools for making a Nostr client.", "repository": { "type": "git",