-
Notifications
You must be signed in to change notification settings - Fork 473
/
Copy pathlistener.js
41 lines (32 loc) · 1.05 KB
/
listener.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
'use strict'
/* eslint-disable no-console */
/*
* Listener Node
*/
const PeerId = require('peer-id')
const createLibp2p = require('./libp2p-bundle')
const pipe = require('it-pipe')
async function run() {
const listenerId = await PeerId.createFromJSON(require('./id-l'))
// Listener libp2p node
const listenerNode = await createLibp2p({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/10333']
},
peerId: listenerId
})
// Log a message when we receive a connection
listenerNode.connectionManager.on('peer:connect', (connection) => {
console.log('received dial to me from:', connection.remotePeer.toB58String())
})
// Handle incoming connections for the protocol by piping from the stream
// back to itself (an echo)
await listenerNode.handle('/echo/1.0.0', ({ stream }) => pipe(stream.source, stream.sink))
// Start listening
await listenerNode.start()
console.log('Listener ready, listening on:')
listenerNode.multiaddrs.forEach((ma) => {
console.log(ma.toString() + '/p2p/' + listenerId.toB58String())
})
}
run()