From 5921ad1080d46c7d4a9b32dee2ad9e3fe04ff5eb Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 4 Jan 2021 14:27:53 -0300 Subject: [PATCH] finish websocket protocol. --- event.js | 2 ++ package.json | 2 +- relay.js | 31 ++++++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/event.js b/event.js index c866703..f6715e2 100644 --- a/event.js +++ b/event.js @@ -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, diff --git a/package.json b/package.json index 3eeef7d..1bf0c4b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nostr-tools", - "version": "0.0.1", + "version": "0.0.2", "dependencies": { "assert": "^2.0.0", "bigi": "^1.4.2", diff --git a/relay.js b/relay.js index 5dd20fe..2f574b5 100644 --- a/relay.js +++ b/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() }