zlwu hace 1 año
padre
commit
da5ba759da
Se han modificado 4 ficheros con 36 adiciones y 7 borrados
  1. 5 2
      README.md
  2. 11 3
      backend/config.py
  3. 15 2
      backend/main.py
  4. 5 0
      frontend/vite.config.ts

+ 5 - 2
README.md

@@ -39,8 +39,11 @@ npm run dev
 ```
 
 ## 访问
-- 前端: https://localhost:3000
-- 后端: http://localhost:8000
+- 前端: https://localhost:3000 或 https://本机IP:3000
+- 后端: https://localhost:8000 或 https://本机IP:8000
+- WebSocket: wss://localhost:8000/ws
+
+**注意**: 使用自签名证书,浏览器会提示不安全,点击"继续访问"即可
 
 ## 核心功能
 - 实时语音录制和传输

+ 11 - 3
backend/config.py

@@ -61,17 +61,25 @@ class Settings(BaseSettings):
     AUDIO_CHANNELS: int = 1
     
     # CORS配置
-    # 支持localhost和任意IP地址访问
+    # 支持localhost和任意IP地址访问(端口3000为Vite默认HTTPS端口)
     CORS_ORIGINS: list = [
+        "http://localhost:3000",
+        "https://localhost:3000",
+        "http://127.0.0.1:3000",
+        "https://127.0.0.1:3000",
         "http://localhost:5173",
         "https://localhost:5173",
         "http://127.0.0.1:5173",
         "https://127.0.0.1:5173",
         # 支持局域网IP访问
+        "http://192.168.2.112:3000",
+        "https://192.168.2.112:3000",
         "http://192.168.2.112:5173",
         "https://192.168.2.112:5173",
-        # 如有其他IP,继续添加
-        # "https://192.168.2.105:5173",
+        "http://192.168.43.35:3000",
+        "https://192.168.43.35:3000",
+        "http://192.168.43.35:5173",
+        "https://192.168.43.35:5173",
     ]
     
     # 是否允许所有来源(开发环境可设为True,生产环境务必设为False)

+ 15 - 2
backend/main.py

@@ -33,13 +33,26 @@ async def websocket_endpoint(websocket: WebSocket):
 
 if __name__ == "__main__":
     import uvicorn
+    import os
+    from pathlib import Path
+    
+    # SSL证书路径
+    ssl_dir = Path(__file__).parent.parent / "ssl"
+    ssl_keyfile = str(ssl_dir / "key.pem")
+    ssl_certfile = str(ssl_dir / "cert.pem")
+    
+    # 检查证书是否存在
+    if not os.path.exists(ssl_keyfile) or not os.path.exists(ssl_certfile):
+        print(f"警告: SSL证书不存在,请检查 {ssl_dir} 目录")
+        ssl_keyfile = None
+        ssl_certfile = None
     
     uvicorn.run(
         "main:app",
         host=settings.host,
         port=settings.port,
         reload=True,
-        ssl_keyfile=None,  # 生产环境配置SSL证书
-        ssl_certfile=None,
+        ssl_keyfile=ssl_keyfile,
+        ssl_certfile=ssl_certfile,
     )
 

+ 5 - 0
frontend/vite.config.ts

@@ -1,6 +1,7 @@
 import { defineConfig } from 'vite';
 import react from '@vitejs/plugin-react';
 import path from 'path';
+import fs from 'fs';
 
 export default defineConfig({
   plugins: [react()],
@@ -12,6 +13,10 @@ export default defineConfig({
   server: {
     port: 3000,
     host: '0.0.0.0',  // 允许外部访问
+    https: {
+      key: fs.readFileSync(path.resolve(__dirname, '../ssl/key.pem')),
+      cert: fs.readFileSync(path.resolve(__dirname, '../ssl/cert.pem')),
+    },
   },
 });