swoole_http_client->upgrade

发起WebSocket握手请求,并将连接升级为WebSocket。

function swoole_http_client->upgrade(string $path, callable $callback);

使用实例

$cli = new swoole_http_client('127.0.0.1', 9501);

$cli->on('message', function ($_cli, $frame) {
    var_dump($frame);
});

$cli->upgrade('/', function ($cli) {
    echo $cli->body;
    $cli->push("hello world");
});

onMessage回调

function onMessage(swoole_http_client $client, swoole_websocket_frame $frame);

握手失败

某些WebSocket服务器对于客户端要求非常严格,Client需要非常接近Chrome等浏览器才可以握手成功。

可以通过增加参数和Http头,尽可能地让ClientChrome等浏览器行为保持一致。

$client->set([
	'websocket_mask' => true,
	'ssl_host_name' => 'www.yourdomain.com',
]);
$client->setHeaders([
	'Host' =>  'www.yourdomain.com',
	'UserAgent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
]);