mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-12 08:41:22 +00:00
refactor: examples/protocol-and-stream-muxing (#502)
This commit is contained in:
@ -1,88 +1,69 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const parallel = require('async/parallel')
|
||||
const series = require('async/series')
|
||||
const pull = require('pull-stream')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ SPDY ]
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const createNode = async () => {
|
||||
const peerInfo = await PeerInfo.create()
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [MPLEX],
|
||||
connEncryption: [SECIO]
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(),
|
||||
createNode()
|
||||
])
|
||||
|
||||
node1.handle('/node-1', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
|
||||
node1.handle('/node-1', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
)
|
||||
})
|
||||
|
||||
node2.handle('/node-2', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
node2.handle('/node-2', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
series([
|
||||
(cb) => node1.dialProtocol(node2.peerInfo, '/node-2', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['from 1 to 2']), conn)
|
||||
cb()
|
||||
}),
|
||||
(cb) => node2.dialProtocol(node1.peerInfo, '/node-1', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
pull(pull.values(['from 2 to 1']), conn)
|
||||
cb()
|
||||
})
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
console.log('Addresses by which both peers are connected')
|
||||
node1.peerBook
|
||||
.getAllArray()
|
||||
.forEach((peer) => console.log('node 1 to node 2:', peer.isConnected().toString()))
|
||||
node2.peerBook
|
||||
.getAllArray()
|
||||
.forEach((peer) => console.log('node 2 to node 1:', peer.isConnected().toString()))
|
||||
})
|
||||
})
|
||||
const { stream: stream1 } = await node1.dialProtocol(node2.peerInfo, ['/node-2'])
|
||||
await pipe(
|
||||
['from 1 to 2'],
|
||||
stream1
|
||||
)
|
||||
|
||||
const { stream: stream2 } = await node2.dialProtocol(node1.peerInfo, ['/node-1'])
|
||||
await pipe(
|
||||
['from 2 to 1'],
|
||||
stream2
|
||||
)
|
||||
})();
|
||||
|
Reference in New Issue
Block a user