mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
06e867b675 | ||
|
|
22e895c7c2 | ||
|
|
02cacd4446 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "0.7.2",
|
"version": "0.9.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
8
pool.js
8
pool.js
@@ -29,19 +29,23 @@ export function relayPool(globalPrivateKey) {
|
|||||||
const activeFilters = filter
|
const activeFilters = filter
|
||||||
|
|
||||||
activeSubscriptions[id] = {
|
activeSubscriptions[id] = {
|
||||||
sub: ({cb = activeCallback, filter = activeFilters}) =>
|
sub: ({cb = activeCallback, filter = activeFilters}) => {
|
||||||
Object.entries(subControllers).map(([relayURL, sub]) => [
|
Object.entries(subControllers).map(([relayURL, sub]) => [
|
||||||
relayURL,
|
relayURL,
|
||||||
sub.sub({cb, filter}, id)
|
sub.sub({cb, filter}, id)
|
||||||
]),
|
])
|
||||||
|
return activeSubscriptions[id]
|
||||||
|
},
|
||||||
addRelay: relay => {
|
addRelay: relay => {
|
||||||
subControllers[relay.url] = relay.sub({cb, filter})
|
subControllers[relay.url] = relay.sub({cb, filter})
|
||||||
|
return activeSubscriptions[id]
|
||||||
},
|
},
|
||||||
removeRelay: relayURL => {
|
removeRelay: relayURL => {
|
||||||
if (relayURL in subControllers) {
|
if (relayURL in subControllers) {
|
||||||
subControllers[relayURL].unsub()
|
subControllers[relayURL].unsub()
|
||||||
if (Object.keys(subControllers).length === 0) unsub()
|
if (Object.keys(subControllers).length === 0) unsub()
|
||||||
}
|
}
|
||||||
|
return activeSubscriptions[id]
|
||||||
},
|
},
|
||||||
unsub: () => {
|
unsub: () => {
|
||||||
Object.values(subControllers).forEach(sub => sub.unsub())
|
Object.values(subControllers).forEach(sub => sub.unsub())
|
||||||
|
|||||||
20
relay.js
20
relay.js
@@ -13,7 +13,7 @@ export function normalizeRelayURL(url) {
|
|||||||
export function relayConnect(url, onNotice) {
|
export function relayConnect(url, onNotice) {
|
||||||
url = normalizeRelayURL(url)
|
url = normalizeRelayURL(url)
|
||||||
|
|
||||||
var ws, resolveOpen, untilOpen
|
var ws, resolveOpen, untilOpen, wasClosed
|
||||||
var openSubs = {}
|
var openSubs = {}
|
||||||
let attemptNumber = 1
|
let attemptNumber = 1
|
||||||
let nextAttemptSeconds = 1
|
let nextAttemptSeconds = 1
|
||||||
@@ -34,10 +34,13 @@ export function relayConnect(url, onNotice) {
|
|||||||
resolveOpen()
|
resolveOpen()
|
||||||
|
|
||||||
// restablish old subscriptions
|
// restablish old subscriptions
|
||||||
for (let channel in openSubs) {
|
if (wasClosed) {
|
||||||
let filters = openSubs[channel]
|
wasClosed = false
|
||||||
let cb = channels[channel]
|
for (let channel in openSubs) {
|
||||||
sub({cb, filter: filters}, channel)
|
let filters = openSubs[channel]
|
||||||
|
let cb = channels[channel]
|
||||||
|
sub({cb, filter: filters}, channel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ws.onerror = () => {
|
ws.onerror = () => {
|
||||||
@@ -46,7 +49,10 @@ export function relayConnect(url, onNotice) {
|
|||||||
ws.onclose = () => {
|
ws.onclose = () => {
|
||||||
resetOpenState()
|
resetOpenState()
|
||||||
attemptNumber++
|
attemptNumber++
|
||||||
nextAttemptSeconds += attemptNumber
|
nextAttemptSeconds += attemptNumber ** 3
|
||||||
|
if (nextAttemptSeconds > 14400) {
|
||||||
|
nextAttemptSeconds = 14400 // 4 hours
|
||||||
|
}
|
||||||
console.log(
|
console.log(
|
||||||
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
|
`relay ${url} connection closed. reconnecting in ${nextAttemptSeconds} seconds.`
|
||||||
)
|
)
|
||||||
@@ -55,6 +61,8 @@ export function relayConnect(url, onNotice) {
|
|||||||
connect()
|
connect()
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
}, nextAttemptSeconds * 1000)
|
}, nextAttemptSeconds * 1000)
|
||||||
|
|
||||||
|
wasClosed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.onmessage = async e => {
|
ws.onmessage = async e => {
|
||||||
|
|||||||
Reference in New Issue
Block a user