Merge branch 'feature' of https://git.valuefrontier.cn/vf/vf_react into feature
This commit is contained in:
@@ -186,6 +186,169 @@ class SocketService {
|
||||
getSocketId() {
|
||||
return this.socket?.id || null;
|
||||
}
|
||||
|
||||
// ==================== 事件推送专用方法 ====================
|
||||
|
||||
/**
|
||||
* 订阅事件推送
|
||||
* @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;
|
||||
|
||||
if (!this.socket || !this.connected) {
|
||||
logger.warn('socketService', 'Cannot subscribe: socket not connected');
|
||||
// 自动连接
|
||||
this.connect();
|
||||
// 等待连接成功后再订阅
|
||||
this.socket.once('connect', () => {
|
||||
this._doSubscribe(eventType, importance, onNewEvent, onSubscribed);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this._doSubscribe(eventType, importance, onNewEvent, onSubscribed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行订阅操作(内部方法)
|
||||
*/
|
||||
_doSubscribe(eventType, importance, onNewEvent, onSubscribed) {
|
||||
// 发送订阅请求
|
||||
this.emit('subscribe_events', {
|
||||
event_type: eventType,
|
||||
importance: importance,
|
||||
});
|
||||
|
||||
// 监听订阅确认
|
||||
this.socket.once('subscription_confirmed', (data) => {
|
||||
logger.info('socketService', 'Subscription confirmed', data);
|
||||
if (onSubscribed) {
|
||||
onSubscribed(data);
|
||||
}
|
||||
});
|
||||
|
||||
// 监听订阅错误
|
||||
this.socket.once('subscription_error', (error) => {
|
||||
logger.error('socketService', 'Subscription error', error);
|
||||
});
|
||||
|
||||
// 监听新事件推送
|
||||
if (onNewEvent) {
|
||||
// 先移除之前的监听器(避免重复)
|
||||
this.socket.off('new_event');
|
||||
// 添加新的监听器
|
||||
this.socket.on('new_event', (eventData) => {
|
||||
logger.info('socketService', 'New event received', eventData);
|
||||
onNewEvent(eventData);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订阅事件推送
|
||||
* @param {object} options - 取消订阅选项
|
||||
* @param {string} options.eventType - 事件类型
|
||||
* @param {Function} options.onUnsubscribed - 取消订阅成功的回调函数(可选)
|
||||
*/
|
||||
unsubscribeFromEvents(options = {}) {
|
||||
const {
|
||||
eventType = 'all',
|
||||
onUnsubscribed,
|
||||
} = options;
|
||||
|
||||
if (!this.socket || !this.connected) {
|
||||
logger.warn('socketService', 'Cannot unsubscribe: socket not connected');
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送取消订阅请求
|
||||
this.emit('unsubscribe_events', {
|
||||
event_type: eventType,
|
||||
});
|
||||
|
||||
// 监听取消订阅确认
|
||||
this.socket.once('unsubscription_confirmed', (data) => {
|
||||
logger.info('socketService', 'Unsubscription confirmed', data);
|
||||
|
||||
// 移除新事件监听器
|
||||
this.socket.off('new_event');
|
||||
|
||||
if (onUnsubscribed) {
|
||||
onUnsubscribed(data);
|
||||
}
|
||||
});
|
||||
|
||||
// 监听取消订阅错误
|
||||
this.socket.once('unsubscription_error', (error) => {
|
||||
logger.error('socketService', 'Unsubscription error', error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 快捷方法:订阅所有类型的事件
|
||||
* @param {Function} onNewEvent - 收到新事件时的回调函数
|
||||
* @returns {Function} 取消订阅的函数
|
||||
*/
|
||||
subscribeToAllEvents(onNewEvent) {
|
||||
this.subscribeToEvents({
|
||||
eventType: 'all',
|
||||
importance: 'all',
|
||||
onNewEvent,
|
||||
});
|
||||
|
||||
// 返回取消订阅的清理函数
|
||||
return () => {
|
||||
this.unsubscribeFromEvents({ eventType: 'all' });
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 快捷方法:订阅特定重要性的事件
|
||||
* @param {string} importance - 重要性级别 ('S' | 'A' | 'B' | 'C')
|
||||
* @param {Function} onNewEvent - 收到新事件时的回调函数
|
||||
* @returns {Function} 取消订阅的函数
|
||||
*/
|
||||
subscribeToImportantEvents(importance, onNewEvent) {
|
||||
this.subscribeToEvents({
|
||||
eventType: 'all',
|
||||
importance,
|
||||
onNewEvent,
|
||||
});
|
||||
|
||||
// 返回取消订阅的清理函数
|
||||
return () => {
|
||||
this.unsubscribeFromEvents({ eventType: 'all' });
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 快捷方法:订阅特定类型的事件
|
||||
* @param {string} eventType - 事件类型
|
||||
* @param {Function} onNewEvent - 收到新事件时的回调函数
|
||||
* @returns {Function} 取消订阅的函数
|
||||
*/
|
||||
subscribeToEventType(eventType, onNewEvent) {
|
||||
this.subscribeToEvents({
|
||||
eventType,
|
||||
importance: 'all',
|
||||
onNewEvent,
|
||||
});
|
||||
|
||||
// 返回取消订阅的清理函数
|
||||
return () => {
|
||||
this.unsubscribeFromEvents({ eventType });
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 导出单例
|
||||
|
||||
Reference in New Issue
Block a user