tuxmartin

ws

Feb 12th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env node
  2. var WebSocketClient = require('websocket').client;
  3.  
  4. var client = new WebSocketClient();
  5.  
  6. client.on('connectFailed', function(error) {
  7.     console.log('Connect Error: ' + error.toString());
  8. });
  9.  
  10. client.on('connect', function(connection) {
  11.     console.log('WebSocket Client Connected');
  12.     connection.on('error', function(error) {
  13.         console.log("Connection Error: " + error.toString());
  14.     });
  15.     connection.on('close', function() {
  16.         console.log('echo-protocol Connection Closed');
  17.     });
  18.     connection.on('message', function(message) {
  19.         if (message.type === 'utf8') {
  20.             console.log("Received: '" + message.utf8Data + "'");
  21.         }
  22.     });
  23.  
  24.     function sendNumber() {
  25.         if (connection.connected) {
  26.             var number = Math.round(Math.random() * 0xFFFFFF);
  27.             connection.sendUTF(number.toString());
  28.             setTimeout(sendNumber, 1000);
  29.         }
  30.     }
  31.     sendNumber();
  32. });
  33.  
  34. client.connect('ws://194.228.153.100:9000/ws/text/75?access_token=67bde5d1b5f14520a565d03deae877fb', 'echo-protocol');
Advertisement
Add Comment
Please, Sign In to add comment