There are data that are constantly updated on the server and written to the file (every 100 ms), i need to transfer that to the client side, for the time being I did this through the setInterval function (which should check them at the same interval), checking this JSON file, and im not sure if it's a correct solution of the problem.
Server side code:
function SerialPortStart(COM, Input){
console.log( COM, Input);
var serialPort = new SerialPort(COM, {
parser: SerialPort.parsers.readline('\n'),
baudrate: +Input || 9600
});
serialPort.on('data', function (data) {
thermodata = data.toString();
console.log(data);
fs.writeFileSync(__dirname +'/views/cockpit.json',JSON.stringify(thermodata));
});
};
Client side code:
function SerialPortStart(){
var xhr = new XMLHttpRequest();
setInterval(function() {
xhr.open('GET', 'cockpit.json', true);
xhr.send();
xhr.onreadystatechange = function() {
if(this.status == 200) {
var data = JSON.parse(xhr.responseText);
console.log(data);
obj = data.split(';');
if(cockpitNum[3] != null){
$('header .mainBlock1 .cockpit .elements .blockHaw .cirle').css({
transform: 'rotate('+(obj[cockpitNum[3]]) +'deg)'
}); }
}, 50);