首页 > 后端开发 > php教程 > swoole 的server.php全局变量数据只能放两个?

swoole 的server.php全局变量数据只能放两个?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-08-04 09:19:15
原创
1305 人浏览过

一直只有两个
swoole 的server.php全局变量数据只能放两个?
开了四个客户端了。。。

<code><?php // 官网demo
$ws = new swoole_websocket_server("***********", 9502);
$fds = [];

/**
 * 广播发送
 * @param swoole_websocket_server $ws
 * @param $msg
 * @param $fds
 * @param null $me
 */
function broadcast(swoole_websocket_server $ws, $msg, $fds , $me = null) {
    foreach($fds as $fd) {
        if($fd != $me) {
            $ws->push($fd, $msg);
        }
    }
}

// 监听WebSocket连接打开事件
$ws->on('open', function (swoole_websocket_server $ws, $request) use(&$fds) {
    $fds[$request->fd] = $request->fd;
    $ws->push($request->fd, "欢迎接入小孟聊天平台\n");
    broadcast($ws, "用户{$request->fd}登录啦!", $fds, $request->fd);
    var_dump($fds);
});

// 监听WebSocket消息事件
$ws->on('message', function (swoole_websocket_server $ws, $frame) use(&$fds) {
    $msg =  'from'.$frame->fd.":{$frame->data}\n";
    broadcast($ws, $msg, $fds, $frame->fd);
   // $ws->push($frame->fd, "server: {$frame->data}");
    // $ws->push($frame->fd, "server: {$frame->data}");
});

// 监听WebSocket连接关闭事件
$ws->on('close', function (swoole_websocket_server $ws, $fd) use(&$fds) {
    unset($fds[$fd]);
    broadcast($ws, "用户{$fd}下线啦!", $fds);
});


$ws->start();</code>
登录后复制
登录后复制

回复内容:

一直只有两个
swoole 的server.php全局变量数据只能放两个?
开了四个客户端了。。。

<code><?php // 官网demo
$ws = new swoole_websocket_server("***********", 9502);
$fds = [];

/**
 * 广播发送
 * @param swoole_websocket_server $ws
 * @param $msg
 * @param $fds
 * @param null $me
 */
function broadcast(swoole_websocket_server $ws, $msg, $fds , $me = null) {
    foreach($fds as $fd) {
        if($fd != $me) {
            $ws->push($fd, $msg);
        }
    }
}

// 监听WebSocket连接打开事件
$ws->on('open', function (swoole_websocket_server $ws, $request) use(&$fds) {
    $fds[$request->fd] = $request->fd;
    $ws->push($request->fd, "欢迎接入小孟聊天平台\n");
    broadcast($ws, "用户{$request->fd}登录啦!", $fds, $request->fd);
    var_dump($fds);
});

// 监听WebSocket消息事件
$ws->on('message', function (swoole_websocket_server $ws, $frame) use(&$fds) {
    $msg =  'from'.$frame->fd.":{$frame->data}\n";
    broadcast($ws, $msg, $fds, $frame->fd);
   // $ws->push($frame->fd, "server: {$frame->data}");
    // $ws->push($frame->fd, "server: {$frame->data}");
});

// 监听WebSocket连接关闭事件
$ws->on('close', function (swoole_websocket_server $ws, $fd) use(&$fds) {
    unset($fds[$fd]);
    broadcast($ws, "用户{$fd}下线啦!", $fds);
});


$ws->start();</code>
登录后复制
登录后复制
相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
php - swoole 扩展和 swoole 框架有什么区别?
来自于 1970-01-01 08:00:00
0
0
0
能不能出swoole,hyperf,workerman的相关课程啊
来自于 1970-01-01 08:00:00
0
0
0
nginx 反向代理 swoole的设置
来自于 1970-01-01 08:00:00
0
0
0
php - 请教一个swoole websocket的问题
来自于 1970-01-01 08:00:00
0
0
0
php - 请问现在Swoole的最新稳定版是哪个版本
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板