-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: examples/discovery-mechanisms (#498)
* refactor: examples-discovery-mechanisms * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * chore: suggestion interval Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * chore: add peer connected event Co-authored-by: Jacob Heun <jacobheun@gmail.com>
- Loading branch information
1 parent
a1717da
commit f182f5b
Showing
3 changed files
with
122 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,45 @@ | ||
/* eslint-disable no-console */ | ||
'use strict' | ||
|
||
const libp2p = require('../../') | ||
const Libp2p = require('../../') | ||
const TCP = require('libp2p-tcp') | ||
const Mplex = require('libp2p-mplex') | ||
const SECIO = require('libp2p-secio') | ||
const PeerInfo = require('peer-info') | ||
const MulticastDNS = require('libp2p-mdns') | ||
const waterfall = require('async/waterfall') | ||
const parallel = require('async/parallel') | ||
const defaultsDeep = require('@nodeutils/defaults-deep') | ||
|
||
class MyBundle extends libp2p { | ||
constructor (_options) { | ||
const defaults = { | ||
modules: { | ||
transport: [ TCP ], | ||
streamMuxer: [ Mplex ], | ||
connEncryption: [ SECIO ], | ||
peerDiscovery: [ MulticastDNS ] | ||
}, | ||
config: { | ||
peerDiscovery: { | ||
mdns: { | ||
interval: 20e3, | ||
enabled: true | ||
} | ||
const createNode = async () => { | ||
const node = await Libp2p.create({ | ||
modules: { | ||
transport: [TCP], | ||
streamMuxer: [Mplex], | ||
connEncryption: [SECIO], | ||
peerDiscovery: [MulticastDNS] | ||
}, | ||
config: { | ||
peerDiscovery: { | ||
mdns: { | ||
interval: 20e3, | ||
enabled: true | ||
} | ||
} | ||
} | ||
}) | ||
node.peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') | ||
|
||
super(defaultsDeep(_options, defaults)) | ||
} | ||
return node | ||
} | ||
|
||
function createNode (callback) { | ||
let node | ||
|
||
waterfall([ | ||
(cb) => PeerInfo.create(cb), | ||
(peerInfo, cb) => { | ||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') | ||
node = new MyBundle({ | ||
peerInfo | ||
}) | ||
node.start(cb) | ||
} | ||
], (err) => callback(err, node)) | ||
} | ||
|
||
parallel([ | ||
(cb) => createNode(cb), | ||
(cb) => createNode(cb) | ||
], (err, nodes) => { | ||
if (err) { throw err } | ||
|
||
const node1 = nodes[0] | ||
const node2 = nodes[1] | ||
;(async () => { | ||
const [node1, node2] = await Promise.all([ | ||
createNode(), | ||
createNode() | ||
]) | ||
|
||
node1.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String())) | ||
node2.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String())) | ||
}) | ||
|
||
await Promise.all([ | ||
node1.start(), | ||
node2.start() | ||
]) | ||
})(); |
Oops, something went wrong.