Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. 'use strict'
  2. /* eslint-disable no-console */
  3.  
  4. const PeerId = require('peer-id')
  5. const PeerInfo = require('peer-info')
  6. const Node = require('./libp2p_bundle')
  7. const pull = require('pull-stream')
  8. const async = require('async')
  9. const chalk = require('chalk');
  10. const emoji = require('node-emoji')
  11. const Pushable = require('pull-pushable')
  12. const p = Pushable()
  13. let moonPeerId
  14.  
  15. async.parallel([
  16. (callback) => {
  17. PeerId.createFromJSON(require('./ids/earthId'), (err, earthPeerId) => {
  18. if (err) {
  19. throw err
  20. }
  21. callback(null, earthPeerId)
  22. })
  23. },
  24. (callback) => {
  25. PeerId.createFromJSON(require('./ids/moonId'), (err, moonPeerId) => {
  26. if (err) {
  27. throw err
  28. }
  29. callback(null, moonPeerId)
  30. })
  31. }
  32. ], (err, ids) => {
  33. if (err) throw err
  34. const earthPeerInfo = new PeerInfo(ids[0])
  35. earthPeerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/0')
  36. const nodeDialer = new Node({ peerInfo: earthPeerInfo })
  37.  
  38. const moonPeerInfo = new PeerInfo(ids[1])
  39. moonPeerId = ids[1]
  40. moonPeerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/10333')
  41. nodeDialer.start((err) => {
  42. if (err) {
  43. throw err
  44. }
  45.  
  46. console.log(emoji.get('large_blue_circle'), chalk.blue(' Earth Ready '), emoji.get('headphones'), chalk.blue(' Listening on: '));
  47.  
  48. nodeDialer.dialProtocol(moonPeerInfo, '/chat/1.0.0', (err, conn) => {
  49. if (err) {
  50. throw err
  51. }
  52. console.log('\n' + emoji.get('large_blue_circle'), chalk.blue(' Earth dialed to Moon on protocol: /chat/1.0.0'));
  53. console.log(`${emoji.get('incoming_envelope')} ${chalk.bold(`Type a message and press enter. See what happens...`)}`)
  54. // Write operation. Data sent as a buffer
  55. pull(
  56. p,
  57. conn
  58. )
  59. // Sink, data converted from buffer to utf8 string
  60. pull(
  61. conn,
  62. pull.map((data) => {
  63. return data.toString('utf8').replace('\n', '')
  64. }),
  65. pull.drain(console.log)
  66. )
  67.  
  68. process.stdin.setEncoding('utf8')
  69. process.openStdin().on('data', (chunk) => {
  70. var data = chunk.toString()
  71. var data = `${chalk.blue("Message received from Earth: ")}\n\n` + chunk.toString() + `\n${emoji.get('incoming_envelope')}${chalk.blue(" Send message from Moon:")}`
  72. p.push(data)
  73. })
  74. })
  75. })
  76. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement