I'm trying to send a packet from my server to ping a client every few seconds in an infinite loop at all times.
This is the code I am running:
const dgram = require('dgram');
const message = Buffer.from('Some bytes');
const clientPing = dgram.createSocket('udp4');
const pinging = true;
function ping() {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
clientPing.close();
});
}
function sleep(time, callback) {
var stop = new Date().getTime();
while(new Date().getTime() < stop + time) {
;
}
callback();
}
function loop() {
while(pinging == true) {
sleep(3000, function() {
ping();
console.log('ping');
});
}
}
loop();
What's weird is that the console logs the string ping just fine at the given interval but the packet is never sent and never reaches my client. But when I run just ping();
outside of the loop the packet hits the client.
Udp isn't supposed to wait for a response after it sends the packet and doesn't wait for a response. What am I missing here?