This commit is contained in:
fiatjaf 2025-07-23 16:22:25 -03:00
parent b2f3a01439
commit 4b6cc19b9c
4 changed files with 11 additions and 13 deletions

View File

@ -110,7 +110,8 @@ export class AbstractRelay {
this.ws.onopen = () => {
clearTimeout(this.connectionTimeoutHandle)
this._connected = true
if (this.ws && this.ws.ping) { // && this.pingHeartBeat
if (this.ws && this.ws.ping) {
// && this.pingHeartBeat
this.pingpong()
}
resolve()
@ -146,31 +147,28 @@ export class AbstractRelay {
private async receivePong() {
return new Promise((res, err) => {
(this.ws && this.ws.on && this.ws.on('pong', () => res(true))) || err("ws can't listen for pong")
;(this.ws && this.ws.on && this.ws.on('pong', () => res(true))) || err("ws can't listen for pong")
})
}
// nodejs requires this magic here to ensure connections are closed when internet goes off and stuff
// in browsers it's done automatically. see https://github.com/nbd-wtf/nostr-tools/issues/491
private async pingpong() {
console.error('pingpong')
// if the websocket is connected
if (this.ws?.readyState == 1) {
console.error('pingpong readyState==1')
// send a ping
console.error('pingpong ping()');
(this.ws && this.ws.ping) && this.ws.ping()
this.ws && this.ws.ping && this.ws.ping()
// wait for either a pong or a timeout
console.error('pingpong wait for pong or timeout')
const result = await Promise.any([
this.receivePong(),
new Promise(res => setTimeout(() => res(false), 10000)) // TODO: opts.pingTimeout
new Promise(res => setTimeout(() => res(false), 10000)), // TODO: opts.pingTimeout
])
console.error('pingpong result', result)
if (result) {
console.error('pingpong scheduling pingpong')
// schedule another pingpong
setTimeout(() => this.pingpong(), 10000) // TODO: opts.pingFrequency
} else {
console.error('pingpong closing socket')
// pingpong closing socket
this.ws && this.ws.close()
}
}

View File

@ -1,6 +1,6 @@
{
"name": "@nostr/tools",
"version": "2.15.1",
"version": "2.15.2",
"exports": {
".": "./index.ts",
"./core": "./core.ts",

View File

@ -2,7 +2,7 @@ import { AbstractSimplePool } from './abstract-pool.ts'
import { Subscription } from './abstract-relay.ts'
import type { Event, EventTemplate } from './core.ts'
import { fetchRelayInformation, RelayInformation } from './nip11.ts'
import { AddressPointer, decode, NostrTypeGuard } from './nip19.ts'
import { decode, NostrTypeGuard } from './nip19.ts'
import { normalizeURL } from './utils.ts'
/**

View File

@ -1,7 +1,7 @@
{
"type": "module",
"name": "nostr-tools",
"version": "2.15.0",
"version": "2.15.2",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",