| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import { useEffect, useState, useCallback } from 'react';
- import { io, Socket } from 'socket.io-client';
- import { useAuth } from '../../mobile/hooks.tsx';
- interface StockPriceUpdate {
- symbol: string;
- price: number;
- timestamp: Date;
- }
- interface ProfitSummary {
- dailyStats: {
- close: number;
- };
- }
- interface StockSocketClient {
- connect(): void;
- disconnect(): void;
- subscribe(symbol: string): void;
- unsubscribe(symbol: string): void;
- currentPrice: number | null;
- lastUpdate: Date | null;
- error: Error | null;
- isConnected: boolean;
- }
- 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(() => {
- if (!token) return;
- const newSocket = io('/', {
- path: '/socket.io',
- transports: ['websocket'],
- query: { token },
- reconnection: true,
- reconnectionAttempts: 5,
- reconnectionDelay: 1000,
- });
- newSocket.on('connect', () => {
- console.log('Stock socket connected');
- setIsConnected(true);
- });
- newSocket.on('disconnect', () => {
- console.log('Stock socket disconnected');
- setIsConnected(false);
- });
- newSocket.on('error', (err) => {
- console.error('Stock 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 () => {
- newSocket.disconnect();
- };
- }, [token]);
- const connect = useCallback(() => {
- if (socket && !socket.connected) {
- socket.connect();
- }
- }, [socket]);
- const disconnect = useCallback(() => {
- if (socket && socket.connected) {
- socket.disconnect();
- }
- }, [socket]);
- const subscribe = useCallback((symbol: string) => {
- if (socket) {
- socket.emit('stock:subscribe', { symbol });
- }
- }, [socket]);
- const unsubscribe = useCallback((symbol: string) => {
- if (socket) {
- socket.emit('stock:unsubscribe', { symbol });
- }
- }, [socket]);
- return {
- connect,
- disconnect,
- subscribe,
- unsubscribe,
- currentPrice,
- lastUpdate,
- error,
- isConnected,
- };
- }
|