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')
|
2020-05-15 15:37:47 +02:00
|
|
|
const { NOISE } = require('libp2p-noise')
|
2017-07-08 22:10:47 +01:00
|
|
|
const MulticastDNS = require('libp2p-mdns')
|
|
|
|
|
2020-01-06 18:01:28 +01:00
|
|
|
const createNode = async () => {
|
|
|
|
const node = await Libp2p.create({
|
2020-05-15 15:37:47 +02:00
|
|
|
addresses: {
|
|
|
|
listen: ['/ip4/0.0.0.0/tcp/0']
|
|
|
|
},
|
2020-01-06 18:01:28 +01:00
|
|
|
modules: {
|
|
|
|
transport: [TCP],
|
|
|
|
streamMuxer: [Mplex],
|
2020-05-15 15:37:47 +02:00
|
|
|
connEncryption: [NOISE, SECIO],
|
2020-01-06 18:01:28 +01:00
|
|
|
peerDiscovery: [MulticastDNS]
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
peerDiscovery: {
|
2020-06-15 12:35:06 +02:00
|
|
|
mdns: {
|
2020-01-06 18:01:28 +01:00
|
|
|
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
|
|
|
})
|
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
|
|
|
|
2020-05-15 15:37:47 +02:00
|
|
|
node1.on('peer:discovery', (peerId) => console.log('Discovered:', peerId.toB58String()))
|
|
|
|
node2.on('peer:discovery', (peerId) => console.log('Discovered:', peerId.toB58String()))
|
2020-01-06 18:01:28 +01:00
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
node1.start(),
|
|
|
|
node2.start()
|
|
|
|
])
|
|
|
|
})();
|