improve reconnection attempts to relays and print less errors.

This commit is contained in:
fiatjaf
2021-05-15 18:12:03 -03:00
parent 2d2d9863d1
commit 28443710e3
2 changed files with 25 additions and 20 deletions

View File

@@ -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": {

View File

@@ -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 => { ws.onerror = () => {
console.log('error connecting to relay', url, err) console.log('error connecting to relay', url)
rejectOpen()
} }
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) { await untilOpen
ws.send(msg) ws.send(msg)
} else {
try {
await untilOpen
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])
}
} }
} }