finish websocket protocol.

This commit is contained in:
fiatjaf 2021-01-04 14:27:53 -03:00
parent b077271d46
commit 5921ad1080
3 changed files with 29 additions and 6 deletions

View File

@ -2,6 +2,8 @@ import shajs from 'sha.js'
import BigInteger from 'bigi'
import schnorr from 'bip-schnorr'
import {makeRandom32} from './utils'
export function serializeEvent(evt) {
return JSON.stringify([
0,

View File

@ -1,6 +1,6 @@
{
"name": "nostr-tools",
"version": "0.0.1",
"version": "0.0.2",
"dependencies": {
"assert": "^2.0.0",
"bigi": "^1.4.2",

View File

@ -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()
}