up. 优化代码内容
This commit is contained in:
parent
f5d587a276
commit
b1455cf70f
@ -121,6 +121,16 @@ export function createWsInstance(config: WsConfig): WsInstance {
|
||||
socket.value.send(payload);
|
||||
addMessage(payload, WsMessageType.OUTGOING);
|
||||
};
|
||||
/**
|
||||
* 推送系统消息
|
||||
* @param event
|
||||
* @param data
|
||||
*/
|
||||
const sendSystemMessage = (event: WsSendEventType, data: any) => {
|
||||
if (!socket.value || !isConnected.value || !data) return;
|
||||
const payload = JSON.stringify({ type: 'system', event, data });
|
||||
socket.value.send(payload);
|
||||
};
|
||||
|
||||
const clearMessages = () => (messages.value = []);
|
||||
|
||||
@ -139,6 +149,7 @@ export function createWsInstance(config: WsConfig): WsInstance {
|
||||
connectSocket,
|
||||
disconnectSocket,
|
||||
sendMessage,
|
||||
sendSystemMessage,
|
||||
clearMessages
|
||||
};
|
||||
}
|
||||
|
||||
@ -5,6 +5,10 @@ export enum WsEvent {
|
||||
CONNECTED = 'CONNECTED',
|
||||
LOCK_CLIENT_SCREEN = 'LOCK_CLIENT_SCREEN'
|
||||
}
|
||||
export enum WsSystemEvent {
|
||||
LOGIN = 'login'
|
||||
}
|
||||
|
||||
|
||||
export enum WsSendEventType {
|
||||
BIND_CONNECT_ID = 'bind_connect_id',
|
||||
@ -61,6 +65,9 @@ export interface WsInstance {
|
||||
|
||||
connectSocket: () => void;
|
||||
disconnectSocket: () => void;
|
||||
/** 发送系统消息(跟踪系统事件) **/
|
||||
sendSystemMessage: (event: WsSystemEvent, data: any) => void;
|
||||
|
||||
sendMessage: (event: WsSendEventType, data: any) => void;
|
||||
clearMessages: () => void;
|
||||
events: WsEventManager; // 重命名为更清晰的events
|
||||
|
||||
@ -9,15 +9,14 @@ import type { User } from '@/api/system/user/model';
|
||||
import type { Menu } from '@/api/system/menu/model';
|
||||
import type { DictionaryData } from '@/api/system/dictionary-data/model';
|
||||
import { getUserInfo } from '@/api/layout';
|
||||
import {getClientInfo, getToken} from '@/utils/token-util';
|
||||
import { getToken } from '@/utils/token-util';
|
||||
import {
|
||||
WsEventContext,
|
||||
wsEventManager,
|
||||
wsInstance,
|
||||
WsEvent,
|
||||
WsInstance,
|
||||
WsSendEventType,
|
||||
wsInstance
|
||||
WsSystemEvent
|
||||
} from '@/plugins/websocket';
|
||||
import { useClientStore } from '@/store/modules/client';
|
||||
|
||||
/** 直接指定菜单数据 */
|
||||
const USER_MENUS: Menu[] | null = null;
|
||||
@ -74,18 +73,15 @@ export const useUserStore = defineStore('user', {
|
||||
})
|
||||
);
|
||||
this.setMenus(menus);
|
||||
|
||||
console.log('管理器塞事件进去');
|
||||
wsEventManager.subscribe(WsEvent.CONNECTED, (sub: WsEventContext) => {
|
||||
console.log('事件执行, 绑定连接和用户关系');
|
||||
// 绑定用户到websocket的connect_id中
|
||||
const clientInfo = getClientInfo();
|
||||
wsInstance.sendMessage(WsSendEventType.BIND_CONNECT_ID, {
|
||||
// 订阅客户端连接事件
|
||||
wsEventManager.subscribe(WsEvent.CONNECTED, () => {
|
||||
// Ws登录授权
|
||||
const clientStore = useClientStore();
|
||||
wsInstance?.sendSystemMessage(WsSystemEvent.LOGIN, {
|
||||
token: getToken(),
|
||||
userId: result.userId,
|
||||
clientId: clientInfo.id,
|
||||
clientName: clientInfo.name,
|
||||
clientVersion: clientInfo.version
|
||||
clientId: clientStore.clientId,
|
||||
clientName: clientStore.clientName,
|
||||
clientVersion: clientStore.clientVersion
|
||||
});
|
||||
});
|
||||
return { menus, homePath };
|
||||
|
||||
Loading…
Reference in New Issue
Block a user