diff options
author | polwex <polwex@sortug.com> | 2025-09-17 12:24:41 +0700 |
---|---|---|
committer | polwex <polwex@sortug.com> | 2025-09-17 12:24:41 +0700 |
commit | 387af8fc1603805b02ce03f8adba4fa73a954f7c (patch) | |
tree | 6ac4fe9c33a14d9da418a97955a38efb9338d869 /shim/ws-shim/src/client.ts | |
parent | 31a47ce72255bb56920e417d250541b04be82648 (diff) |
relay much more robust
Diffstat (limited to 'shim/ws-shim/src/client.ts')
-rw-r--r-- | shim/ws-shim/src/client.ts | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/shim/ws-shim/src/client.ts b/shim/ws-shim/src/client.ts index b7df3e9..c261f60 100644 --- a/shim/ws-shim/src/client.ts +++ b/shim/ws-shim/src/client.ts @@ -13,6 +13,8 @@ export class Relay { private messageQueue: ClientMessage[] = []; private reconnectTimer: Timer | null = null; private reconnectAttempts = 0; + private recentDisconnects = 0; + private disconnectTimer: Timer | null = null; private maxReconnectAttempts = 5; private reconnectDelay = 1000; @@ -21,14 +23,22 @@ export class Relay { public onconnect?: () => void; public ondisconnect?: () => void; + public onfailure?: () => void; public onerror?: (error: Error) => void; public onnotice?: (message: string) => void; + public onmsg: (message: RelayMessage) => void; - constructor(url: string) { + constructor(url: string, onmsg: (msg: RelayMessage) => void) { this.url = url; + this.onmsg = onmsg; } async connect(): Promise<void> { + // console.log("connecting to ws", { + // url: this.url, + // tries: this.reconnectAttempts, + // bad: this.recentDisconnects, + // }); return new Promise((resolve, reject) => { if (this.ws?.readyState === WebSocket.OPEN) { resolve(); @@ -82,11 +92,26 @@ export class Relay { } private attemptReconnect(): void { - if (this.reconnectAttempts >= this.maxReconnectAttempts) { + if ( + this.reconnectAttempts >= this.maxReconnectAttempts || + this.recentDisconnects >= this.maxReconnectAttempts + ) { this.status = "error"; + this.onfailure?.(); this.onerror?.(new Error("Max reconnection attempts reached")); return; } + if (this.disconnectTimer) { + clearTimeout(this.disconnectTimer); + this.disconnectTimer = null; + } + + this.recentDisconnects = this.recentDisconnects + 1; + const ddelay = 10_000; + this.disconnectTimer = setTimeout(() => { + console.log("resetting disconnect timer"); + this.recentDisconnects = 0; + }, ddelay); this.reconnectAttempts++; const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1); @@ -118,6 +143,7 @@ export class Relay { private handleMessage(data: string): void { try { const message = JSON.parse(data) as RelayMessage; + this.onmsg(message); switch (message[0]) { case "EVENT": { @@ -135,6 +161,7 @@ export class Relay { if (!success) { console.error(`Event ${eventId} rejected: ${messag}`); } + break; } @@ -147,6 +174,8 @@ export class Relay { case "CLOSED": { const [, subscriptionId, messag] = message; + const subscription = this.subscriptions.get(subscriptionId); + subscription?.onclose?.(messag); this.subscriptions.delete(subscriptionId); console.log(`Subscription ${subscriptionId} closed: ${messag}`); break; @@ -178,8 +207,13 @@ export class Relay { handlers: { onevent?: (event: NostrEvent) => void; oneose?: () => void; + onclose?: (message: string) => void; }, ): () => void { + // TODO mmm + const has = this.subscriptions.has(id); + if (has) return () => {}; + // const subscription: Subscription = { id, filters, |