-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
46 lines (40 loc) · 978 Bytes
/
example.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
'use strict'
const { inspect } = require('util')
const dwebswarm = require('./')
const crypto = require('crypto')
const swarm = dwebswarm({
announceLocalAddress: true
})
if (!process.argv[2]) { throw Error('node example.js <topic-key>') }
const key = crypto.createHash('sha256')
.update(process.argv[2])
.digest()
swarm.connectivity((err, capabilities) => {
console.log('network capabilities', capabilities, err || '')
})
swarm.join(key, {
announce: true,
lookup: true
}, function () {
console.log('fully joined...')
})
swarm.on('connection', function (socket, info) {
const {
priority,
status,
retries,
peer,
client
} = info
console.log('new connection!', `
priority: ${priority}
status: ${status}
retries: ${retries}
client: ${client}
peer: ${!peer ? peer : `
${inspect(peer, { indentationLvl: 4 }).slice(2, -2)}
`}
`)
if (client) process.stdin.pipe(socket)
else socket.pipe(process.stdout)
})