连接迭代器

最后更新于:2022-04-02 06:23:43

# 连接迭代器 `1.10.0`或更高版本,提供了监听端口迭代器,可以只遍历当前服务器端口所维持的`TCP`连接,而不是遍历所有端口的连接。 > 连接迭代器依赖`pcre`库 > > 4.1.0以上版本移除了`pcre`依赖 ~~~ $server = new swoole_websocket_server("0.0.0.0", 9514, SWOOLE_BASE); $server->set([ 'enable_static_handler' => true, 'document_root' => __DIR__ . '/web', ]); $tcp = $server->listen("0.0.0.0", 9515, SWOOLE_SOCK_TCP); $tcp->set([ 'open_length_check' => true, 'package_max_length' => 2 * 1024 * 1024, 'package_length_type' => 'N', 'package_body_offset' => 16, 'package_length_offset' => 0, ]); $server->on("open", function ($serv, $req) { echo "new WebSocket Client, fd={$req->fd}\n"; }); $tcp->on('receive', function ($server, $fd, $reactor_id, $data) { $body = substr($data, 16); $value = swoole_serialize::unpack($body); //仅遍历 9514 端口的连接 $websocket = $server->ports[0]; foreach ($websocket->connections as $_fd) { if ($server->exist($_fd)) { $server->push($_fd, json_encode($value)); } } }); $server->start(); ~~~
';