Home > Backend Development > PHP Tutorial > Does swoole's webSocket class have no connections attribute? error

Does swoole's webSocket class have no connections attribute? error

PHP中文网
Release: 2023-03-01 06:00:01
Original
1601 people have browsed it


Notice: Undefined property: swoole_websocket_server::$connections in /www/server.php on line 34

Does swooles webSocket class have no connections attribute? error

Reply content:

<?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(&#39;open&#39;, function (swoole_websocket_server $ws, $request) {
    $ws->push($request->fd, "欢迎接入小孟聊天平台\n");
    broadcast($ws, "用户{$request->fd}登录啦!", $request->fd);
});

// 监听WebSocket消息事件
$ws->on(&#39;message&#39;, function (swoole_websocket_server $ws, $frame) {
    $msg =  &#39;from&#39;.$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(&#39;close&#39;, function (swoole_websocket_server $ws, $fd) {
    broadcast($ws, "用户{$fd}下线啦!");
});


$ws->start();
Copy after login

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

Does swoole's webSocket class have no connections attribute? errorDoes swooles webSocket class have no connections attribute? error

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template