2018-10-19 17:37:34 +02:00
|
|
|
/* eslint-disable no-console */
|
2017-07-08 22:10:47 +01:00
|
|
|
'use strict'
|
|
|
|
|
2020-01-06 18:01:28 +01:00
|
|
|
const Libp2p = require('../../')
|
2017-07-08 22:10:47 +01:00
|
|
|
const TCP = require('libp2p-tcp')
|
2018-02-19 09:46:11 +00:00
|
|
|
const Mplex = require('libp2p-mplex')
|
2017-07-08 22:10:47 +01:00
|
|
|
const SECIO = require('libp2p-secio')
|
|
|
|
const MulticastDNS = require('libp2p-mdns')
|
|
|
|
|
2020-01-06 18:01:28 +01:00
|
|
|
const createNode = async () => {
|
|
|
|
const node = await Libp2p.create({
|
|
|
|
modules: {
|
|
|
|
transport: [TCP],
|
|
|
|
streamMuxer: [Mplex],
|
|
|
|
connEncryption: [SECIO],
|
|
|
|
peerDiscovery: [MulticastDNS]
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
peerDiscovery: {
|
|
|
|
mdns: {
|
|
|
|
interval: 20e3,
|
|
|
|
enabled: true
|
2018-06-28 10:06:25 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-08 22:10:47 +01:00
|
|
|
}
|
2020-01-06 18:01:28 +01:00
|
|
|
})
|
|
|
|
node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
2018-06-28 10:06:25 +02:00
|
|
|
|
2020-01-06 18:01:28 +01:00
|
|
|
return node
|
2017-07-08 22:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-06 18:01:28 +01:00
|
|
|
;(async () => {
|
|
|
|
const [node1, node2] = await Promise.all([
|
|
|
|
createNode(),
|
|
|
|
createNode()
|
|
|
|
])
|
2017-07-08 22:10:47 +01:00
|
|
|
|
|
|
|
node1.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
|
|
|
node2.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
|
2020-01-06 18:01:28 +01:00
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
node1.start(),
|
|
|
|
node2.start()
|
|
|
|
])
|
|
|
|
})();
|