feat: 初始化前端项目,搭建 React + TypeScript + Vite 工程结构
- 创建 Vite + React 18 + TypeScript 项目(严格模式) - 安装 @ricky0123/vad-web、onnxruntime-web、uuid 依赖 - 定义全部 WebSocket 消息类型和数据模型(对齐接口文档) - 实现 WebSocket 连接管理(心跳保活、指数退避重连) - 实现音频编码工具(PCM↔Base64、DataURL 转换) - 创建组件骨架:CameraManager、MicManager、EdgeProcessor、WebSocketManager、ChatPanel、VideoPreview - 实现核心 useVisionSession Hook 骨架 - 配置 ESLint(_前缀变量忽略规则) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
32
frontend/src/components/WebSocketManager/index.tsx
Normal file
32
frontend/src/components/WebSocketManager/index.tsx
Normal file
@@ -0,0 +1,32 @@
|
||||
// ============================================================
|
||||
// WebSocketManager — WebSocket 连接生命周期管理
|
||||
// 职责:管理连接状态、消息分发
|
||||
// ============================================================
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { wsClient } from "../../lib/websocket";
|
||||
import type { ConnectionStatus } from "../../lib/websocket";
|
||||
import type { ServerMessage } from "../../types";
|
||||
|
||||
export function useWebSocketManager() {
|
||||
const [status, setStatus] = useState<ConnectionStatus>(wsClient.status);
|
||||
const [lastMessage, setLastMessage] = useState<ServerMessage | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const unsubStatus = wsClient.onStatusChange(setStatus);
|
||||
const unsubMessage = wsClient.onMessage(setLastMessage);
|
||||
|
||||
return () => {
|
||||
unsubStatus();
|
||||
unsubMessage();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return {
|
||||
status,
|
||||
lastMessage,
|
||||
connect: () => wsClient.connect(),
|
||||
disconnect: () => wsClient.disconnect(),
|
||||
send: wsClient.send.bind(wsClient),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user