From db7efeb1feeb15bb0a7e25d200f202df8e6f8af9 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Mon, 10 Nov 2025 17:39:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(socket):=20=E7=A7=BB=E9=99=A4=20SOCKET=5FTY?= =?UTF-8?q?PE=20=E5=BC=95=E7=94=A8=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除了移除 Mock Socket 后遗留的 SOCKET_TYPE 导出引用。 ## 修改文件 - src/contexts/NotificationContext.js - 删除 SOCKET_TYPE 导入 - 更新文件注释,删除 Mock/Real 模式说明 - 简化日志输出,删除 socket 类型显示 - 移除所有 SOCKET_TYPE 条件判断 - 统一使用 socket.reconnect?.() 重连逻辑 - src/components/NotificationTestTool/index.js - 删除 SOCKET_TYPE 导入 - Badge 固定显示 "REAL" ## 问题修复 - ✅ 修复构建错误: "SOCKET_TYPE is not exported from '../services/socket'" - ✅ 简化重连逻辑,不再需要区分 Mock/Real 模式 - ✅ 代码更简洁,移除了 9 处过时引用 ## 验证 - npm run build 构建成功 - 无 TypeScript 错误 - 无 import 错误 --- src/components/NotificationTestTool/index.js | 3 +- src/contexts/NotificationContext.js | 36 +++++--------------- 2 files changed, 10 insertions(+), 29 deletions(-) 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?.(); }, []); /**