Skip to content

Commit

Permalink
fix: async await examples/echo
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Nov 26, 2019
1 parent c4b64de commit 7bd8e29
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 66 deletions.
73 changes: 38 additions & 35 deletions examples/echo/src/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,54 @@
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const Node = require('./libp2p-bundle')
const pull = require('pull-stream')
const async = require('async')
const pipe = require('it-pipe')

async.parallel([
(cb) => PeerId.createFromJSON(require('./id-d'), cb),
(cb) => PeerId.createFromJSON(require('./id-l'), cb)
], (err, ids) => {
if (err) { throw err }
async function run() {
const [dialerId, listenerId] = await Promise.all([
PeerId.createFromJSON(require('./id-d')),
PeerId.createFromJSON(require('./id-l'))
])

// Dialer
const dialerId = ids[0]
const dialerPeerInfo = new PeerInfo(dialerId)
dialerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
const dialerNode = new Node({
peerInfo: dialerPeerInfo
})

// Peer to Dial
const listenerPeerInfo = new PeerInfo(ids[1])
const listenerId = ids[1]
// Peer to Dial (the listener)
const listenerPeerInfo = new PeerInfo(listenerId)
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/p2p/' +
listenerId.toB58String()
listenerPeerInfo.multiaddrs.add(listenerMultiaddr)

dialerNode.start((err) => {
if (err) { throw err }

console.log('Dialer ready, listening on:')
dialerPeerInfo.multiaddrs.forEach((ma) => console.log(ma.toString() +
'/p2p/' + dialerId.toB58String()))

console.log('Dialing to peer:', listenerMultiaddr.toString())
dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0', (err, conn) => {
if (err) { throw err }

console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')

pull(
pull.values(['hey']),
conn,
pull.collect((err, data) => {
if (err) { throw err }
console.log('received echo:', data.toString())
})
)
})
})
})
// Start the dialer libp2p node
await dialerNode.start()

console.log('Dialer ready, listening on:')
dialerPeerInfo.multiaddrs.forEach((ma) => console.log(ma.toString() +
'/p2p/' + dialerId.toB58String()))

// Dial the listener node
console.log('Dialing to peer:', listenerMultiaddr.toString())
const { stream } = await dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0')

console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')

pipe(
// Source data
['hey'],
// Write to the stream, and pass its output to the next function
stream,
// Sink function
async function (source) {
// For each chunk of data
for await (const data of source) {
// Output the data
console.log('received echo:', data.toString())
}
}
)
}

run()
57 changes: 26 additions & 31 deletions examples/echo/src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,34 @@
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const Node = require('./libp2p-bundle')
const pull = require('pull-stream')
const series = require('async/series')

let listenerId
let listenerNode

series([
(cb) => {
PeerId.createFromJSON(require('./id-l'), (err, id) => {
if (err) { return cb(err) }
listenerId = id
cb()
})
},
(cb) => {
const listenerPeerInfo = new PeerInfo(listenerId)
listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
listenerNode = new Node({
peerInfo: listenerPeerInfo
})

listenerNode.on('peer:connect', (peerInfo) => {
console.log('received dial to me from:', peerInfo.id.toB58String())
})

listenerNode.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
listenerNode.start(cb)
}
], (err) => {
if (err) { throw err }
const pipe = require('it-pipe')

async function run() {
const listenerId = await PeerId.createFromJSON(require('./id-l'))

// Listener libp2p node
const listenerPeerInfo = new PeerInfo(listenerId)
listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
const listenerNode = new Node({
peerInfo: listenerPeerInfo
})

// Log a message when we receive a connection
listenerNode.on('peer:connect', (peerInfo) => {
console.log('received dial to me from:', peerInfo.id.toB58String())
})

// Handle incoming connections for the protocol by piping from the stream
// back to itself (an echo)
await listenerNode.handle('/echo/1.0.0', ({ stream }) => pipe(stream.source, stream.sink))

// Start listening
await listenerNode.start()

console.log('Listener ready, listening on:')
listenerNode.peerInfo.multiaddrs.forEach((ma) => {
console.log(ma.toString() + '/p2p/' + listenerId.toB58String())
})
})
}

run()

0 comments on commit 7bd8e29

Please sign in to comment.