swoole / rfc

Swoole 提案
116 stars 3 forks source link

RFC-1003 监听端口支持 $connections 迭代器 #9

Closed matyhtf closed 6 years ago

matyhtf commented 6 years ago

Swoole RFC: 监听端口支持 $connections 迭代器

说明

目前 Server::$connections 迭代器会遍历全部客户端连接。监听端口支持 $connections 迭代器,如只需要遍历某个端口 Accept 的连接,可使用 Port 对象的 $connections 迭代器。

代码

$serv = new swoole_server("0.0.0.0", 9501, SWOOLE_BASE);

$port2 = $serv->listen('127.0.0.1', 9503, SWOOLE_SOCK_TCP);
$port2->on('receive', function (swoole_server $serv, $fd, $reactor_id, $data) use ($port2) {
    foreach($port2->connections as $fd) {
        $serv->send($fd, "data");
    }
});

$serv->on('connect', function ($serv, $fd, $reactor_id){
    echo "[#".posix_getpid()."]\tClient@[$fd:$reactor_id]: Connect.\n";
});

$serv->on('receive', function (swoole_server $serv, $fd, $reactor_id, $data) {
    echo "[#".$serv->worker_id."]\tClient[$fd]: $data\n";
});

$serv->on('close', function ($serv, $fd, $reactor_id) {
    echo "[#".posix_getpid()."]\tClient@[$fd:$reactor_id]: Close.\n";
});

$serv->start();

影响版本