diff --git a/src/services/mockSocketService.js b/src/services/mockSocketService.js index d1b03ae8..db29b810 100644 --- a/src/services/mockSocketService.js +++ b/src/services/mockSocketService.js @@ -684,6 +684,113 @@ class MockSocketService { getMaxReconnectAttempts() { return Infinity; } + + /** + * 订阅事件推送(Mock 实现) + * @param {object} options - 订阅选项 + * @param {string} options.eventType - 事件类型 ('all' | 'policy' | 'market' | 'tech' | ...) + * @param {string} options.importance - 重要性 ('all' | 'S' | 'A' | 'B' | 'C') + * @param {Function} options.onNewEvent - 收到新事件时的回调函数 + * @param {Function} options.onSubscribed - 订阅成功的回调函数(可选) + */ + subscribeToEvents(options = {}) { + const { + eventType = 'all', + importance = 'all', + onNewEvent, + onSubscribed, + } = options; + + logger.info('mockSocketService', 'Subscribing to events', { eventType, importance }); + + // Mock: 立即触发订阅成功回调 + if (onSubscribed) { + setTimeout(() => { + onSubscribed({ + success: true, + event_type: eventType, + importance: importance, + message: 'Mock subscription confirmed' + }); + }, 100); + } + + // Mock: 如果提供了 onNewEvent 回调,监听 'new_event' 事件 + if (onNewEvent) { + // 先移除之前的监听器(避免重复) + this.off('new_event', onNewEvent); + // 添加新的监听器 + this.on('new_event', onNewEvent); + logger.info('mockSocketService', 'Event listener registered for new_event'); + } + } + + /** + * 取消订阅事件推送(Mock 实现) + * @param {object} options - 取消订阅选项 + * @param {string} options.eventType - 事件类型 + * @param {Function} options.onUnsubscribed - 取消订阅成功的回调函数(可选) + */ + unsubscribeFromEvents(options = {}) { + const { + eventType = 'all', + onUnsubscribed, + } = options; + + logger.info('mockSocketService', 'Unsubscribing from events', { eventType }); + + // Mock: 移除 new_event 监听器 + this.off('new_event'); + + // Mock: 立即触发取消订阅成功回调 + if (onUnsubscribed) { + setTimeout(() => { + onUnsubscribed({ + success: true, + event_type: eventType, + message: 'Mock unsubscription confirmed' + }); + }, 100); + } + } + + /** + * 快捷方法:订阅所有类型的事件(Mock 实现) + * @param {Function} onNewEvent - 收到新事件时的回调函数 + */ + subscribeToAllEvents(onNewEvent) { + this.subscribeToEvents({ + eventType: 'all', + importance: 'all', + onNewEvent, + }); + } + + /** + * 快捷方法:订阅指定重要性的事件(Mock 实现) + * @param {string} importance - 重要性级别 ('S' | 'A' | 'B' | 'C') + * @param {Function} onNewEvent - 收到新事件时的回调函数 + */ + subscribeToImportantEvents(importance, onNewEvent) { + this.subscribeToEvents({ + eventType: 'all', + importance, + onNewEvent, + }); + } + + /** + * 快捷方法:订阅指定类型的事件(Mock 实现) + * @param {string} eventType - 事件类型 + * @param {Function} onNewEvent - 收到新事件时的回调函数 + */ + subscribeToEventType(eventType, onNewEvent) { + this.subscribeToEvents({ + eventType, + importance: 'all', + onNewEvent, + }); + } } // 导出单例