fix(socket): 移除 SOCKET_TYPE 引用,修复构建错误
移除了移除 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 错误
This commit is contained in:
@@ -26,7 +26,6 @@ import {
|
|||||||
} from '@chakra-ui/react';
|
} from '@chakra-ui/react';
|
||||||
import { MdNotifications, MdClose, MdVolumeOff, MdVolumeUp, MdCampaign, MdTrendingUp, MdArticle, MdAssessment, MdWarning } from 'react-icons/md';
|
import { MdNotifications, MdClose, MdVolumeOff, MdVolumeUp, MdCampaign, MdTrendingUp, MdArticle, MdAssessment, MdWarning } from 'react-icons/md';
|
||||||
import { useNotification } from '../../contexts/NotificationContext';
|
import { useNotification } from '../../contexts/NotificationContext';
|
||||||
import { SOCKET_TYPE } from '../../services/socket';
|
|
||||||
import { NOTIFICATION_TYPES, PRIORITY_LEVELS } from '../../constants/notificationTypes';
|
import { NOTIFICATION_TYPES, PRIORITY_LEVELS } from '../../constants/notificationTypes';
|
||||||
|
|
||||||
const NotificationTestTool = () => {
|
const NotificationTestTool = () => {
|
||||||
@@ -295,7 +294,7 @@ const NotificationTestTool = () => {
|
|||||||
{isConnected ? 'Connected' : 'Disconnected'}
|
{isConnected ? 'Connected' : 'Disconnected'}
|
||||||
</Badge>
|
</Badge>
|
||||||
<Badge colorScheme="purple">
|
<Badge colorScheme="purple">
|
||||||
{SOCKET_TYPE}
|
REAL
|
||||||
</Badge>
|
</Badge>
|
||||||
<Badge colorScheme={getPermissionColor()}>
|
<Badge colorScheme={getPermissionColor()}>
|
||||||
浏览器: {getPermissionLabel()}
|
浏览器: {getPermissionLabel()}
|
||||||
|
|||||||
@@ -2,20 +2,15 @@
|
|||||||
/**
|
/**
|
||||||
* 通知上下文 - 管理实时消息推送和通知显示
|
* 通知上下文 - 管理实时消息推送和通知显示
|
||||||
*
|
*
|
||||||
* 环境说明:
|
* 使用真实 Socket.IO 连接到后端服务器
|
||||||
* - SOCKET_TYPE === 'REAL': 使用真实 Socket.IO 连接(生产环境),连接到 wss://valuefrontier.cn
|
* 连接地址配置在环境变量中 (REACT_APP_API_URL)
|
||||||
* - SOCKET_TYPE === 'MOCK': 使用模拟 Socket 服务(开发环境),用于本地测试
|
|
||||||
*
|
|
||||||
* 环境切换:
|
|
||||||
* - 设置 REACT_APP_ENABLE_MOCK=true 或 REACT_APP_USE_MOCK_SOCKET=true 使用 MOCK 模式
|
|
||||||
* - 否则使用 REAL 模式连接生产环境
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { createContext, useContext, useState, useEffect, useCallback, useRef } from 'react';
|
import React, { createContext, useContext, useState, useEffect, useCallback, useRef } from 'react';
|
||||||
import { useToast, Box, HStack, Text, Button, CloseButton, VStack, Icon } from '@chakra-ui/react';
|
import { useToast, Box, HStack, Text, Button, CloseButton, VStack, Icon } from '@chakra-ui/react';
|
||||||
import { BellIcon } from '@chakra-ui/icons';
|
import { BellIcon } from '@chakra-ui/icons';
|
||||||
import { logger } from '../utils/logger';
|
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 notificationSound from '../assets/sounds/notification.wav';
|
||||||
import { browserNotificationService } from '../services/browserNotificationService';
|
import { browserNotificationService } from '../services/browserNotificationService';
|
||||||
import { notificationMetricsService } from '../services/notificationMetricsService';
|
import { notificationMetricsService } from '../services/notificationMetricsService';
|
||||||
@@ -603,7 +598,7 @@ export const NotificationProvider = ({ children }) => {
|
|||||||
// 连接到 Socket 服务
|
// 连接到 Socket 服务
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
logger.info('NotificationContext', 'Initializing socket connection...');
|
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;');
|
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);
|
logger.error('NotificationContext', 'Socket connect_error', error);
|
||||||
setConnectionStatus(CONNECTION_STATUS.RECONNECTING);
|
setConnectionStatus(CONNECTION_STATUS.RECONNECTING);
|
||||||
|
|
||||||
// 获取重连次数(Real 和 Mock 都支持)
|
// 获取重连次数
|
||||||
const attempts = socket.getReconnectAttempts?.() || 0;
|
const attempts = socket.getReconnectAttempts?.() || 0;
|
||||||
setReconnectAttempt(attempts);
|
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 = () => {
|
const handleVisibilityChange = () => {
|
||||||
if (document.visibilityState === 'visible' && !isConnected && connectionStatus === CONNECTION_STATUS.FAILED) {
|
if (document.visibilityState === 'visible' && !isConnected && connectionStatus === CONNECTION_STATUS.FAILED) {
|
||||||
logger.info('NotificationContext', 'Tab refocused, attempting auto-reconnect');
|
logger.info('NotificationContext', 'Tab refocused, attempting auto-reconnect');
|
||||||
if (SOCKET_TYPE === 'REAL') {
|
|
||||||
socket.reconnect?.();
|
socket.reconnect?.();
|
||||||
} else {
|
|
||||||
socket.connect();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -828,11 +819,7 @@ export const NotificationProvider = ({ children }) => {
|
|||||||
isClosable: true,
|
isClosable: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (SOCKET_TYPE === 'REAL') {
|
|
||||||
socket.reconnect?.();
|
socket.reconnect?.();
|
||||||
} else {
|
|
||||||
socket.connect();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -864,12 +851,7 @@ export const NotificationProvider = ({ children }) => {
|
|||||||
const retryConnection = useCallback(() => {
|
const retryConnection = useCallback(() => {
|
||||||
logger.info('NotificationContext', 'Manual reconnection triggered');
|
logger.info('NotificationContext', 'Manual reconnection triggered');
|
||||||
setConnectionStatus(CONNECTION_STATUS.RECONNECTING);
|
setConnectionStatus(CONNECTION_STATUS.RECONNECTING);
|
||||||
|
|
||||||
if (SOCKET_TYPE === 'REAL') {
|
|
||||||
socket.reconnect?.();
|
socket.reconnect?.();
|
||||||
} else {
|
|
||||||
socket.connect();
|
|
||||||
}
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user