Quantcast
Channel: Questions in topic: "nodejs"
Viewing all articles
Browse latest Browse all 130

Nodejs and Unity: Incorrect timing

$
0
0
Hey guys, apparently Unity and Node.js are treating / calculating time differently. On the node.js side, I start a new round by having two variables, timestampLastRound and timestampNextRound. They are initialized as Date.now() and Date.now() + ROUND_LENGTH * 1000 (because the round length is in seconds and JS timestamps are in milliseconds). When Unity asks for the timings, it sends a GET request to the server (running on the same machine, so this should be pretty much instantaneous, right?) http://localhost/time is the url, and the server answers with a JSON Object which looks like this: { nextRound: Math.round(timestampNextRound / 1000), //unity uses seconds instead of milliseconds anyway and it seemed a little easier to read to just transmit seconds roundCount: roundCount, roundLength: ROUND_LENGTH } On the Unity side of things, the JSON Object is first parsed by the class JSONObject (not written by me), and then converted like so: public override void FromJSON(JSONObject json) { int nextRoundTimestamp = 0; json.GetField(ref nextRoundTimestamp, "nextRound"); nextRound = ConvertFromUnixTimestamp(nextRoundTimestamp); json.GetField(ref roundCount, "roundCount"); json.GetField(ref roundLength, "roundLength"); } /// /// Converts a Unix timestamp into a System.DateTime /// /// The Unix timestamp in seconds to convert /// DateTime obtained through conversion public static DateTime ConvertFromUnixTimestamp(double timestamp) { DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); origin = origin.AddSeconds(timestamp); return DateTime.SpecifyKind(origin, DateTimeKind.Utc); } Here's my problem: If I now output nextRound - System.DateTime.Now, formatted in minutes and seconds, I find out that the next timestep will be in 1 minute and 30 seconds, whereas the rounds are only 60 seconds long and the server correctly shows, that there is at most a minute left until the next round. Compensating for 30 seconds would be easy, but I want to know why this happens. Also it's not always 30 seconds off, sometimes it's 37 seconds, sometimes it's less. Does anyone see why this happens and how I can fix it?

Viewing all articles
Browse latest Browse all 130

Latest Images

Trending Articles



Latest Images