|
|
@@ -75,10 +75,10 @@ $ws_worker->onConnect = function(TcpConnection $connection) {
|
|
|
echo "[Connect] {$addr}\n";
|
|
|
};
|
|
|
|
|
|
-// WebSocket 握手回调 - 解析 URL 查询参数(包括 to_uid)
|
|
|
-$ws_worker->onWebSocketConnect = function(TcpConnection $connection) {
|
|
|
+// WebSocket 握手回调 - 解析 URL 查询参数(包括 to_uid)并自动登录
|
|
|
+$ws_worker->onWebSocketConnect = function(TcpConnection $connection) use ($ws_worker) {
|
|
|
// 从 URL 查询字符串解析参数
|
|
|
- // 例如: wss://api.myjie.cn:2345?type=user&token=xxx&form_type=3&to_uid=456
|
|
|
+ // 例如: wss://api.myjie.cn:2345?type=user&token=xxx&from=3&to_uid=456
|
|
|
$query = $_GET ?? [];
|
|
|
|
|
|
$connection->type = $query['type'] ?? 'user';
|
|
|
@@ -86,7 +86,21 @@ $ws_worker->onWebSocketConnect = function(TcpConnection $connection) {
|
|
|
$connection->form_type = $query['form_type'] ?? 3;
|
|
|
$connection->to_uid = isset($query['to_uid']) ? intval($query['to_uid']) : 0; // 聊天对象UID
|
|
|
|
|
|
- echo "[WebSocket Connect] type={$connection->type}, to_uid={$connection->to_uid}\n";
|
|
|
+ // 自动登录:根据 URL 参数设置用户信息
|
|
|
+ if ($connection->token) {
|
|
|
+ // TODO: 实际项目中应验证 token 有效性
|
|
|
+ // 从 URL 参数获取用户 UID(from 或 uid 参数)
|
|
|
+ $uid = intval($query['from'] ?? $query['uid'] ?? $connection->form_type ?? 0);
|
|
|
+ $connection->user = (object)[
|
|
|
+ 'uid' => $uid,
|
|
|
+ 'nickname' => $query['nickname'] ?? '用户',
|
|
|
+ 'avatar' => $query['avatar'] ?? '',
|
|
|
+ ];
|
|
|
+ $connection->chatToUid = $connection->to_uid;
|
|
|
+ $ws_worker->chatService->setUser($connection);
|
|
|
+ }
|
|
|
+
|
|
|
+ echo "[WebSocket Connect] type={$connection->type}, uid={$connection->user->uid ?? 0}, to_uid={$connection->to_uid}\n";
|
|
|
};
|
|
|
|
|
|
// 当收到客户端消息时
|