$server->connections

// s e r v e r − > c o n n e c t i o n s 遍 历 所 有 w e b s o c k e t 连 接 用 户 的 f d , 给 所 有 用 户 推 送 统 计 人 数 : c o u n t ( server->connections 遍历所有websocket连接用户的fd,给所有用户推送 统计人数:count( server−>connections遍历所有websocket连接用户的fd,给所有用户推送统计人数:count(server->connections)
例子:
在开启或关闭时统计在线人数
开启

$server->on('open', function (swoole\websocket\server $server, $request) {
    $arr = array('action'=>'count','num'=>count($server->connections));
    $jsonto = json_encode($arr);
    echo "在线人数:{$jsonto};server: handshake success with fd={$request->fd}\n";
});

关闭

$server->on('close', function ($ser, $fd) {
    $arr = array('action'=>'count','num'=>count($server->connections));
    $jsonto = json_encode($arr);
    echo "在线人数:{$jsonto};client {$fd} closed\n";
});

出现错误:php fatal error: uncaught swoole\exception: failed to listen server port[0.0.0.0:9502], error: address already in use

这是因为swoole调试由于开启了进程守护,导至报错
关掉进行就,在重新启动就不会了
1 查看我的 swoole 监听端口号 9502

//查看端口
netstat -anp  | grep  9502
//关掉守护
kill -9 174739

再重启服务

参考:

https://wenda.swoole.com/detail/106719
http://www.51zuso.com/admin/p/710.html

到此这篇关于php之使用swoole统计在线人数和id案例讲解的文章就介绍到这了,更多相关php之使用swoole统计在线人数和id内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!