diff --git a/src/components/NotificationTestTool/index.js b/src/components/NotificationTestTool/index.js index 51f09b64..b70f2bc5 100644 --- a/src/components/NotificationTestTool/index.js +++ b/src/components/NotificationTestTool/index.js @@ -26,7 +26,6 @@ import { } from '@chakra-ui/react'; import { MdNotifications, MdClose, MdVolumeOff, MdVolumeUp, MdCampaign, MdTrendingUp, MdArticle, MdAssessment, MdWarning } from 'react-icons/md'; import { useNotification } from '../../contexts/NotificationContext'; -import { SOCKET_TYPE } from '../../services/socket'; import { NOTIFICATION_TYPES, PRIORITY_LEVELS } from '../../constants/notificationTypes'; const NotificationTestTool = () => { @@ -295,7 +294,7 @@ const NotificationTestTool = () => { {isConnected ? 'Connected' : 'Disconnected'} - {SOCKET_TYPE} + REAL 浏览器: {getPermissionLabel()} diff --git a/src/contexts/NotificationContext.js b/src/contexts/NotificationContext.js index b7cb6528..e2e9d55e 100644 --- a/src/contexts/NotificationContext.js +++ b/src/contexts/NotificationContext.js @@ -2,20 +2,15 @@ /** * 通知上下文 - 管理实时消息推送和通知显示 * - * 环境说明: - * - SOCKET_TYPE === 'REAL': 使用真实 Socket.IO 连接(生产环境),连接到 wss://valuefrontier.cn - * - SOCKET_TYPE === 'MOCK': 使用模拟 Socket 服务(开发环境),用于本地测试 - * - * 环境切换: - * - 设置 REACT_APP_ENABLE_MOCK=true 或 REACT_APP_USE_MOCK_SOCKET=true 使用 MOCK 模式 - * - 否则使用 REAL 模式连接生产环境 + * 使用真实 Socket.IO 连接到后端服务器 + * 连接地址配置在环境变量中 (REACT_APP_API_URL) */ import React, { createContext, useContext, useState, useEffect, useCallback, useRef } from 'react'; import { useToast, Box, HStack, Text, Button, CloseButton, VStack, Icon } from '@chakra-ui/react'; import { BellIcon } from '@chakra-ui/icons'; import { logger } from '../utils/logger'; -import socket, { SOCKET_TYPE } from '../services/socket'; +import socket from '../services/socket'; import notificationSound from '../assets/sounds/notification.wav'; import { browserNotificationService } from '../services/browserNotificationService'; import { notificationMetricsService } from '../services/notificationMetricsService'; @@ -603,7 +598,7 @@ export const NotificationProvider = ({ children }) => { // 连接到 Socket 服务 useEffect(() => { logger.info('NotificationContext', 'Initializing socket connection...'); - console.log(`%c[NotificationContext] Initializing socket (type: ${SOCKET_TYPE})`, 'color: #673AB7; font-weight: bold;'); + console.log('%c[NotificationContext] Initializing socket connection', 'color: #673AB7; font-weight: bold;'); // ✅ 第一步: 注册所有事件监听器 console.log('%c[NotificationContext] Step 1: Registering event listeners...', 'color: #673AB7;'); @@ -665,10 +660,10 @@ export const NotificationProvider = ({ children }) => { logger.error('NotificationContext', 'Socket connect_error', error); setConnectionStatus(CONNECTION_STATUS.RECONNECTING); - // 获取重连次数(Real 和 Mock 都支持) + // 获取重连次数 const attempts = socket.getReconnectAttempts?.() || 0; setReconnectAttempt(attempts); - logger.info('NotificationContext', 'Reconnection attempt', { attempts, socketType: SOCKET_TYPE }); + logger.info('NotificationContext', 'Reconnection attempt', { attempts }); }); // 监听重连失败 @@ -798,11 +793,7 @@ export const NotificationProvider = ({ children }) => { const handleVisibilityChange = () => { if (document.visibilityState === 'visible' && !isConnected && connectionStatus === CONNECTION_STATUS.FAILED) { logger.info('NotificationContext', 'Tab refocused, attempting auto-reconnect'); - if (SOCKET_TYPE === 'REAL') { - socket.reconnect?.(); - } else { - socket.connect(); - } + socket.reconnect?.(); } }; @@ -828,11 +819,7 @@ export const NotificationProvider = ({ children }) => { isClosable: true, }); - if (SOCKET_TYPE === 'REAL') { - socket.reconnect?.(); - } else { - socket.connect(); - } + socket.reconnect?.(); } }; @@ -864,12 +851,7 @@ export const NotificationProvider = ({ children }) => { const retryConnection = useCallback(() => { logger.info('NotificationContext', 'Manual reconnection triggered'); setConnectionStatus(CONNECTION_STATUS.RECONNECTING); - - if (SOCKET_TYPE === 'REAL') { - socket.reconnect?.(); - } else { - socket.connect(); - } + socket.reconnect?.(); }, []); /**