Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var WebSocketClient = require('websocket').client;
- var client = new WebSocketClient();
- client.on('connectFailed', function (error) {
- console.log('Connect Error: ' + error.toString());
- });
- client.on('connect', function (connection) {
- console.log('WebSocket Client Connected');
- connection.on('error', function (error) {
- console.log("Connection Error: " + error.toString());
- });
- connection.on('close', function () {
- console.log('echo-protocol Connection Closed');
- });
- connection.on('message', function (message) {
- if (message.type === 'utf8') {
- console.log("Received: '" + message.utf8Data + "'");
- }
- if (JSON.parse(message.utf8Data).stateId && JSON.parse(message.utf8Data).pulled) {
- sendNumber(JSON.parse(message.utf8Data).stateId, 0, 1);
- sendNumber(JSON.parse(message.utf8Data).stateId, 0, 2);
- sendNumber(JSON.parse(message.utf8Data).stateId, 0, 3);
- sendNumber(JSON.parse(message.utf8Data).stateId, 1, 3);
- }
- });
- function sendNumber(stateId,lever1,lever2) {
- if (connection.connected) {
- var checkMessage = { "action": "check", "lever1": lever1, "lever2": lever2, "stateId": stateId };
- console.log("Sent: " + JSON.stringify(checkMessage));
- connection.send(JSON.stringify(checkMessage));
- }
- }
- });
- client.connect('ws://nuclear.t.javascript.ninja', 'echo-protocol');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement