I am making some simple client / server interaction in NodeJS. Shown is part of the server code I can't get to work. In the first server.on function, the server receives the data and removes the STX and ETX characters. This works, if I log within that function everything seems to be working fine. However, for the second server.on function, I can't seem to get use the result variable from the first function. Now I do know that NodeJS works async and I should use callbacks, but the examples I found where not really applicable for this scenario. Anyone got an idea?var result; server.on('connection', function(socket) { socket.on('data', function (chunk) { var str = chunk.toString(); if (str.charAt(0) === STX && str.charAt(str.length - 1) === ETX) { result = str.replace(/[\x00-\x1F\x7F-\x9F]/g, ""); return result; } else console.log('No STX or ETX found'); }); server.on('connection', function(socket,result){ var end = result.replace('7','8'); socket.write(`${end.toString()}`); });
Submitted December 03, 2019 at 06:56PM by YungErpie
No comments:
Post a Comment