Files
vf_react/src/services/socket/index.js
zdl eebd207276 fix(socket): 暴露 Socket 实例到 window 对象,修复生产环境事件监听器失效问题
## 问题
生产环境收到 WebSocket 消息但不触发通知:
- Network 面板显示消息已接收
- 但事件监听器未触发(事件处理函数不执行)
- 手动测试 `window.__TEST_NOTIFICATION__.testAllTypes()` 正常工作
- 诊断脚本显示 `window.socket: undefined`

## 根本原因
Socket 实例未暴露到全局作用域,导致:
1. 无法验证 NotificationContext 中的监听器是否注册在正确的 Socket 实例上
2. 可能存在多个 Socket 实例(导入的实例 vs 实际连接的实例)
3. 事件监听器注册在错误的实例上

## 解决方案
在 `src/services/socket/index.js` 中暴露 Socket 实例到 window 对象:

### 代码变更
```javascript
//  新增:暴露 Socket 实例到 window(用于调试和验证)
if (typeof window !== 'undefined') {
    window.socket = socketService;
    window.socketService = socketService;

    console.log(' Socket instance exposed to window');
    console.log('  📍 window.socket:', window.socket);
    console.log('  📍 Socket.IO instance:', window.socket?.socket);
    console.log('  📍 Connection status:', window.socket?.connected);
}
```

## 好处
1. **可调试性**: 可在浏览器 Console 直接访问 Socket 实例
2. **验证监听器**: 可检查 `window.socket.socket._callbacks` 确认监听器已注册
3. **诊断连接**: 可实时查看 `window.socket.connected` 状态
4. **手动测试**: 可通过 `window.socket.emit()` 手动触发事件

## 验证步骤
部署后在浏览器 Console 执行:
```javascript
// 1. 验证 Socket 实例已暴露
console.log(window.socket);

// 2. 检查连接状态
console.log('Connected:', window.socket.connected);

// 3. 检查监听器
console.log('Listeners:', window.socket.socket._callbacks);

// 4. 测试手动触发事件
window.socket.socket.emit('new_event', { id: 999, title: 'Test' });
```

## 影响范围
- 修改文件: `src/services/socket/index.js`(1 个文件)
- 影响范围: 仅新增调试功能,不改变业务逻辑
- 风险等级: 低(只读暴露,不修改 Socket 行为)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-11 13:59:23 +08:00

35 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// src/services/socket/index.js
/**
* Socket 服务统一导出
* 使用真实 Socket.IO 服务连接后端
*/
import { socketService } from '../socketService';
// 导出 socket 服务
export const socket = socketService;
export { socketService };
// ⚡ 新增:暴露 Socket 实例到 window用于调试和验证
if (typeof window !== 'undefined') {
window.socket = socketService;
window.socketService = socketService;
console.log(
'%c[Socket Service] ✅ Socket instance exposed to window',
'color: #4CAF50; font-weight: bold; font-size: 14px;'
);
console.log(' 📍 window.socket:', window.socket);
console.log(' 📍 window.socketService:', window.socketService);
console.log(' 📍 Socket.IO instance:', window.socket?.socket);
console.log(' 📍 Connection status:', window.socket?.connected ? '✅ Connected' : '❌ Disconnected');
}
// 打印当前使用的服务类型
console.log(
'%c[Socket Service] Using REAL Socket Service',
'color: #4CAF50; font-weight: bold; font-size: 12px;'
);
export default socket;