Nekland / Woketo

A WebSocket library for PHP.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Client stuck after connecting

nospoon opened this issue · comments

I've set up a simple client connection as per documentation, with a simple message handler for logging the events, however after receiving the onConnection event, the client just hangs indefinitely unless I explicitly close the connection on the event.
My understanding is that it should be asynchronous, isn't that correct?

It is asynchronous indeed. Can you provide a reproducer please?

It's the most basic setup really.

try {
   $client = new WebSocketClient('ws://' . config('ari.ari_url') . "/events?app=$app&api_key=asterisk:dev", ['prod' => false]);
   $client->start(new AriMessageHandler());
} catch (Exception $e) {
   $this->line($e->getMessage());
}

dd('test');

AriMessageHandler

use Nekland\Woketo\Core\AbstractConnection;
use Nekland\Woketo\Exception\WebsocketException;
use Nekland\Woketo\Message\MessageHandlerInterface;

class AriMessageHandler implements MessageHandlerInterface
{
    public function onConnection(AbstractConnection $connection)
    {
        \Log::debug('Websocket connected');
    }

    public function onMessage(string $rawData, AbstractConnection $connection)
    {
        \Log::debug($rawData);
    }

    public function onBinary(string $data, AbstractConnection $connection)
    {
        // Do nothing
    }

    public function onError(WebsocketException $e, AbstractConnection $connection)
    {
        \Log::error($e->getMessage());
    }

    public function onDisconnect(AbstractConnection $connection)
    {
        // Do nothing
    }

}

I am getting the "Websocket connected" log, but that's all.

Running php 7.3 on Debian 10 virtual machine

If the server does not send any message. That's totally normal behavior. You're dd() cannot be reached unless the loop is over. PHP does not provide any async feature that allows us to give back the hand on the process after starting the client.

The asynchronous is about execution order: you define things that will be automatically called later and can also add timers, buffers and other stuff that need non-synchronous processing.

As I understand there's no problem here. You can refer to the documentation of React to learn more.

It should not block execution of the followup logic. It is useless that way. Check out ratchet/pawl client to see what I mean by asynchronous. This is not.

Ratchet is also based on reactphp. Thanks for your concern, please be kinder next time.

For the record, they use a fun hack, but it works the same as Woketo finally.
Both are asynchronously working.