I'm using emit method to start a countdown on my android app.
My problem is that the countdown doesn't start at the same time for the player and opponent. My game will play a random song and give points to the first player who guesses correct
I heard that it depends on their latency. I couldn't find any solution to solve this.
var countdown = 3;
var countdownInterval = setInterval(function () {
socket.emit('countdown', countdown); // to player
socket.broadcast.to(opponent).emit('countdown', countdown); // to opponent
if (countdown == 0) {
clearInterval(countdownInterval);
}
countdown--;
}, 1000);
You might want to get a callback (in your case decrease the counter variable) when the client confirmed the message reception.
To do this, simply pass a function as the last parameter of
.send
or.emit
. When you use.emit
, the acknowledgement is done by you, which means you can also pass data along: