I've been working on a website requiring accurate, in-sync, timing across any user on the website. To do so I use web sockets in order to ensure fast responses between the frontend and backend (because there's a lot of timers that need to be in-sync and such). However, after recoding my web-socket related code numerous times I've come to the conclusion that the best way to proceed with my project and maintain a fast, reliable, and most importantly, clean, understandable, code would be to somehow make a repeating task.
I've done extensive googling about this subject and all I've seen so far is people advising not to, and to rather use JS, which I don't want.
My logic for this code is as such:
-
When the PHP class is first made (and it'll only be made once) create some kind of storage unit to store all web socket connections that will come through. (Such as an SplObjectStorage).
-
When a new connection is opened they get added to the storage, or vice versa if the connection is closed.
-
Every second (first activated in the constructor) a function gets ran in the same PHP class will send a web-socket request to all the stored connections telling them to update their times (which will then be handled with JS). This way it ensures that everyones timers will be in-sync to one another, rather than being a few seconds off. This would also patch up a few security issues I can imagine will occur on the website.
I've already tried one way, using an infinite while-loop and the sleep method. However that froze my PHP server on startup so that would therefore not work.
If anyone has any ideas or a better solution than the one above it would be greatly appreciated.
Thank you, - Exporting
Aucun commentaire:
Enregistrer un commentaire