当前位置: 首页 > 工具软件 > laravel-s > 使用案例 >

Laravel使用hhxsv5/laravel-s(Swoole适配器)实现广播

郜卓君
2023-12-01

1.hhxsv5/laravel-s适配器github地址: 

https://github.com/hhxsv5/laravel-s

2.主要代码

        /**@var \Swoole\Http\Server|\Swoole\WebSocket\Server $swoole */
        $swoole = app('swoole');
        // $swoole->ports:遍历所有Port对象,https://wiki.swoole.com/#/server/properties?id=ports
        $port = $swoole->ports[1]; // 获得`Swoole\Server\Port`对象,$port[0]是主服务器的端口
        foreach ($port->connections as $fd) { // 遍历所有连接
            // $swoole->send($fd, 'Send tcp message');
            // if($swoole->isEstablished($fd)) {
            //     $swoole->push($fd, 'Send websocket message');
            // }
        }

3.说明

  • 需要使用swoole代替php-fpm,也就是说需要修改nginx配置文件,详见github上的文档.
  • 上述代码$port = $swoole->ports[1];中的1为文档示例,需要根据实际情况填写,以确定需要广播的websocket(端口)链接.
 类似资料: