0
\$\begingroup\$

I'm new to game server develop. I'm developing a card game server. How could I make nodejs server wait for few seconds then push data to client after an event fired ? Example : in poker game, one player have about 20 seconds to make a move, after 20 seconds server will auto fold and then push a message to another player. How could I make server wait for 20 seconds and then do next action ?

\$\endgroup\$
1
\$\begingroup\$

Node has build in timer functionality well documented on their website. Set the delay to 20 * 1000 and have the code in the callback trigger the fold then inform the client.

Timers

setTimeout(callback, delay, [arg], [...])#

To schedule execution of a one-time callback after delay milliseconds. Returns a timeoutObject for possible use with clearTimeout(). Optionally you can also pass arguments to the callback.

It is important to note that your callback will probably not be called in exactly delay milliseconds - Node.js makes no guarantees about the exact timing of when the callback will fire, nor of the ordering things will fire in. The callback will be called as close as possible to the time specified.

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

If you talking about "few seconds", you can use setInterval as well as setTimeout.

There is all you need to know about timers in nodejs https://nodejs.org/api/timers.html#timers_setinterval_callback_delay_args

setInterval Example:

setInterval(function(){
   //do something each 5 seconds (maybe +/- few milliseconds)
}, 5000);

setTimeout example:

function timeout() {
   setTimeout(function () {
     //do something each 5 seconds (maybe +/- few milliseconds)
     timeout();
   }, 1000);
}
timeout(); // here we start loop

Difference:

setInterval CAN skip some timer ticks if callback works longer than timer duration. (some hard loop for 7 seconds for example). You should never meet that you talking about "few seconds". But it may happen if you set intervals to few milliseconds.

setTimeout will not skip ticks, and start new times each time when all tick tasks is complete.

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

Well, the answer to this is not that easy. If I would be developing this, I would create a loop on server side, where server

  1. starts
  2. loads all resources
  3. Updates game / caches packets to send
  4. sends packets to all players, delete all cached messages
  5. goto 3

Then, If you want to wait before sending user data, then in UPDATE logic you will keep track of time. If time is up, put the message to be sent to cached queue.

As there will be message to be sent, the server will send it as soon as it can.

Here is a diagram from my website, where is depicted how server operates: Example of my game server's workflow

Please, notice mainly the server updates, and then sends all packets to their recipients.

So If you want to wait 20s, you create new class Timer, which will get the current time upon creation, and how long to wait. Then, every single time you call timer.update() it returns you true if time is up.

For additional info, I have publicly available documentation for my own nodeJS project. Documentation describes how I solve certain issues in game dev, such as client-server interactions, how I send data, serialization, and what to be aware of.

That can be found here, in section documentation.

Good luck with your project, if you have any further question, just ask me. I like game dev and will be happy to help

By the way, if you are new to server develop, it is crucial to know how to create the architecture of the code, otherwise you get lost. Trust me, I used to be new to that stuff too. Rewriting code is a pain.

| 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.