请问大家是如何保存所有链接的?json_encode提示Recursion detected

小七他哥

问题描述

使用json_encode格式化保存 $connection变量报错提示:JSON encode error: Recursion detected

程序代码或配置

......
if ($request->header('upgrade') === 'websocket') {
        $connection->protocol = Websocket::class;
        Websocket::input($request, $connection);
        if ($sid = $request->get('sid')) {
            $connectionList = WsService::getConnectionList();

            $connection->sid = $sid;
            $connection->num = (isset($connectionList[$sid]) ? count($connectionList[$sid]) : 0) + 1;
            //告诉客户端当前是相同用户id下的第几个链接
            $connection->send(WsService::getMessage('init', '', ['num' => $connection->num]));

            $connectionList[$sid][$connection->num] = $connection;

            try {
                $json = json_encode($connectionList, JSON_THROW_ON_ERROR);
                $globalDataClient->connectionList = $json;//上面语句返回false,导致这里存的是空
            } catch (JsonException $e) {
                echo 'JSON encode error: ',  $e->getMessage();  // 输出: JSON encode error: Recursion detected
            }

            echo '<pre>';print_r(WsService::getConnectionList());
        }
        return false;
    }
......

WsServer 的 getConnectionList 方法如下:

public static function getConnectionList() {
        global $globalDataClient;

        $connectionList = $globalDataClient->connectionList;
        return $connectionList ? unserialize($connectionList) : [];
    }

操作系统环境及workerman/webman等具体版本

ubuntu 系统 + workerman 4.1.16 版本

178 1 0
1个回答

walkor 打赏

连接对象不支持序列化

  • 暂无评论
×
🔝