access Ratchet loop from inside the chat class

231 Views Asked by At

I'm trying the use the already made event loop by Ratchet.

$server=IoServer::factory(new HttpServer(new WsServer(new class implements MessageComponentInterface{
    public function __construct(){
        // get the Ratchet Loop
    }

    public function onOpen(ConnectionInterface $conn){}
    
    public function onMessage(ConnectionInterface $from, $msg){}
    
    public function onClose(ConnectionInterface $conn){}

    public function onError(ConnectionInterface $conn, \Exception $e){}
})),123);

I can get it by calling the $server->loop, but I can't pass it to the class constructor since $server is not accessible during the initialization of itself, I wondered if there is a better way of getting it?

1

There are 1 best solutions below

0
On

You can instantiation a loop and pass it to __construct

$loop    = React\EventLoop\Factory::create();
$webSock = new React\Socket\Server('127.0.0.1:8080', $loop);

$server = new IoServer(new HttpServer(new WsServer(new class($loop) implements MessageComponentInterface {
    protected $loop;

    public function __construct(\React\EventLoop\LoopInterface $loop)
    {
        $this->loop = $loop;
    }

    public function onOpen(ConnectionInterface $conn)
    {
    }

    public function onMessage(ConnectionInterface $from, $msg)
    {
    }

    public function onClose(ConnectionInterface $conn)
    {
    }

    public function onError(ConnectionInterface $conn, \Exception $e)
    {
    }
})), $webSock);
$loop->run();

In this approach can't use IoServer::factory. Because you need to pass your own loop to $webSock and IoServer