0
\$\begingroup\$

I have a game I'm working on using node.js and socket.io. The issue I'm having, is I'm trying to have it so that players execute whatever actions they inputed about 50ms in the future, giving everyone a chance to stay relatively in sync.

I've got frame-independant code running, which works well until a client has a different time than the server.

to predict when the client should act out movement code from the server, I use

Date.now() >= command.timeStamp

The issue I'm having is that the client isn't in the same timezone as the server, for instance someone I'm testing with is just one minute above/below my server time, resulting in commands being 900 milliseconds in the past or future, which is non-ideal.

Is there some way I can use a common timestamp across clients and server?

\$\endgroup\$
  • 1
    \$\begingroup\$ Not related to game development. \$\endgroup\$ – nathan Aug 26 '13 at 18:39
  • \$\begingroup\$ I disagree, this question is in regards to synchronous multi-player communication, and its a valid problem. \$\endgroup\$ – Evan Aug 26 '13 at 18:42
  • 1
    \$\begingroup\$ Just because it's used in a game loop doesn't mean it's a game problem, especially with node.js and client side javascript involved. You'll get much better results on the standard programming stacks. \$\endgroup\$ – Patrick Hughes Aug 26 '13 at 20:44
  • \$\begingroup\$ aren't you are talking about two differents issues : the time setting accuracy (hence a 900ms delay where it should be the exact same time) and the timezone issue (hence a several hours issue) ??? \$\endgroup\$ – GameAlchemist Aug 26 '13 at 20:53
2
\$\begingroup\$

Your game timing system should have its own internal clock that is started when the game begins (this can be setup by the server to force clients to be the same). Have you considered using this internal timer? It would be valid, and the same across all clients.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I ended up using something similar where the client syncs with server time. \$\endgroup\$ – ArkahnX Aug 27 '13 at 19:51
0
\$\begingroup\$

Yes, you can use unix time on all the clients, or you can use the same time zone on all the clients, likely UTC, Javascript has a function to help with this.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

The correct answer to your underlying question may be to just not use Date.now() and instead use requestAnimationFrame or performance.now() on the client and process.hrtime() on the server. The former two are much newer and less widely supported but can give you more accuracy than Date.now() can, which is a serious issue for most Web games (especially if the game is real-time or uses canvas animations). requestAnimationFrame at least is in the current versions of all major browsers, though still prefixed for a few of them. The NodeJS one has been available for some time.

See https://stackoverflow.com/questions/6875625/does-javascript-provide-a-high-resolution-timer for some more information, as well https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming.now and https://developer.mozilla.org/en-US/docs/Web/API/window.requestAnimationFrame.

For NodeJS (I'm unclear if you're timing in the server or the client portion), you can't use requestAnimationFrame obviously. You have http://nodejs.org/api/process.html#process_process_hrtime or https://npmjs.org/package/performance-now (which wraps the former) for NodeJS timing.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.