const WebSocket = require('ws'); var exec = require('child_process').exec; const fs = require("fs"); var whoami; try{ var tempObj = fs.readFileSync('whoami'); tempRead = JSON.parse(tempObj); whoami = tempRead.Name; console.log("I am '" + whoami + "'"); } catch (e) { console.log("I could not open whoami, I'm going to exit"); process.exit(); } var ws; var wsConnect = function(){ ws = new WebSocket('ws://192.168.10.26:8080/'); ws.on('open', function open() { console.log("connected"); }) ws.on('message', function incoming(data, flags) { console.log("Received : " + data); try{ myObj = JSON.parse(data); console.log(myObj); console.log(myObj.labelSource); if(whoami == myObj.who){ console.log("I am " + whoami + ", this message was for me"); if(myObj.label){ fs.writeFileSync('internetShipLabel.zpl', myObj.label); printLabel('internetShipLabel.zpl'); } }else{ console.log("this Message was not for me"); } } catch(e){console.log(e);} }) ws.on('error', function(error){ console.log('Connection Error: ' + error.toString()); //process.exit(); }); ws.on('close', function() { console.log('socket close'); setTimeout(wsConnect, 30*1000); }); } function printLabel(filename){ console.log('trying to print'); exec('cat ' + filename + ' > /dev/usb/lp0', function(error, stdout, stderr) { }); } wsConnect();