Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 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'), chalk.blue(' Moon found Earth '), emoji.get('large_blue_circle'), chalk.blue(` on: ${peerInfo.id.toB58String()}`));
  28. console.log('\n' + emoji.get('moon'), chalk.green(' Moon waiting for message from Earth ') + emoji.get('large_blue_circle'))
  29. })
  30.  
  31. nodeListener.handle('/chat/1.0.0', (protocol, conn) => {
  32. pull(
  33. p,
  34. conn
  35. )
  36.  
  37. pull(
  38. conn,
  39. pull.map((data) => {
  40. return data.toString('utf8').replace('\n', '')
  41. }),
  42. pull.drain(console.log)
  43. )
  44.  
  45. process.stdin.setEncoding('utf8')
  46. process.openStdin().on('data', (chunk) => {
  47. var data = `${chalk.blue("Message received from Moon: ")}\n\n` + chunk.toString() + `\n${emoji.get('incoming_envelope')}${chalk.blue(" Send message from Earth:")}`
  48. p.push(data)
  49. })
  50. })
  51.  
  52. console.log(emoji.get('moon'), chalk.blue(' Moon ready '), emoji.get('headphones'), chalk.blue(' Listening on: '));
  53.  
  54. peerInfo.multiaddrs.forEach((ma) => {
  55. console.log(ma.toString() + '/p2p/' + peerId.toB58String())
  56. })
  57.  
  58. console.log('\n' + emoji.get('moon'), chalk.blue(' Moon trying to connect with Earth '), emoji.get('large_blue_circle'));
  59. })
  60. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement