1

I am creating a HTML5 game with NodeJS (using websockets). To update my the game and send the data to every player I use setInterval() with a 33ms interval (to update 30 times per second).

It happens that when over 10 players connect to the game, the websockets library starts to consume a lot of CPU and the setInterval start to having some delay (it goes to 27-28 frames per second).

After profiling my code I found out that most of the time my CPU is idle. So I was thiking. Is my whole program idle in that 33ms interval beetwen every loop? Would it work if I set a dynamic interval? Something like when a lot of players are connected and the frames per second starts to drop I reset the interval from 33ms to 28ms for example? Could this work/have anyone use any system like this?

Best regards, Daniel

7
  • You could make a while loop that says while users connected is under 10 then set interval as 33ms and while user connected is over 10, then set the interval as 28ms. Commented Aug 31, 2016 at 1:22
  • why would you increase the interval frequency as a reaction to increased load? Commented Aug 31, 2016 at 1:22
  • @JaromandaX I would increase the frequency on the server to keep the same frequency on the client side. Since I think my program is mostly idle during that 33ms interval between every loop and I want maximize every milisecond I can Commented Aug 31, 2016 at 1:28
  • @Jacob Wilson that is something like I was thinking. But I won´t set a fixed value. I was thinking about calculating the frame rate for example every 10 seconds and then re-adjust the interval to allow the client keep receiving messages every 33ms. Commented Aug 31, 2016 at 1:28
  • @DanielOliveira One thing to consider is when calculating the frame rate, is if you would calculate it client side, you would have to calculate the frame rate then send the rate back server side and adjust in there, but by the time all that takes place, the change may be too late if the adjustment is only for an extremely minimal amount of time Commented Aug 31, 2016 at 1:34

1 Answer 1

1

I understand you problem a lot more clearly now. I'm not sure how I'd tackle that. I found a similar question and the solution was not to use setInterval but rather setTimeout. I think the same logic could be applied once you calculate the time it took for the loop to execute.

javascript while loop or set interval with dynamic delay

EDIT

Would also like to include this thanks to @Bergi How to create an accurate timer in javascript?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.