Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Coroutine\Http\Client->recv(float $timeout = 0): bool|OpenSwoole\WebSocket\Frame
In seconds, the timeout of the request, 1.5 means 1.5 seconds.
When the client successfully receives data from the WebSocket connection, a frame object is received, see details below.
If the receive operation fails, false
will be returned and you should check $client->errCode
to see what went wrong. See more details below.
Receive data from the remote WebSocket server.
Important: This method is only available when you upgrade your connection to use the WebSocket protocol.
When setting a timeout, the parameter timeout takes priority but please read the timeout guide as well.
Upon a successful receive, this method will return a OpenSwoole\WebSocket\Frame
object.
When the receive operation fails, false
is given, check $client->errCode
to see what went wrong. When an error happens the connection may be closed.
<?php
use OpenSwoole\Coroutine\Http\Client;
co::run(function()
{
$client = new Client('127.0.0.1', 9501);
$ret = $client->upgrade('/');
if($ret)
{
while(true)
{
// Push, receive and wait
$client->push('Hello World!');
var_dump($client->recv());
Co\System::sleep(2);
}
}
});