echo_client.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const Peer = require('../index.js').Peer;
  2. // FUNCTIONS --------------------------------------------------------------------------------------
  3. const readline = require('readline').createInterface({
  4. input: process.stdin,
  5. output: process.stdout
  6. });
  7. async function askForInput(promptStr){
  8. return new Promise((resolve, reject) => {
  9. readline.question(promptStr, (input) => {
  10. resolve(input);
  11. });
  12. })
  13. }
  14. // ------------------------------------------------------------------------------------------------
  15. const peer = new Peer({debug: 2});
  16. peer.on('open', async (localId) => {
  17. console.log(localId);
  18. // Connect to the server peer whose ID we already know
  19. const conn = peer.connect('abcdefghijklmnopqrstuvwxyz');
  20. conn.on('open', async () => {
  21. conn.on('data', async (data) => {
  22. console.log(data);
  23. const newUserInput = await askForInput('>');
  24. conn.send(newUserInput);
  25. });
  26. const userInput = await askForInput('>');
  27. conn.send(userInput);
  28. });
  29. })