mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
WIP: hack in pingpong #495
TypeScript does not like the duck typing of .on and .ping (only valid on Node ws).
This commit is contained in:
committed by
fiatjaf_
parent
de1cf0ed60
commit
b3cc9f50e5
@@ -49,6 +49,9 @@ export class AbstractRelay {
|
|||||||
this.url = normalizeURL(url)
|
this.url = normalizeURL(url)
|
||||||
this.verifyEvent = opts.verifyEvent
|
this.verifyEvent = opts.verifyEvent
|
||||||
this._WebSocket = opts.websocketImplementation || WebSocket
|
this._WebSocket = opts.websocketImplementation || WebSocket
|
||||||
|
// this.pingHeartBeat = opts.pingHeartBeat
|
||||||
|
// this.pingFrequency = opts.pingFrequency
|
||||||
|
// this.pingTimeout = opts.pingTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
|
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
|
||||||
@@ -102,6 +105,9 @@ export class AbstractRelay {
|
|||||||
this.ws.onopen = () => {
|
this.ws.onopen = () => {
|
||||||
clearTimeout(this.connectionTimeoutHandle)
|
clearTimeout(this.connectionTimeoutHandle)
|
||||||
this._connected = true
|
this._connected = true
|
||||||
|
if (this.ws && this.ws.ping) { // && this.pingHeartBeat
|
||||||
|
this.pingpong()
|
||||||
|
}
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,6 +139,31 @@ export class AbstractRelay {
|
|||||||
return this.connectionPromise
|
return this.connectionPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async receivePong() {
|
||||||
|
return new Promise((res, err) => {
|
||||||
|
this.ws?.on('pong', () => res(true)) || err("ws can't listen for pong")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private async pingpong() {
|
||||||
|
// if the websocket is connected
|
||||||
|
if (this.ws?.readyState == 1) {
|
||||||
|
// send a ping
|
||||||
|
this.ws.ping()
|
||||||
|
// wait for either a pong or a timeout
|
||||||
|
const result = await Promise.any([
|
||||||
|
this.receivePong(),
|
||||||
|
new Promise(res => setTimeout(() => res(false), 10000)) // TODO: opts.pingTimeout
|
||||||
|
])
|
||||||
|
if (result) {
|
||||||
|
// schedule another pingpong
|
||||||
|
setTimeout(() => this.pingpong(), 10000) // TODO: opts.pingFrequency
|
||||||
|
} else {
|
||||||
|
this.ws && this.ws.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async runQueue() {
|
private async runQueue() {
|
||||||
this.queueRunning = true
|
this.queueRunning = true
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|||||||
Reference in New Issue
Block a user