有办法区分 $server->connections 是http还是websocket么?
来源:8-12 赛事直播在线用户处理 - redis方案(一)
弱弱小乖乖
2019-02-25
如果我启动的 \Swoole\WebSocket\Server ,然后有的请求是http过来的,有的是websocket过来的,都是同一个端口。那我要怎么区别 Server::$connections迭代出来的是http的请求客户端,还是websocket请求的客户端呢?
public function push()
{
$server = $_SERVER['swoole_server'];
foreach($server->connections as $fd)
{
// Swoole\WebSocket\Server::push(): the connected client of connection[2] is not a websocket client or closed
$server->push($fd, "你在干嘛呀!小子!");
}
echo "当前服务器共有 ".count($server->connections). " 个连接\n";
return '';
}
因为有的请求是http过来的,有的是websocket过来的,如果我直接这样foreach去push每个fd,就会报错
Swoole\WebSocket\Server::push(): the connected client of connection[2] is not a websocket client or closed
有办法区分 $server->connections 是http还是websocket么?
写回答
2回答
-
hen_nam
2021-08-07
foreach ($server->connections as $fd) { if ($server->isEstablished($fd)) { $server->push($fd, '...'); } }
https://wiki.swoole.com/#/websocket_server?id=isestablished
10 -
qq_起个啥名呢_0
2019-03-07
我觉得既然是这样的话,那就不要用swoole提供的connections ,可以使用swoole的table或者redis,在onOpen回调中把fd保存一下,这样fd都是websocket的了
10
相似问题