Socket.io countdown synchronously?

On my server side of the script, I call two emissions at the same time, which look like this.

if (songs.length > 0) { socket.emit('data loaded', songs); socket.broadcast.to(opponent).emit('data loaded', songs); } 

One for the adversary, and another for himself.

My question is , when the data is downloaded, a countdown will appear for both players in my Android application. It is important for me that they display the same number on the screen and simultaneously play music.

I am open to all the advice.

+7
javascript asynchronous
source share
4 answers

As for js timers, this will be a small difference. We can reduce the time difference by reducing the latency, with the difference between the request and the response time from the server.

 function syncTime() { console.log("syncing time") var currentTime = (new Date).getTime(); res.open('HEAD', document.location, false); res.onreadystatechange = function() { var latency = (new Date).getTime() - currentTime; var timestring = res.getResponseHeader("DATE"); systemtime = new Date(timestring); systemtime.setMilliseconds(systemtime.getMilliseconds() + (latency / 2)) }; res.send(null); } 

The elapsed time between sending the request and returning the response needs to be calculated, divide this value by 2. This gives you an approximate delay value. If you add this time value from the server, you will be closer to the true server time (the difference will be in microseconds)

Link: http://ejohn.org/blog/accuracy-of-javascript-time/

Hope this helps.

+1
source share

I made a request and I had the same problem. In this case, I solved the problem, leaving time control on the server. The server sends to the client, and the client increases the time. Perhaps in your case you may have a connection problem. If the problem exists, you can leave clients to increase the time yourself and send a tick several times with the correct time for synchronization.

0
source share

I could give you something like a roar, but I did not check. This solution has the following steps:

  • Synchronize timers for client and server . all users have the same difference with the server timer.
  • For the desired response/request get the client time and look for differences with server time.
  • Consider smallest as the first countdown to be launched.
  • For each response(socket) subtract the difference from smallest and start the client counter after waiting as much as this time .

A client that receives 0 in the response data will start immediately. and the main problem that you may have is the broadcast method, which you cannot use if you think this solution will be useful. This post may help you.

0
source share

Add time to the original message. Let's say that songs are an object with {"time" : timeString, "songs" : songsList} .

If we believe that the device time is correct, you can calculate the time required to move the information, and then simply use the server timer as the main calculator.

The client will receive the time when the countdown begins:

 var start = false; var startTime = 0; var myTime = new Date().getMilliseconds(); var delay = 1000 - myTime; setTimeout(function(){ intervalID = setInterval(function(){ myTime = new Date().getTime(); //console.log(myTime); to check if there is round number of milliseconds if (startTime <= myTime && start = true) {startCountdown();} }, 100); //put 1000 to check every second if second is round //or put 100 or 200 is second is not round }, delay); socket.on('data loaded', data){ startTime = data.time; start = true; } function startCountdown(){ //your time countdown } 

And this works great when 2 clients are from the same region, so you will need a “time converter” to check if the time is good due to the time difference if you strictly need the same numbers.

After the countdown ends, you must clearInterval(intervalID);

0
source share

All Articles