My problem here is that i need to communicate with a server and each time i send a UDP socket, the server replies. What i need here is each time i send, i need to wait for a reply or organize the replies to be used later before i make a request again.I'm not used to node.js and i realized that instead of using a send and a recv command(i made a similar program in python with socket), there's an event that triggers when the data arrives to my client, i tried to use function callbacks but i don't know how i wait for an event to continue the execution of the function or store the data arrived on a variable and check the variable if the data i want arrived.var dgram = require('dgram'); var read_result = []; var udpsocket = dgram.createSocket('udp4'); udpsocket.on('message', function (message, remote) { read_result.push(message.toString("hex")); console.log(remote.address + ':' + remote.port +' - ' + message.toString('hex')); }); function executeCommand_read(message){ var BufferData = new Buffer.from(message, "hex"); sendRawSocket(BufferData); //i want to return here the result from here //return resultData } function sendRawSocket(BufferData){ udpsocket.send(BufferData, 0, BufferData.length, 9600, '192.168.250.1', function(err, bytes) { if (err) throw err; console.log('UDP message sent, the bytes: ' + bytes + " the data " + BufferData.toString('hex')); }); } executeCommand_read('800003000100003700010101320064010002'); executeCommand_read('800003000100003700010101320064010002'); executeCommand_read('800003000100003700010101320064010002'); executeCommand_read('800003000100003700010101320064010002'); executeCommand_read('800003000100003700010101320064010002'); executeCommand_read('800003000100003700010101320064010002'); in this case the result is:UDP message sent, the bytes: 18 the data 800003000100003700010101320064010002UDP message sent, the bytes: 18 the data 800003000100003700010101320064010002UDP message sent, the bytes: 18 the data 800003000100003700010101320064010002UDP message sent, the bytes: 18 the data 800003000100003700010101320064010002UDP message sent, the bytes: 18 the data 800003000100003700010101320064010002UDP message sent, the bytes: 18 the data 800003000100003700010101320064010002192.168.250.1:9600 - c0000200370000010001010100000101192.168.250.1:9600 - c0000200370000010001010100000101192.168.250.1:9600 - c0000200370000010001010100000101192.168.250.1:9600 - c0000200370000010001010100000101192.168.250.1:9600 - c0000200370000010001010100000101192.168.250.1:9600 - c0000200370000010001010100000101it reads and the replies are correct but i don't know how to handle them when i'm still inside the function executeCommand_read to return them to whoever calls it. I want to make a timer and each time i call this function, i want the result to update values outside this.Tried to put the 'message' event inside the send callback, tried to store the server responses and make a loop so when the correct data arrives it returns the value but i really don't know how to do this...Please help
Submitted June 16, 2019 at 06:29PM by yard1e
No comments:
Post a Comment