From 564c9bca179ca97762fab370d3530731e8c5a930 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 16 Mar 2024 13:38:08 -0300 Subject: [PATCH] don't try to send a ["CLOSE"] after the websocket is closed. addresses https://github.com/nbd-wtf/nostr-tools/pull/387 --- abstract-relay.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/abstract-relay.ts b/abstract-relay.ts index e0445bd..8189993 100644 --- a/abstract-relay.ts +++ b/abstract-relay.ts @@ -99,17 +99,20 @@ export class AbstractRelay { this.ws.onerror = ev => { reject((ev as any).message) if (this._connected) { + this._connected = false + this.connectionPromise = undefined this.onclose?.() this.closeAllSubscriptions('relay connection errored') - this._connected = false } } this.ws.onclose = async () => { - this.connectionPromise = undefined - this.onclose?.() - this.closeAllSubscriptions('relay connection closed') - this._connected = false + if (this._connected) { + this._connected = false + this.connectionPromise = undefined + this.onclose?.() + this.closeAllSubscriptions('relay connection closed') + } } this.ws.onmessage = this._onmessage.bind(this) @@ -338,7 +341,7 @@ export class Subscription { } public close(reason: string = 'closed by caller') { - if (!this.closed) { + if (!this.closed && this.relay.connected) { // if the connection was closed by the user calling .close() we will send a CLOSE message // otherwise this._open will be already set to false so we will skip this this.relay.send('["CLOSE",' + JSON.stringify(this.id) + ']')