Files
mayfly-go/frontend/src/common/syssocket.ts

108 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-12-08 13:04:23 +08:00
import { getToken } from '@/common/utils/storage';
import { createWebSocket } from './request';
2024-12-08 13:04:23 +08:00
import { ElNotification } from 'element-plus';
import { MsgSubtypeEnum } from './commonEnum';
import EnumValue from './Enum';
import { h } from 'vue';
import { MessageRenderer } from '@/components/message/message';
class SysSocket {
/**
* socket连接
*/
socket: WebSocket | null = null;
/**
* key -> value ->
*/
categoryHandlers: Map<string, any> = new Map();
/**
* websocket
*/
async init() {
// 存在则不需要重新建立连接
if (this.socket) {
return;
}
const token = getToken();
if (!token) {
return null;
}
2023-10-20 21:31:46 +08:00
console.log('init system ws');
try {
this.socket = await createWebSocket('/sysmsg');
this.socket.onmessage = async (event: { data: string }) => {
2023-12-27 19:55:36 +08:00
let message;
try {
message = JSON.parse(event.data);
} catch (e) {
console.error('解析ws消息失败', e);
return;
}
// 存在消息类别对应的处理器,则进行处理,否则进行默认通知处理
const handler = this.categoryHandlers.get(message.category);
if (handler) {
handler(message);
return;
}
const msgSubtype = EnumValue.getEnumByValue(MsgSubtypeEnum, message.subtype);
if (!msgSubtype) {
console.log(`not found msg subtype: ${message.subtype}`);
return;
}
// 动态导入 i18n 或延迟获取 i18n 实例
let title = '';
try {
// 方式1: 动态导入
const { i18n } = await import('@/i18n');
title = i18n.global.t(msgSubtype?.label);
} catch (e) {
console.warn('i18n not ready, using default title');
}
ElNotification({
duration: 0,
title,
message: h(MessageRenderer, { content: message.msg }),
type: msgSubtype?.extra.notifyType || 'info',
});
};
} catch (e) {
console.error('open system ws error', e);
}
}
destory() {
2023-12-28 17:21:33 +08:00
this.socket?.close();
this.socket = null;
2023-12-28 17:21:33 +08:00
this.categoryHandlers?.clear();
}
/**
*
*
* @param category
* @param handlerFunc
*/
async registerMsgHandler(category: any, handlerFunc: any) {
if (this.categoryHandlers.has(category)) {
console.log(`${category}该类别消息处理器已存在...`);
return;
}
if (typeof handlerFunc != 'function') {
throw new Error('message handler需为函数');
}
this.categoryHandlers.set(category, handlerFunc);
}
}
// 全局系统消息websocket;
const sysSocket = new SysSocket();
export default sysSocket;