2017-07-07 20:27:37 +01:00
|
|
|
'use strict'
|
|
|
|
|
2021-01-21 09:27:27 +01:00
|
|
|
const Libp2p = require('../..')
|
2017-07-07 20:27:37 +01:00
|
|
|
const TCP = require('libp2p-tcp')
|
2019-12-18 02:35:49 +00:00
|
|
|
const Mplex = require('libp2p-mplex')
|
2020-05-15 15:37:47 +02:00
|
|
|
const { NOISE } = require('libp2p-noise')
|
2018-06-28 10:06:25 +02:00
|
|
|
|
2019-12-18 02:35:49 +00:00
|
|
|
const pipe = require('it-pipe')
|
2017-07-07 20:27:37 +01:00
|
|
|
|
2019-12-18 02:35:49 +00: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']
|
|
|
|
},
|
2019-12-18 02:35:49 +00:00
|
|
|
modules: {
|
|
|
|
transport: [TCP],
|
|
|
|
streamMuxer: [Mplex],
|
2020-10-07 16:16:36 +02:00
|
|
|
connEncryption: [NOISE]
|
2017-07-07 20:27:37 +01:00
|
|
|
}
|
2019-12-18 02:35:49 +00:00
|
|
|
})
|
2017-07-07 20:27:37 +01:00
|
|
|
|
2019-12-18 02:35:49 +00:00
|
|
|
await node.start()
|
2017-07-07 20:27:37 +01:00
|
|
|
|
2019-12-18 02:35:49 +00:00
|
|
|
return node
|
|
|
|
}
|
2017-07-07 20:27:37 +01:00
|
|
|
|
2019-12-18 02:35:49 +00:00
|
|
|
;(async () => {
|
|
|
|
const [node1, node2] = await Promise.all([
|
|
|
|
createNode(),
|
|
|
|
createNode()
|
|
|
|
])
|
|
|
|
|
2020-05-15 15:37:47 +02:00
|
|
|
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
|
|
|
|
2019-12-18 02:35:49 +00:00
|
|
|
node2.handle('/a-protocol', ({ stream }) => {
|
|
|
|
pipe(
|
|
|
|
stream,
|
|
|
|
async function (source) {
|
|
|
|
for await (const msg of source) {
|
|
|
|
console.log(msg.toString())
|
|
|
|
}
|
|
|
|
}
|
2017-07-07 20:27:37 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 15:37:47 +02:00
|
|
|
const { stream } = await node1.dialProtocol(node2.peerId, '/a-protocol')
|
2019-12-18 02:35:49 +00:00
|
|
|
|
|
|
|
await pipe(
|
|
|
|
['This information is sent out encrypted to the other peer'],
|
|
|
|
stream
|
|
|
|
)
|
|
|
|
})();
|