使用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) : [];
}
ubuntu 系统 + workerman 4.1.16 版本
连接对象不支持序列化