OpenSwoole\Coroutine\Http\Client->recv(...)

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine\Http\Client->recv(float $timeout = 0): bool|OpenSwoole\WebSocket\Frame

Parameters

timeout

In seconds, the timeout of the request, 1.5 means 1.5 seconds.

Return

Success

When the client successfully receives data from the WebSocket connection, a frame object is received, see details below.

Failure

If the receive operation fails, false will be returned and you should check $client->errCode to see what went wrong. See more details below.


Description

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.


Return Values

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.


Example

<?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);
        }
    }
});
Last updated on September 1, 2022