PHP接入WebSocket行情
示例代码
<?php
use Swoole\Coroutine\Http\Client;
use function Swoole\Coroutine\run;
run(function () {
$host = '<服务器地址>';
$port = <服务器端口>;
$conn = new Client($host, $port);
$conn->upgrade("/?token=<jvQuant token>");
$conn->push("add=lv2_600519,lv1_000001");
go("receive", $conn);
});
function receive($client)
{
while (true) {
$data = $client->recv();
$time = date("H:i:s");
@$zipStr = gzinflate($data->data);
if ($zipStr) {
echo "{$time} Binary推送:{$zipStr}\n";
} else {
echo "{$time} Text响应:{$data->data}\n";
}
}
}