>  기사  >  백엔드 개발  >  swoole 的server.php全局变量数据只能放两个?

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

WBOY
WBOY원래의
2016-08-04 09:19:151209검색

一直只有两个
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으로 문의하세요.