Efficient solution for countdown timer

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Devops
Posts: 23
Joined: 27 May 2020, 15:23

Efficient solution for countdown timer

Postby Devops » 10 Jun 2020, 05:19

I need to send a countdown timer to the players in the room before the game starts.
So I need to maintain a timer for few seconds to send event on every second to the players in room

For this purpose what would be the best and efficient thing to do and why?
- Using Task scheduler
- Using setInterval

I would need a efficient way so that it won't be causing any issue when many rooms are active and many countdown events have to be sent if many rooms are active at a time
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Efficient solution for countdown timer

Postby Lapo » 10 Jun 2020, 07:20

Hi,
using the scheduler seems a good fit.
You can simply add a non-repeating Task that runs after 1 second and keeps track of the countdown. The task will run and send a message to the client, then check if the countdown is complete. If not it will reschedule itself.

For this you can use the global TaskScheduler, the one you access via:

Code: Select all

getApi().getSystemScheduler();

and maybe reconfigure its thread pool, to 10-12 threads. This should be enough to handle hundreds of tasks, probably even thousands, since the Task is very light.

To change the default number of threads: AdminTool > Server Configurator

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Baidu [Spider], Stevenor and 102 guests