|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const IpfsHttpClient = require('ipfs-http-client') |
| 4 | +const { sleep, Logger, onEnterPress, catchAndLog } = require('./util') |
| 5 | + |
| 6 | +async function main () { |
| 7 | + const apiUrlInput = document.getElementById('api-url') |
| 8 | + const nodeConnectBtn = document.getElementById('node-connect') |
| 9 | + |
| 10 | + const peerAddrInput = document.getElementById('peer-addr') |
| 11 | + const peerConnectBtn = document.getElementById('peer-connect') |
| 12 | + |
| 13 | + const topicInput = document.getElementById('topic') |
| 14 | + const subscribeBtn = document.getElementById('subscribe') |
| 15 | + |
| 16 | + const messageInput = document.getElementById('message') |
| 17 | + const sendBtn = document.getElementById('send') |
| 18 | + |
| 19 | + let log = Logger(document.getElementById('console')) |
| 20 | + let ipfs |
| 21 | + let topic |
| 22 | + let peerId |
| 23 | + |
| 24 | + async function reset () { |
| 25 | + if (ipfs && topic) { |
| 26 | + log(`Unsubscribing from topic ${topic}`) |
| 27 | + await ipfs.pubsub.unsubscribe(topic) |
| 28 | + } |
| 29 | + log = Logger(document.getElementById('console')) |
| 30 | + topicInput.value = '' |
| 31 | + topic = null |
| 32 | + peerId = null |
| 33 | + ipfs = null |
| 34 | + } |
| 35 | + |
| 36 | + async function nodeConnect (url) { |
| 37 | + await reset() |
| 38 | + log(`Connecting to ${url}`) |
| 39 | + ipfs = IpfsHttpClient(url) |
| 40 | + const { id, agentVersion } = await ipfs.id() |
| 41 | + peerId = id |
| 42 | + log(`<span class="green">Success!</span>`) |
| 43 | + log(`Version ${agentVersion}`) |
| 44 | + log(`Peer ID ${id}`) |
| 45 | + } |
| 46 | + |
| 47 | + async function peerConnect (addr) { |
| 48 | + if (!addr) throw new Error('Missing peer multiaddr') |
| 49 | + if (!ipfs) throw new Error('Connect to a node first') |
| 50 | + log(`Connecting to peer ${addr}`) |
| 51 | + await ipfs.swarm.connect(addr) |
| 52 | + log(`<span class="green">Success!</span>`) |
| 53 | + log('Listing swarm peers...') |
| 54 | + await sleep() |
| 55 | + const peers = await ipfs.swarm.peers() |
| 56 | + peers.forEach(peer => { |
| 57 | + const fullAddr = `${peer.addr}/ipfs/${peer.peer.toB58String()}` |
| 58 | + log(`<span class="${addr.endsWith(peer.peer.toB58String()) ? 'teal' : ''}">${fullAddr}</span>`) |
| 59 | + }) |
| 60 | + log(`(${peers.length} peers total)`) |
| 61 | + } |
| 62 | + |
| 63 | + async function subscribe (nextTopic) { |
| 64 | + if (!nextTopic) throw new Error('Missing topic name') |
| 65 | + if (!ipfs) throw new Error('Connect to a node first') |
| 66 | + |
| 67 | + const lastTopic = topic |
| 68 | + |
| 69 | + if (topic) { |
| 70 | + topic = null |
| 71 | + log(`Unsubscribing from topic ${lastTopic}`) |
| 72 | + await ipfs.pubsub.unsubscribe(lastTopic) |
| 73 | + } |
| 74 | + |
| 75 | + log(`Subscribing to ${nextTopic}...`) |
| 76 | + |
| 77 | + await ipfs.pubsub.subscribe(nextTopic, msg => { |
| 78 | + const from = msg.from |
| 79 | + const seqno = msg.seqno.toString('hex') |
| 80 | + if (from === peerId) return log(`Ignoring message ${seqno} from self`) |
| 81 | + log(`Message ${seqno} from ${from}:`) |
| 82 | + try { |
| 83 | + log(JSON.stringify(msg.data.toString(), null, 2)) |
| 84 | + } catch (_) { |
| 85 | + log(msg.data.toString('hex')) |
| 86 | + } |
| 87 | + }, { |
| 88 | + onError: (err, fatal) => { |
| 89 | + if (fatal) { |
| 90 | + console.error(err) |
| 91 | + log(`<span class="red">${err.message}</span>`) |
| 92 | + topic = null |
| 93 | + log('Resubscribing in 5s...') |
| 94 | + setTimeout(catchAndLog(() => subscribe(nextTopic), log), 5000) |
| 95 | + } else { |
| 96 | + console.warn(err) |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + topic = nextTopic |
| 102 | + log(`<span class="green">Success!</span>`) |
| 103 | + } |
| 104 | + |
| 105 | + async function send (msg) { |
| 106 | + if (!msg) throw new Error('Missing message') |
| 107 | + if (!topic) throw new Error('Subscribe to a topic first') |
| 108 | + if (!ipfs) throw new Error('Connect to a node first') |
| 109 | + |
| 110 | + log(`Sending message to ${topic}...`) |
| 111 | + await ipfs.pubsub.publish(topic, msg) |
| 112 | + log(`<span class="green">Success!</span>`) |
| 113 | + } |
| 114 | + |
| 115 | + const onNodeConnectClick = catchAndLog(() => nodeConnect(apiUrlInput.value), log) |
| 116 | + apiUrlInput.addEventListener('keydown', onEnterPress(onNodeConnectClick)) |
| 117 | + nodeConnectBtn.addEventListener('click', onNodeConnectClick) |
| 118 | + |
| 119 | + const onPeerConnectClick = catchAndLog(() => peerConnect(peerAddrInput.value), log) |
| 120 | + peerAddrInput.addEventListener('keydown', onEnterPress(onPeerConnectClick)) |
| 121 | + peerConnectBtn.addEventListener('click', onPeerConnectClick) |
| 122 | + |
| 123 | + const onSubscribeClick = catchAndLog(() => subscribe(topicInput.value), log) |
| 124 | + topicInput.addEventListener('keydown', onEnterPress(onSubscribeClick)) |
| 125 | + subscribeBtn.addEventListener('click', onSubscribeClick) |
| 126 | + |
| 127 | + const onSendClick = catchAndLog(async () => { |
| 128 | + await send(messageInput.value) |
| 129 | + messageInput.value = '' |
| 130 | + }, log) |
| 131 | + messageInput.addEventListener('keydown', onEnterPress(onSendClick)) |
| 132 | + sendBtn.addEventListener('click', onSendClick) |
| 133 | +} |
| 134 | + |
| 135 | +main() |
0 commit comments