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

Declaration

<?php
Swoole\Coroutine\Http\Client->recv(float $timeout = 0): bool|Swoole\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 Swoole\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 Swoole\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(0.2);
        }
    }
});