Compare commits

...

2 Commits

Author SHA1 Message Date
fiatjaf
06e867b675 stop sending repeated REQs. 2021-12-13 20:58:49 -03:00
fiatjaf
22e895c7c2 use exponential backoff for reconnections. 2021-12-12 11:39:56 -03:00
2 changed files with 15 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "0.8.0",
"version": "0.9.0",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",

View File

@@ -13,7 +13,7 @@ export function normalizeRelayURL(url) {
export function relayConnect(url, onNotice) {
url = normalizeRelayURL(url)
var ws, resolveOpen, untilOpen
var ws, resolveOpen, untilOpen, wasClosed
var openSubs = {}
let attemptNumber = 1
let nextAttemptSeconds = 1
@@ -34,10 +34,13 @@ export function relayConnect(url, onNotice) {
resolveOpen()
// restablish old subscriptions
for (let channel in openSubs) {
let filters = openSubs[channel]
let cb = channels[channel]
sub({cb, filter: filters}, channel)
if (wasClosed) {
wasClosed = false
for (let channel in openSubs) {
let filters = openSubs[channel]
let cb = channels[channel]
sub({cb, filter: filters}, channel)
}
}
}
ws.onerror = () => {
@@ -46,7 +49,10 @@ export function relayConnect(url, onNotice) {
ws.onclose = () => {
resetOpenState()
attemptNumber++
nextAttemptSeconds += attemptNumber
nextAttemptSeconds += attemptNumber ** 3
if (nextAttemptSeconds > 14400) {
nextAttemptSeconds = 14400 // 4 hours
}
console.log(
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
)
@@ -55,6 +61,8 @@ export function relayConnect(url, onNotice) {
connect()
} catch (err) {}
}, nextAttemptSeconds * 1000)
wasClosed = true
}
ws.onmessage = async e => {