Tuesday, 27 August 2013

proper calculation for javascript time differences from server roundtrip

proper calculation for javascript time differences from server roundtrip

trying to get difference in time handled for a game, but I haven't been
able to get the math right (My weak point). Heres what I have so far:
function(timeSentToServer, difference, serverTime) {
var roundTripTime = Date.now()-timeSentToServer;
var responseTime = roundTripTime-difference;
timeDifference = responseTime;
}
timeSentToServer is the Date.now() when the request was sent difference is
serverTime-timeSentToServer serverTime is the time when the server
received the request
the timeDifference should be a value that I can add to Date.now() to match
it up with the current serverTime (probably a few milliseconds off, but
thats acceptable)
however, I am running into an issue where one device Im testing against is
just one second ahead of the server, and its throwing the whole
calculation out of whack
e.g.: currentTime: 968952 sentTime: 968834 serverTime: 967742 difference:
1210
which results in being even more ahead of the server.
will Date.now()-difference be the solution I'm looking for? and also, is
there any other articles I can read on the subject, and any different
solutions?

No comments:

Post a Comment