ratchetphp / Pawl

Asynchronous WebSocket client
MIT License
583 stars 84 forks source link

What is the best way to send single message and read reply #98

Open khvalov opened 5 years ago

khvalov commented 5 years ago

Hi, I'm interacting with Websocket server and the first step I have to implement is - authorize with sending websocket JSON packet. And I'm expecting answer contain Client ID some other connection related data The problem I have experienced with - as all I send is combined into frames (?) the authorization request get sent only withing next iteraction backages and I got "not authorized" response.

The way I use Pawl (simplified)


$connector('ws://webserver/connection/websocket')
            ->then(function(\Ratchet\Client\WebSocket $conn) {

            $this->authenticate($conn);
            $this->subscribe($channelID);

            //Default behavior on connection close
            $conn->on('close', function($code = null, $reason = null) {
                $this->runEvents(self::EVENT_DISCONNECTED,[$code=>$reason]);
                $this->loop->stop();
            });

            //Handling messages
            $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
                $this->messageHandler(strval($msg));
            });
        }, function(\Exception $e){
            echo "Could not connect: {$e->getMessage()}\n";
            $this->loop->stop();
        });

        $this->loop->run();
    }

is any way to enforce send ws request ?

ced1check commented 1 year ago

It's been quite some time, however did you find a solution for this? I have a similar issue where I need to send an init message, get the ack and then send a subscribe, however I never catch the ack reply. the on('message') handler never gets called for some odd reason!?