Advertisement
monkk

Untitled

Feb 7th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. var WebSocketClient = require('websocket').client;
  2.  
  3. var client = new WebSocketClient();
  4.  
  5. client.on('connectFailed', function (error) {
  6. console.log('Connect Error: ' + error.toString());
  7. });
  8.  
  9. client.on('connect', function (connection) {
  10. console.log('WebSocket Client Connected');
  11.  
  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. if (JSON.parse(message.utf8Data).stateId && JSON.parse(message.utf8Data).pulled) {
  23. sendNumber(JSON.parse(message.utf8Data).stateId, 0, 1);
  24. sendNumber(JSON.parse(message.utf8Data).stateId, 0, 2);
  25. sendNumber(JSON.parse(message.utf8Data).stateId, 0, 3);
  26. sendNumber(JSON.parse(message.utf8Data).stateId, 1, 3);
  27. }
  28. });
  29.  
  30. function sendNumber(stateId,lever1,lever2) {
  31. if (connection.connected) {
  32. var checkMessage = { "action": "check", "lever1": lever1, "lever2": lever2, "stateId": stateId };
  33. console.log("Sent: " + JSON.stringify(checkMessage));
  34. connection.send(JSON.stringify(checkMessage));
  35. }
  36. }
  37.  
  38. });
  39.  
  40. client.connect('ws://nuclear.t.javascript.ninja', 'echo-protocol');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement