mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 16:48:50 +00:00
improve reconnection attempts to relays and print less errors.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "0.4.2",
|
"version": "0.4.3",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
39
relay.js
39
relay.js
@@ -13,13 +13,14 @@ export function normalizeRelayURL(url) {
|
|||||||
export function relayConnect(url, onNotice) {
|
export function relayConnect(url, onNotice) {
|
||||||
url = normalizeRelayURL(url)
|
url = normalizeRelayURL(url)
|
||||||
|
|
||||||
var ws, resolveOpen, untilOpen, rejectOpen
|
var ws, resolveOpen, untilOpen
|
||||||
|
var openSubs = {}
|
||||||
let attemptNumber = 1
|
let attemptNumber = 1
|
||||||
|
let nextAttemptSeconds = 1
|
||||||
|
|
||||||
function resetOpenState() {
|
function resetOpenState() {
|
||||||
untilOpen = new Promise((resolve, reject) => {
|
untilOpen = new Promise(resolve => {
|
||||||
resolveOpen = resolve
|
resolveOpen = resolve
|
||||||
rejectOpen = reject
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,22 +32,29 @@ export function relayConnect(url, onNotice) {
|
|||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
console.log('connected to', url)
|
console.log('connected to', url)
|
||||||
resolveOpen()
|
resolveOpen()
|
||||||
|
|
||||||
|
// restablish old subscriptions
|
||||||
|
for (let channel in openSubs) {
|
||||||
|
let filters = openSubs[channel]
|
||||||
|
let cb = channels[channel]
|
||||||
|
sub({cb, filter: filters}, channel)
|
||||||
}
|
}
|
||||||
ws.onerror = err => {
|
}
|
||||||
console.log('error connecting to relay', url, err)
|
ws.onerror = () => {
|
||||||
rejectOpen()
|
console.log('error connecting to relay', url)
|
||||||
}
|
}
|
||||||
ws.onclose = () => {
|
ws.onclose = () => {
|
||||||
resetOpenState()
|
resetOpenState()
|
||||||
attemptNumber++
|
attemptNumber++
|
||||||
|
nextAttemptSeconds += attemptNumber
|
||||||
console.log(
|
console.log(
|
||||||
`relay ${url} connection closed. reconnecting in ${attemptNumber} seconds.`
|
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
|
||||||
)
|
)
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
connect()
|
connect()
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
}, attemptNumber * 1000)
|
}, nextAttemptSeconds * 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.onmessage = async e => {
|
ws.onmessage = async e => {
|
||||||
@@ -94,16 +102,8 @@ export function relayConnect(url, onNotice) {
|
|||||||
async function trySend(params) {
|
async function trySend(params) {
|
||||||
let msg = JSON.stringify(params)
|
let msg = JSON.stringify(params)
|
||||||
|
|
||||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
||||||
ws.send(msg)
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
await untilOpen
|
await untilOpen
|
||||||
ws.send(msg)
|
ws.send(msg)
|
||||||
} catch (e) {
|
|
||||||
console.log(`waiting to connect to ${url}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sub = ({cb, filter}, channel = Math.random().toString().slice(2)) => {
|
const sub = ({cb, filter}, channel = Math.random().toString().slice(2)) => {
|
||||||
@@ -116,10 +116,15 @@ export function relayConnect(url, onNotice) {
|
|||||||
|
|
||||||
trySend(['REQ', channel, ...filters])
|
trySend(['REQ', channel, ...filters])
|
||||||
channels[channel] = cb
|
channels[channel] = cb
|
||||||
|
openSubs[channel] = filters
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sub: ({cb = cb, filter = filter}) => sub({cb, filter}, channel),
|
sub: ({cb = cb, filter = filter}) => sub({cb, filter}, channel),
|
||||||
unsub: () => trySend(['CLOSE', channel])
|
unsub: () => {
|
||||||
|
delete openSubs[channel]
|
||||||
|
delete channels[channel]
|
||||||
|
trySend(['CLOSE', channel])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user