入门指引 Server Client Process AsyncIO Memory HttpServer WebSocket 高级 其他

发布于 2015-08-09 09:41:31 | 106 次阅读 | 评论: 0 | 来源: 网络整理

用来遍历当前Server所有的客户端连接,connection_list方法是基于共享内存的,不存在IOWait,遍历的速度很快。另外connection_list会返回所有TCP连接,而不仅仅是当前worker进程的TCP连接。

需要swoole-1.5.8以上版本
connection_list仅可用于TCP,UDP服务器需要自行保存客户端信息
SWOOLE_BASE模式下只能获取当前进程的连接

函数原型:

swoole_server::connection_list(int $start_fd = 0, int $pagesize = 10);

此函数接受2个参数,第1个参数是起始fd,第2个参数是每页取多少条,最大不得超过100.

  • 调用成功将返回一个数字索引数组,元素是取到的$fd。数组会按从小到大排序。最后一个$fd作为新的start_fd再次尝试获取
  • 调用失败返回false

示例:

$start_fd = 0;
while(true)
{
    $conn_list = $serv->connection_list($start_fd, 10);
    if($conn_list===false or count($conn_list) === 0)
    {
        echo "finishn";
        break;
    }
    $start_fd = end($conn_list);
    var_dump($conn_list);
    foreach($conn_list as $fd)
    {
        $serv->send($fd, "broadcast");
    }
}
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务