Node: Sending UDP packet on timer, infinite loop [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
Why does a while loop block the event loop?
5 answers
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?
node.js loops udp
marked as duplicate by jfriend00
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does a while loop block the event loop?
5 answers
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?
node.js loops udp
marked as duplicate by jfriend00
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
You're blocking the event queue completely. Don't sleep synchronously.
– tkausl
Nov 22 '18 at 2:47
I'm not sure what you mean. How would you loop this? I've tried it withsetTimeout(() => { ping(); }, 3000);
and the loop seems to run without waiting for the timeout to end.
– Eru
Nov 22 '18 at 2:50
node.js runs your Javascript single threaded. So, while you're insleep()
NOTHING else can get processed by your Javascript. Nothing. Thus, the value ofpinging
can never change unless it happens directly inside thewhile
loop. No other events will ever get processed. This type of question has been covered multiple times here.
– jfriend00
Nov 22 '18 at 3:30
add a comment |
This question already has an answer here:
Why does a while loop block the event loop?
5 answers
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?
node.js loops udp
This question already has an answer here:
Why does a while loop block the event loop?
5 answers
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?
This question already has an answer here:
Why does a while loop block the event loop?
5 answers
node.js loops udp
node.js loops udp
asked Nov 22 '18 at 2:45
EruEru
286
286
marked as duplicate by jfriend00
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jfriend00
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 3:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
You're blocking the event queue completely. Don't sleep synchronously.
– tkausl
Nov 22 '18 at 2:47
I'm not sure what you mean. How would you loop this? I've tried it withsetTimeout(() => { ping(); }, 3000);
and the loop seems to run without waiting for the timeout to end.
– Eru
Nov 22 '18 at 2:50
node.js runs your Javascript single threaded. So, while you're insleep()
NOTHING else can get processed by your Javascript. Nothing. Thus, the value ofpinging
can never change unless it happens directly inside thewhile
loop. No other events will ever get processed. This type of question has been covered multiple times here.
– jfriend00
Nov 22 '18 at 3:30
add a comment |
2
You're blocking the event queue completely. Don't sleep synchronously.
– tkausl
Nov 22 '18 at 2:47
I'm not sure what you mean. How would you loop this? I've tried it withsetTimeout(() => { ping(); }, 3000);
and the loop seems to run without waiting for the timeout to end.
– Eru
Nov 22 '18 at 2:50
node.js runs your Javascript single threaded. So, while you're insleep()
NOTHING else can get processed by your Javascript. Nothing. Thus, the value ofpinging
can never change unless it happens directly inside thewhile
loop. No other events will ever get processed. This type of question has been covered multiple times here.
– jfriend00
Nov 22 '18 at 3:30
2
2
You're blocking the event queue completely. Don't sleep synchronously.
– tkausl
Nov 22 '18 at 2:47
You're blocking the event queue completely. Don't sleep synchronously.
– tkausl
Nov 22 '18 at 2:47
I'm not sure what you mean. How would you loop this? I've tried it with
setTimeout(() => { ping(); }, 3000);
and the loop seems to run without waiting for the timeout to end.– Eru
Nov 22 '18 at 2:50
I'm not sure what you mean. How would you loop this? I've tried it with
setTimeout(() => { ping(); }, 3000);
and the loop seems to run without waiting for the timeout to end.– Eru
Nov 22 '18 at 2:50
node.js runs your Javascript single threaded. So, while you're in
sleep()
NOTHING else can get processed by your Javascript. Nothing. Thus, the value of pinging
can never change unless it happens directly inside the while
loop. No other events will ever get processed. This type of question has been covered multiple times here.– jfriend00
Nov 22 '18 at 3:30
node.js runs your Javascript single threaded. So, while you're in
sleep()
NOTHING else can get processed by your Javascript. Nothing. Thus, the value of pinging
can never change unless it happens directly inside the while
loop. No other events will ever get processed. This type of question has been covered multiple times here.– jfriend00
Nov 22 '18 at 3:30
add a comment |
2 Answers
2
active
oldest
votes
As @tkausl says, you're blocking the event loop. Don't use that sleep()
function. Try something like this instead:
function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}
loop();
I first had to comment outclientPing.close();
in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.
– Eru
Nov 22 '18 at 3:23
add a comment |
So I finally got it behave properly using this:
function loop() {
setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);
}
loop();
So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.
There's no point inloop
in this case. Just usesetInterval
directly.
– Brad
Nov 22 '18 at 3:31
I wanted to use it as an export from this module that my server file can import. So I didconst loop = funtcion() { setInterval(() => {}, 3000); }
in the end.
– Eru
Nov 22 '18 at 3:39
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As @tkausl says, you're blocking the event loop. Don't use that sleep()
function. Try something like this instead:
function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}
loop();
I first had to comment outclientPing.close();
in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.
– Eru
Nov 22 '18 at 3:23
add a comment |
As @tkausl says, you're blocking the event loop. Don't use that sleep()
function. Try something like this instead:
function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}
loop();
I first had to comment outclientPing.close();
in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.
– Eru
Nov 22 '18 at 3:23
add a comment |
As @tkausl says, you're blocking the event loop. Don't use that sleep()
function. Try something like this instead:
function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}
loop();
As @tkausl says, you're blocking the event loop. Don't use that sleep()
function. Try something like this instead:
function loop() {
if (!pinging) {
return;
}
ping();
console.log('ping');
setTimeout(loop, 3000);
}
loop();
answered Nov 22 '18 at 2:56
BradBrad
117k29239398
117k29239398
I first had to comment outclientPing.close();
in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.
– Eru
Nov 22 '18 at 3:23
add a comment |
I first had to comment outclientPing.close();
in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.
– Eru
Nov 22 '18 at 3:23
I first had to comment out
clientPing.close();
in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.– Eru
Nov 22 '18 at 3:23
I first had to comment out
clientPing.close();
in order to get your code looping properly. Forgot to get rid of that from just the standalone file. Even after doing so though I was only seeing the console log the the ping but not see the packet reach my client. Though I think I found another method I answer below.– Eru
Nov 22 '18 at 3:23
add a comment |
So I finally got it behave properly using this:
function loop() {
setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);
}
loop();
So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.
There's no point inloop
in this case. Just usesetInterval
directly.
– Brad
Nov 22 '18 at 3:31
I wanted to use it as an export from this module that my server file can import. So I didconst loop = funtcion() { setInterval(() => {}, 3000); }
in the end.
– Eru
Nov 22 '18 at 3:39
add a comment |
So I finally got it behave properly using this:
function loop() {
setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);
}
loop();
So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.
There's no point inloop
in this case. Just usesetInterval
directly.
– Brad
Nov 22 '18 at 3:31
I wanted to use it as an export from this module that my server file can import. So I didconst loop = funtcion() { setInterval(() => {}, 3000); }
in the end.
– Eru
Nov 22 '18 at 3:39
add a comment |
So I finally got it behave properly using this:
function loop() {
setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);
}
loop();
So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.
So I finally got it behave properly using this:
function loop() {
setInterval(() => {
clientPing.send("Hi Uno", 8888, 'XXX.XX.XX.XXX', (err) => {
});
}, 5000);
}
loop();
So now the Pi running this pings my arduino client successfully every 5s, the arduino then replies with temp and humidity data, the Pi server then catches it and pushes that to mongo.
answered Nov 22 '18 at 3:27
EruEru
286
286
There's no point inloop
in this case. Just usesetInterval
directly.
– Brad
Nov 22 '18 at 3:31
I wanted to use it as an export from this module that my server file can import. So I didconst loop = funtcion() { setInterval(() => {}, 3000); }
in the end.
– Eru
Nov 22 '18 at 3:39
add a comment |
There's no point inloop
in this case. Just usesetInterval
directly.
– Brad
Nov 22 '18 at 3:31
I wanted to use it as an export from this module that my server file can import. So I didconst loop = funtcion() { setInterval(() => {}, 3000); }
in the end.
– Eru
Nov 22 '18 at 3:39
There's no point in
loop
in this case. Just use setInterval
directly.– Brad
Nov 22 '18 at 3:31
There's no point in
loop
in this case. Just use setInterval
directly.– Brad
Nov 22 '18 at 3:31
I wanted to use it as an export from this module that my server file can import. So I did
const loop = funtcion() { setInterval(() => {}, 3000); }
in the end.– Eru
Nov 22 '18 at 3:39
I wanted to use it as an export from this module that my server file can import. So I did
const loop = funtcion() { setInterval(() => {}, 3000); }
in the end.– Eru
Nov 22 '18 at 3:39
add a comment |
2
You're blocking the event queue completely. Don't sleep synchronously.
– tkausl
Nov 22 '18 at 2:47
I'm not sure what you mean. How would you loop this? I've tried it with
setTimeout(() => { ping(); }, 3000);
and the loop seems to run without waiting for the timeout to end.– Eru
Nov 22 '18 at 2:50
node.js runs your Javascript single threaded. So, while you're in
sleep()
NOTHING else can get processed by your Javascript. Nothing. Thus, the value ofpinging
can never change unless it happens directly inside thewhile
loop. No other events will ever get processed. This type of question has been covered multiple times here.– jfriend00
Nov 22 '18 at 3:30