implement some pong-reply logic.

This commit is contained in:
fiatjaf
2021-01-10 19:25:40 -03:00
parent b3685990c0
commit f4fd261084
2 changed files with 18 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "0.1.2",
"version": "0.1.3",
"description": "Tools for making a Nostr client.",
"main": "index.js",
"repository": {

View File

@@ -32,12 +32,26 @@ export function relayConnect(url, onEvent, onNotice) {
ws.onclose = () => console.log('relay connection closed', url)
ws.onmessage = async e => {
let data = JSON.parse(e.data)
var data
try {
data = JSON.parse(e.data)
} catch (err) {
data = e.data
}
if (data.length > 1) {
if (data === 'PING') {
ws.send('PONG')
return
}
if (data[0] === 'notice') {
console.log('message from relay ' + url + ': ' + data[1])
onNotice(data[1])
} else if (typeof data[0] === 'object') {
return
}
if (typeof data[0] === 'object') {
let event = data[0]
let context = data[1]
@@ -50,6 +64,7 @@ export function relayConnect(url, onEvent, onNotice) {
context
)
}
return
}
}
}