Home > Article > Backend Development > Does swoole's webSocket class have no connections attribute? error
Notice: Undefined property: swoole_websocket_server::$connections in /www/server.php on line 34
<?php // 官网demo $ws = new swoole_websocket_server("************", 9502); function broadcast(swoole_websocket_server $ws, $msg, $me = null) { foreach($ws->connections as $fd) { if($fd != $me) { $ws->push($fd, $msg); } } } // 监听WebSocket连接打开事件 $ws->on('open', function (swoole_websocket_server $ws, $request) { $ws->push($request->fd, "欢迎接入小孟聊天平台\n"); broadcast($ws, "用户{$request->fd}登录啦!", $request->fd); }); // 监听WebSocket消息事件 $ws->on('message', function (swoole_websocket_server $ws, $frame) { $msg = 'from'.$frame->fd.":{$frame->data}\n"; broadcast($ws, $msg); // $ws->push($frame->fd, "server: {$frame->data}"); // $ws->push($frame->fd, "server: {$frame->data}"); }); // 监听WebSocket连接关闭事件 $ws->on('close', function (swoole_websocket_server $ws, $fd) { broadcast($ws, "用户{$fd}下线啦!"); }); $ws->start();
This is code, doesn’t it use connectionsattribute
Your system lacks the pcre component, and the connections iterator depends on this library.
Installpcre-dev(ubuntu) or pcre-devel(centos)
Recompile and install swoole
I also encountered this problem when installing swoole1.8.7-beta, and then I installed pcre and it still persisted No, I think the order is wrong. You should install pcre first and then swoole. It should be OK
The above is that the webSocket class of swoole does not have the connections attribute? Error content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!