Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 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 Pushable = require('pull-pushable')
  9. const p = Pushable()
  10. const chalk = require('chalk');
  11. const emoji = require('node-emoji')
  12.  
  13. PeerId.createFromJSON(require('./ids/moonId'), (err, peerId) => {
  14. if (err) {
  15. throw err
  16. }
  17. const peerInfo = new PeerInfo(peerId)
  18. peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/10333')
  19. const nodeListener = new Node({ peerInfo })
  20.  
  21. nodeListener.start((err) => {
  22. if (err) {
  23. throw err
  24. }
  25.  
  26. nodeListener.on('peer:connect', (peerInfo) => {
  27. console.log(emoji.get('moon'),
  28. chalk.blue(' Moon found Earth '),
  29. emoji.get('large_blue_circle'),
  30. chalk.blue(` on: ${peerInfo.id.toB58String()}`));
  31. console.log('\n' + emoji.get('moon'),
  32. chalk.green(' Moon waiting for message from Earth ')
  33. + emoji.get('large_blue_circle'))
  34. })
  35.  
  36. nodeListener.handle('/chat/1.0.0', (protocol, conn) => {
  37. pull(
  38. p,
  39. conn
  40. )
  41.  
  42. pull(
  43. conn,
  44. pull.map((data) => {
  45. return data.toString('utf8').replace('\n', '')
  46. }),
  47. pull.drain(console.log)
  48. )
  49.  
  50. process.stdin.setEncoding('utf8')
  51. process.openStdin().on('data', (chunk) => {
  52. var data = `${chalk.blue("Message received from Moon: ")}\n\n`
  53. + chunk.toString() + `\n${emoji.get('incoming_envelope')}
  54. ${chalk.blue(" Send message from Earth:")}`
  55.  
  56. p.push(data)
  57. })
  58. })
  59.  
  60. console.log(emoji.get('moon'), chalk.blue(' Moon ready '),
  61. emoji.get('headphones'), chalk.blue(' Listening on: '));
  62.  
  63. peerInfo.multiaddrs.forEach((ma) => {
  64. console.log(ma.toString() + '/p2p/' + peerId.toB58String())
  65. })
  66.  
  67. console.log('\n' + emoji.get('moon'), chalk.blue(' Moon trying to connect with Earth '),
  68. emoji.get('large_blue_circle'));
  69. })
  70. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement