Coroutine\Http\Client->recv
最后更新于:2022-04-02 06:27:23
# Coroutine\\Http\\Client->recv
[TOC]
接收消息。与`setDefer`或`upgrade`配合使用。
~~~
function Coroutine\Http\Client->recv(float $timeout = -1);
~~~
* `$timeout`为`upgrade`升级为websocket时可用的超时参数
* `$timeout`设置超时,优先使用指定的参数,其次使用`set`方法中传入的`timeout`配置
* 未设置任何超时,将持续等待
> `$timeout`参数需要`2.1.2`或更高版本
## Http
~~~
go(function () {
$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80);
$cli->setHeaders([
'Host' => "localhost",
"User-Agent" => 'Chrome/49.0.2587.3',
'Accept' => 'text/html,application/xhtml+xml,application/xml',
'Accept-Encoding' => 'gzip',
]);
$cli->set([ 'timeout' => 1]);
$cli->setDefer();
$cli->get('/');
co::sleep(1);
$data = $cli->recv();
});
~~~
## WebSocket
~~~
go(function () {
$cli = new Co\http\Client("127.0.0.1", 9501);
$ret = $cli->upgrade("/");
if ($ret) {
while(true) {
$cli->push("hello");
var_dump($cli->recv());
co::sleep(0.1);
}
}
});
~~~
';