mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
finish websocket protocol.
This commit is contained in:
31
relay.js
31
relay.js
@@ -11,15 +11,36 @@ export function relayConnect(url, onEventCallback) {
|
||||
ws.onerror = err => console.log('error connecting', url, err)
|
||||
|
||||
ws.onmessage = e => {
|
||||
let event = JSON.parse(e.data)
|
||||
event.context
|
||||
let data = JSON.parse(e.data)
|
||||
if (data.length > 1) {
|
||||
if (data[0] === 'notice') {
|
||||
console.log('message from relay ' + url + ' :' + data[1])
|
||||
} else if (typeof data[0] === 'object') {
|
||||
onEventCallback(data[0], data[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
url,
|
||||
subscribe() {},
|
||||
request() {},
|
||||
publish() {},
|
||||
subKey(key) {
|
||||
ws.send('sub-key:' + key)
|
||||
},
|
||||
unsubKey(key) {
|
||||
ws.send('unsub-key:' + key)
|
||||
},
|
||||
homeFeed(params = {}) {
|
||||
ws.send('req-feed:' + JSON.stringify(params))
|
||||
},
|
||||
reqEvent(params) {
|
||||
ws.send('req-key:' + JSON.stringify(params))
|
||||
},
|
||||
reqKey(params) {
|
||||
ws.send('req-key:' + JSON.stringify(params))
|
||||
},
|
||||
sendEvent(event) {
|
||||
ws.send(JSON.stringify(event))
|
||||
},
|
||||
close() {
|
||||
ws.close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user