2
0
Эх сурвалжийг харах

将socket事件名称从'exam:pushData'改为'exam:question'
更新了数据格式:
添加了roomId字段
将dailyStats.date和dailyStats.close映射到question.date和question.price
更新了相关类型定义
确保与服务器端接口完全匹配
所有功能测试通过

yourname 6 сар өмнө
parent
commit
0ce01cd963

+ 18 - 48
client/mobile/components/stock/hooks/useStockSocketClient.ts

@@ -2,25 +2,18 @@ import { useEffect, useState, useCallback } from 'react';
 import { io, Socket } from 'socket.io-client';
 import { useAuth } from '../../../hooks.tsx';
 
-interface StockPriceUpdate {
-  symbol: string;
-  price: number;
-  timestamp: Date;
-}
-
-interface ProfitSummary {
-  dailyStats: {
-    close: number;
+interface ExamData {
+  roomId: string;
+  question: {
+    date: string;
+    price: number;
   };
 }
 
 interface StockSocketClient {
   connect(): void;
   disconnect(): void;
-  subscribe(symbol: string): void;
-  unsubscribe(symbol: string): void;
-  currentPrice: number | null;
-  lastUpdate: Date | null;
+  pushExamData(data: { roomId: string; question: { date: string; price: number } }): void;
   error: Error | null;
   isConnected: boolean;
 }
@@ -28,12 +21,8 @@ interface StockSocketClient {
 export function useStockSocket(): StockSocketClient {
   const { token } = useAuth();
   const [socket, setSocket] = useState<Socket | null>(null);
-  const [currentPrice, setCurrentPrice] = useState<number | null>(null);
-  const [lastUpdate, setLastUpdate] = useState<Date | null>(null);
   const [error, setError] = useState<Error | null>(null);
   const [isConnected, setIsConnected] = useState(false);
-  const [currentDate, setCurrentDate] = useState<Date | null>(null);
-  const [profitSummary, setProfitSummary] = useState<ProfitSummary | null>(null);
 
   // 初始化socket连接
   useEffect(() => {
@@ -49,36 +38,20 @@ export function useStockSocket(): StockSocketClient {
     });
 
     newSocket.on('connect', () => {
-      console.log('Stock socket connected');
+      console.log('Exam socket connected');
       setIsConnected(true);
     });
 
     newSocket.on('disconnect', () => {
-      console.log('Stock socket disconnected');
+      console.log('Exam socket disconnected');
       setIsConnected(false);
     });
 
     newSocket.on('error', (err) => {
-      console.error('Stock socket error:', err);
+      console.error('Exam socket error:', err);
       setError(err);
     });
 
-    // 监听股票价格更新
-    newSocket.on('stock:priceUpdate', (data: StockPriceUpdate) => {
-      setCurrentPrice(data.price);
-      setLastUpdate(new Date(data.timestamp));
-    });
-
-    // 监听当前日期更新
-    newSocket.on('stock:currentDate', (date: string) => {
-      setCurrentDate(new Date(date));
-    });
-
-    // 监听收益摘要更新
-    newSocket.on('stock:profitSummary', (summary: ProfitSummary) => {
-      setProfitSummary(summary);
-    });
-
     setSocket(newSocket);
 
     return () => {
@@ -98,25 +71,22 @@ export function useStockSocket(): StockSocketClient {
     }
   }, [socket]);
 
-  const subscribe = useCallback((symbol: string) => {
-    if (socket) {
-      socket.emit('stock:subscribe', { symbol });
-    }
-  }, [socket]);
-
-  const unsubscribe = useCallback((symbol: string) => {
+  const pushExamData = useCallback((data: ExamData) => {
     if (socket) {
-      socket.emit('stock:unsubscribe', { symbol });
+      socket.emit('exam:question', {
+        roomId: data.roomId,
+        question: {
+          date: data.question.date,
+          price: data.question.price
+        }
+      });
     }
   }, [socket]);
 
   return {
     connect,
     disconnect,
-    subscribe,
-    unsubscribe,
-    currentPrice,
-    lastUpdate,
+    pushExamData,
     error,
     isConnected,
   };

+ 18 - 22
client/mobile/components/stock/stock_main.tsx

@@ -12,16 +12,13 @@ export function StockMain() {
   const {
     connect,
     disconnect,
-    subscribe,
-    unsubscribe,
-    currentPrice,
-    lastUpdate,
+    pushExamData,
     error,
     isConnected
   } = useStockSocket();
   const codeFromUrl = searchParams.get('code');
   const [stockCode, setStockCode] = useState(codeFromUrl || '001339' || undefined);//
-//   const classroom = searchParams.get('classroom');
+  const classroom = searchParams.get('classroom');
 //   const { connected } = useSocketRoom(classroom);
 //   const { sendNextQuestion } = useQuestionManagement(classroom);
   
@@ -50,10 +47,10 @@ export function StockMain() {
 
   const handleNextDay = useCallback(async () => {
     const nextDate = await moveToNextDay();
-    if (nextDate) {
+    if (nextDate && profitSummary?.dailyStats) {
       updateCurrentDate(nextDate);
     }
-  }, [moveToNextDay, updateCurrentDate]);
+  }, [moveToNextDay, updateCurrentDate, profitSummary, pushExamData]);
 
 //   useEffect(() => {
 //     const currentDate = profitSummary.dailyStats.date;
@@ -68,6 +65,19 @@ export function StockMain() {
 //     }
 //   }, [classroom, connected, profitSummary.dailyStats, sendNextQuestion]);
 
+  useEffect(() => {
+    const currentDate = profitSummary.dailyStats.date;
+    if (classroom && isConnected && currentDate ) {
+      pushExamData({
+        roomId: classroom, 
+        question: {
+          date: currentDate,
+          price: profitSummary.dailyStats.close
+        }
+      });
+    }
+  }, [classroom, isConnected, profitSummary.dailyStats ]);
+
   const handleDayNumChange = useCallback((days: number) => {
     if (!isInitialized) {
       initializeView();
@@ -103,24 +113,10 @@ export function StockMain() {
   // 管理socket连接生命周期
   useEffect(() => {
     connect();
-    if (stockCode) {
-      subscribe(stockCode);
-    }
-
     return () => {
-      if (stockCode) {
-        unsubscribe(stockCode);
-      }
       disconnect();
     };
-  }, [connect, disconnect, subscribe, unsubscribe, stockCode]);
-
-  // 同步socket数据到组件状态
-  useEffect(() => {
-    if (currentPrice && profitSummary) {
-      profitSummary.dailyStats.close = currentPrice;
-    }
-  }, [currentPrice, profitSummary]);
+  }, [connect, disconnect]);
 
   // 错误处理
   useEffect(() => {