PHP之使用swoole统计在线人数和ID PHP之使用swoole统计在线人数和ID案例讲解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

PHP之使用swoole统计在线人数和ID PHP之使用swoole统计在线人数和ID案例讲解

我是高手高手高高手   2021-08-06 我要评论
想了解PHP之使用swoole统计在线人数和ID案例讲解的相关内容吗,我是高手高手高高手在本文为您仔细讲解PHP之使用swoole统计在线人数和ID的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:php之swoole统计在线,php之swoole统计在线人数讲解,下面大家一起来学习吧。
$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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们