Advertisement
Guest User

Test 2

a guest
Nov 20th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const WebSocket = require('ws');
  2. var ws, interval, response;
  3.  
  4. function connect () {
  5.         console.log('Connecting...')
  6.         clearInterval(interval);
  7.         ws = new WebSocket('wss://fairlay.com/testing/');
  8.         ws.on('close', function (e) { console.log(e); setTimeout(connect, 5000); });
  9.         ws.on('error', function (e) { console.log(e); setTimeout(connect, 5000); });
  10.         ws.on('open', function () {
  11.             response = false;
  12.             console.log("Ignore next response:")
  13.             interval = setInterval(send_and_wait, 5000);
  14.         });
  15.         ws.on('message', function (data) {
  16.                 console.log('PONG!');
  17.                 response = true;
  18.         });
  19. }
  20.  
  21. function send_and_wait () {
  22.     if (!response) console.log('No response from server');
  23.     else console.log('Everything\'s fine')
  24.  
  25.     response = false;
  26.     console.log('PING?')
  27.     ws.send('PING');
  28.        
  29. }
  30.  
  31. connect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement