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

Daniel Oliveira
  • 1,280
  • 14
  • 36
  • 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. – Jacob Wilson Aug 31 '16 at 01:22
  • why would you increase the interval **frequency** as a reaction to increased load? – Jaromanda X Aug 31 '16 at 01: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 – Daniel Oliveira Aug 31 '16 at 01: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. – Daniel Oliveira Aug 31 '16 at 01: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 – Jacob Wilson Aug 31 '16 at 01:34
  • @JacobWilson No, I could get the time before the code of my loop starts and then get the time after the loop ends. With that I can calculate the execution time of the loop. I would subtract the execution time of the loop from the 33ms. If my loop is takes 5ms to execute I would set the javascript interval to 28ms so the client keep getting every message with a 33ms interval. My question is, can this system fail? Is this a good approach to this problem? – Daniel Oliveira Aug 31 '16 at 01:38
  • You might want to have a look at [How to create an accurate timer in javascript?](http://stackoverflow.com/q/29971898/1048572) – Bergi Aug 31 '16 at 01:45

1 Answers1

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?

Jacob Wilson
  • 430
  • 1
  • 5
  • 12