Answer the question
In order to leave comments, you need to log in
How to update data every second?
Good afternoon, tell me how to properly organize the data update?
For example, there are all sorts of VKontakte farms there. He built a plant, and the countdown is on, the user refreshed the page, it shows him how much time is left, went for a walk, left the game, after the countdown is completed, the function is executed. How to organize it correctly? Knon? And if there are a lot of users, the server will not fail?
Answer the question
In order to leave comments, you need to log in
So there the timer works on the server side, which is launched by pressing the "Build" button. And to the user this timer and in general everything is displayed through JS as far as I know. When the timer expires you can do a page refresh.
Those. The data is not updated every second.
If no calculations are needed on the server in the absence of the user, then marrk2 is right, even cron is not needed. But if you still need the server to be alive and constantly process data, then look towards queues. For example https://github.com/pda/pheanstalk, https://github.com/videlalvaro/php-amqplib, https://github.com/videlalvaro/Thumper. You can also look at pgq for postgresql.
1. On the server side, a record "required action, time of this action, related information (user, room, etc.)" is written somewhere. There is a system that keeps track of upcoming events (according to the "time" field) and performs the necessary actions by changing the state of the room, user, etc. If the client is connected, then the appropriate command is sent to the client to change the game environment.
2. On the client side:
a) the client polls the server about all the events that the user should have.
b) according to the timer, clients display these events in the right places and update the time for them.
How to complicate things... Just write timestamp the moment when the event started. Further, knowing how long the event should go and its start time, when loading the page, you can always find out how much time is left
// now - текущий timesamp
// timesamp - сохраненный timesamp начала отсчета
// duration - длительность события в секундах
if (now > timestamp + duration) {
// событие уже истекло
} else {
// осталось (timestamp - now) секунд
}
Alliance is right. I wrote this solution myself. A bunch of php + js, information about events is stored in the database. At the beginning of the event, a js timer is set, which will refresh the page upon completion. When loading the page, we look at the current events in the database, if something has completed, we display information, if not, we display js timers for events ..
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question