-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
128 lines (115 loc) · 3.13 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { useEffect, useState } from 'react'
import ReactDOM from 'react-dom'
import { html } from 'htm/react'
import Libp2p from 'libp2p'
import WebRTCStar from 'libp2p-webrtc-star'
import Secio from 'libp2p-secio'
import Mplex from 'libp2p-mplex'
function App (props) {
const [logs] = useState([])
const [_, forceUpdate] = useState(0)
const [peerId, setPeerId] = useState(null)
const [peers] = useState({})
const [libp2p, setLibp2p] = useState(null)
function log (txt) {
logs.push(txt)
forceUpdate(Date.now())
}
function addPeer (peerInfo) {
const peerId = peerInfo.id.toB58String()
if (!peers[peerId]) {
peers[peerId] = {
peerInfo: peerInfo,
connected: false
}
forceUpdate(Date.now())
}
return peers[peerId]
}
function connectPeer (peerInfo) {
const peer = addPeer(peerInfo)
peer.connected = true
}
function disconnectPeer (peerInfo) {
const peer = addPeer(peerInfo)
peer.connected = false
}
useEffect(() => {
async function run () {
const libp2p = await Libp2p.create({
modules: {
transport: [WebRTCStar],
connEncryption: [Secio],
streamMuxer: [Mplex]
},
config: {
peerDiscovery: {
autoDial: false
}
}
})
setPeerId(libp2p.peerInfo.id.toB58String())
const webrtcAddr = '/ip4/0.0.0.0/tcp/9090/wss/p2p-webrtc-star'
libp2p.peerInfo.multiaddrs.add(webrtcAddr)
libp2p.on('peer:discovery', peerInfo => {
log(`Found peer ${peerInfo.id.toB58String()}`)
addPeer(peerInfo)
})
// Listen for new connections to peers
libp2p.on('peer:connect', peerInfo => {
log(`Connected to ${peerInfo.id.toB58String()}`)
connectPeer(peerInfo)
})
// Listen for peers disconnecting
libp2p.on('peer:disconnect', peerInfo => {
log(`Disconnected from ${peerInfo.id.toB58String()}`)
disconnectPeer(peerInfo)
})
await libp2p.start()
setLibp2p(libp2p)
}
run()
}, [])
return html`
<div>
PeerId: ${peerId}
<h3>Peers</h3>
<ul>
${Object.keys(peers).map(peerId => {
const peer = peers[peerId]
return html`
<li key=${peerId}>
${peerId.slice(-3)} ${peer.connected ? 'Connected' : 'Disconnected'}
${!peer.connected &&
html`
<button onClick=${connect}>Connect</button>
`}
</li>
`
function connect () {
console.log('Jim connect', peerId, peer)
async function dial () {
log(`Dialing ${peerId}`)
const conn = await libp2p.dial(peer.peerInfo)
log(`Dialed ${peerId}`)
console.log('Jim dialed', peerId, conn)
}
dial()
}
})}
</ul>
<h3>Logs</h3>
${logs.map((line, i) => {
return html`
<div key=${i}>${line}</div>
`
})}
</div>
`
}
const appEl = document.getElementById('app')
ReactDOM.createRoot(appEl).render(
html`
<${App} />
`
)