2017-07-04 11:43:45 +01:00
|
|
|
'use strict'
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
|
|
|
const PeerId = require('peer-id')
|
|
|
|
const PeerInfo = require('peer-info')
|
2017-07-05 12:12:51 +01:00
|
|
|
const Node = require('./libp2p-bundle')
|
2017-07-04 11:43:45 +01:00
|
|
|
const pull = require('pull-stream')
|
|
|
|
const async = require('async')
|
|
|
|
const Pushable = require('pull-pushable')
|
|
|
|
const p = Pushable()
|
|
|
|
let idListener
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
(callback) => {
|
|
|
|
PeerId.createFromJSON(require('./peer-id-dialer'), (err, idDialer) => {
|
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
callback(null, idDialer)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
(callback) => {
|
|
|
|
PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
|
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
callback(null, idListener)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
], (err, ids) => {
|
|
|
|
if (err) throw err
|
|
|
|
const peerDialer = new PeerInfo(ids[0])
|
2017-07-11 01:39:57 -05:00
|
|
|
peerDialer.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
2018-06-28 10:06:25 +02:00
|
|
|
const nodeDialer = new Node({
|
|
|
|
peerInfo: peerDialer
|
|
|
|
})
|
2017-07-04 11:43:45 +01:00
|
|
|
|
|
|
|
const peerListener = new PeerInfo(ids[1])
|
|
|
|
idListener = ids[1]
|
2017-07-11 01:39:57 -05:00
|
|
|
peerListener.multiaddrs.add('/ip4/127.0.0.1/tcp/10333')
|
2017-07-04 11:43:45 +01:00
|
|
|
nodeDialer.start((err) => {
|
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Dialer ready, listening on:')
|
|
|
|
|
|
|
|
peerListener.multiaddrs.forEach((ma) => {
|
2019-02-06 04:59:42 +10:00
|
|
|
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
|
2017-07-04 11:43:45 +01:00
|
|
|
})
|
|
|
|
|
2018-02-07 08:28:36 +00:00
|
|
|
nodeDialer.dialProtocol(peerListener, '/chat/1.0.0', (err, conn) => {
|
2017-07-04 11:43:45 +01:00
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
console.log('nodeA dialed to nodeB on protocol: /chat/1.0.0')
|
|
|
|
console.log('Type a message and see what happens')
|
|
|
|
// Write operation. Data sent as a buffer
|
|
|
|
pull(
|
|
|
|
p,
|
|
|
|
conn
|
|
|
|
)
|
|
|
|
// Sink, data converted from buffer to utf8 string
|
|
|
|
pull(
|
|
|
|
conn,
|
|
|
|
pull.map((data) => {
|
|
|
|
return data.toString('utf8').replace('\n', '')
|
|
|
|
}),
|
|
|
|
pull.drain(console.log)
|
|
|
|
)
|
|
|
|
|
|
|
|
process.stdin.setEncoding('utf8')
|
|
|
|
process.openStdin().on('data', (chunk) => {
|
|
|
|
var data = chunk.toString()
|
|
|
|
p.push(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|