hoaproject / Websocket

The Hoa\Websocket library.
https://hoa-project.net/
423 stars 75 forks source link

How can I do action every x seconds/ticks/whatever ? #105

Closed marmotz closed 6 years ago

marmotz commented 6 years ago

Hi everyone,

I want to do a given action every x seconds or ticks.

Is there a way to do that ?

Pierozi commented 6 years ago

Hello :) Can you bit more specific? Did you want a Hoa\Websocket\Server send indefinitely message to client within a interval?

marmotz commented 6 years ago

Well... I don't know how to be more specific.

My need is to check a local variable and resend message if there was no response from client. So I want to check this variable every "tick" to check message sent one minute ago and resend it. But this is so specif to my app.

My need is a global need: to do an action every x seconds and not on a given event (message, ping, etc...)

For the moment, I use a workaround. I listen every events (binary-message, close, error, message, open and ping) and I hope there are enough events to simulate what I want...

Pierozi commented 6 years ago

I dunno if this is possible right now, because the run method will be blocked by stream_select It mean, even with concurrent tasks generator that should probably don't work with another loop.

The easier solution in your case is to spawn a client for this purpose. You can have a local daemon or a cronjob that will send message to server with specific key that make the job you want to do.

If you want to hack it, check it out nikita work: https://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html

marmotz commented 6 years ago

I think my workaround will do the job :-/

Hywan commented 6 years ago

The next version of hoa/websocket will rely on hoa/future and a new version of hoa/socket. It will allow that. Right now, it's not possible. Sorry.

marmotz commented 6 years ago

Good news, thanks Hywan :)