WIN-2308041133\Administrator před 2 týdny
rodič
revize
28e0a4af5d
2 změnil soubory, kde provedl 59 přidání a 7 odebrání
  1. 10 2
      app/services/workerman/chat/ChatHandle.php
  2. 49 5
      socket.php

+ 10 - 2
app/services/workerman/chat/ChatHandle.php

@@ -61,9 +61,16 @@ class ChatHandle
         ];
 
         $connection->user = (object)$userInfo;
+        
+        // 从 URL 参数获取聊天对象 UID(在 socket.php onWebSocketConnect 中设置)
+        $connection->chatToUid = $connection->to_uid ?? 0;
+        
         $this->service->setUser($connection);
 
-        return $response->success();
+        return $response->success([
+            'msg' => '登录成功',
+            'to_uid' => $connection->chatToUid,  // 返回当前聊天对象
+        ]);
     }
 
     /**
@@ -71,7 +78,8 @@ class ChatHandle
      */
     public function chat(TcpConnection &$connection, array $res, Response $response)
     {
-        $to_uid = $res['data']['to_uid'] ?? 0;
+        // 优先从消息数据获取 to_uid,其次从 URL 参数获取(聊天对象UID)
+        $to_uid = $res['data']['to_uid'] ?? $connection->chatToUid ?? 0;
         $msn = $res['data']['msn'] ?? '';
         $msn_type = $res['data']['type'] ?? 1; // 1=文字, 2=语音, 3=图片
 

+ 49 - 5
socket.php

@@ -1,6 +1,6 @@
 <?php
 /**
- * WebSocket 服务端启动文件
+ * WebSocket 服务端启动文件 (WSS 加密版)
  * 
  * 使用方法:
  *   php socket.php start          # 调试模式启动(前台运行)
@@ -8,6 +8,9 @@
  *   php socket.php stop           # 停止服务
  *   php socket.php restart        # 重启服务
  *   php socket.php status         # 查看运行状态
+ * 
+ * 客户端连接地址:
+ *   wss://api.myjie.cn:2345?type=user&token=xxx&form_type=3&to_uid=对方UID
  */
 
 require __DIR__ . '/vendor/autoload.php';
@@ -21,8 +24,30 @@ if (!is_dir(LOG_PATH)) {
     mkdir(LOG_PATH, 0755, true);
 }
 
-// 创建 WebSocket 服务
-$ws_worker = new Worker('websocket://0.0.0.0:2345');
+// ========== SSL 证书配置(WSS 必须) ==========
+// 宝塔证书默认文件名:fullchain.pem(证书)、privkey.pem(私钥)
+$ssl_cert_path = __DIR__ . '/config/ssl/fullchain.pem';    // 证书文件
+$ssl_key_path  = __DIR__ . '/config/ssl/privkey.pem';     // 私钥文件
+
+$context = [];
+if (file_exists($ssl_cert_path) && file_exists($ssl_key_path)) {
+    $context = [
+        'ssl' => [
+            'local_cert'  => $ssl_cert_path,
+            'local_pk'    => $ssl_key_path,
+            'verify_peer' => false,
+        ]
+    ];
+}
+// ==============================================
+
+// 创建 WebSocket 服务(带 SSL 上下文)
+$ws_worker = new Worker('websocket://0.0.0.0:2345', $context);
+
+// 启用 SSL 传输层(使 websocket:// 升级为 wss://)
+if (!empty($context)) {
+    $ws_worker->transport = 'ssl';
+}
 
 // 设置进程名称
 $ws_worker->name = 'ChatWebSocket';
@@ -49,6 +74,20 @@ $ws_worker->onConnect = function(TcpConnection $connection) {
     echo "[Connect] {$addr}\n";
 };
 
+// WebSocket 握手回调 - 解析 URL 查询参数(包括 to_uid)
+$ws_worker->onWebSocketConnect = function(TcpConnection $connection) {
+    // 从 URL 查询字符串解析参数
+    // 例如: wss://api.myjie.cn:2345?type=user&token=xxx&form_type=3&to_uid=456
+    $query = $_GET ?? [];
+    
+    $connection->type      = $query['type'] ?? 'user';
+    $connection->token     = $query['token'] ?? '';
+    $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";
+};
+
 // 当收到客户端消息时
 $ws_worker->onMessage = function(TcpConnection $connection, $data) use (&$chatService) {
     $chatService->onMessage($connection, $data);
@@ -65,10 +104,15 @@ $ws_worker->onError = function(TcpConnection $connection, $code, $msg) {
 };
 
 // 输出启动信息
+$ssl_ok = !empty($context);
 echo "\n";
 echo "========================================\n";
-echo "  Chat WebSocket Server\n";
-echo "  Address: ws://0.0.0.0:2345\n";
+echo "  Chat WebSocket Server (WSS)\n";
+echo "  Address: " . ($ssl_ok ? "wss" : "ws") . "://0.0.0.0:2345\n";
+if (!$ssl_ok) {
+    echo "  [警告] SSL 证书未配置,运行在 ws 模式\n";
+    echo "  请将证书放到 config/ssl/cert.pem 和 config/ssl/key.pem\n";
+}
 echo "  PID: " . getmypid() . "\n";
 echo "========================================\n\n";