PHP套接字_read等待读取数据

PHP套接字_read等待读取数据,php,laravel,websocket,phpwebsocket,Php,Laravel,Websocket,Phpwebsocket,我不熟悉套接字,下面是我的代码: while (true) { $status = $this->client->read(); echo $status; $this->eventQueue->hasEventToSend() ? $this->client->send($this->eventQueue->getEvent()) : null; } 客户端读取方法只是: try { $status = soc

我不熟悉套接字,下面是我的代码:

while (true) {
    $status = $this->client->read();
    echo $status;

    $this->eventQueue->hasEventToSend() ? $this->client->send($this->eventQueue->getEvent()) : null;
}
客户端读取方法只是:

try {
    $status = socket_read($this->socket, $length);
} catch (Throwable $exception) {
    $this->reConnect();
    Log::error($exception->getMessage());

    $status = false;
}

return (string) $status;

$status=$this->client->read()
读取新数据之前,不会执行任何操作。我想停止
socket\u read()
的行为,就像等待数据读取一样,或者仅当有任何数据要读取时才调用
socket\u read()
。我不知道如何做到这一点,所以我在这里问;)

默认情况下,读取操作总是阻塞的。你可以使用

插座\设置\非块($socket)

在调用read()以防止套接字在读取操作中阻塞之前,也可以使用

socket_select()

在读取数据之前检查数据是否可用。

socket\u set\u nonblock()有效,感谢您的帮助:)