Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ratchet send doesn't work with sleep function

Tags:

php

sleep

ratchet

Ratchet send doesn't work with the sleep function, how can I fix this?

Here is my code:

$i = 0;
while($i < 180)
{
    foreach ($this->clients as $client)
    {

       $client->send($res->asXML()."\0");
    }
    sleep(2);
    $i++;
}
like image 294
user2507316 Avatar asked Jun 08 '26 16:06

user2507316


1 Answers

It is not a good idea to use sleep() with ratchet. Instead it would be best to take advantage of its existing event loop.

I'm not sure what exactly you are trying to accomplish, but here is a basic example. I am willing to bet that you will need to re-work your code to accommodate this different technique.

//Start the server
$server = IoServer::factory(
    new WsServer($session),
    $port,
    $addr
);

//Attach the loop
$server->loop->addPeriodicTimer(2, function () {
    $client->send($res->asXML()."\0");
});

Sources:

  1. https://github.com/reactphp/event-loop
  2. https://groups.google.com/forum/#!msg/ratchet-php/MsTqELDoBb0/lQ_J8aR2eUcJ
like image 82
user729928 Avatar answered Jun 10 '26 07:06

user729928



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!