up. 优化代码内容

This commit is contained in:
扶桑花间 2025-08-29 22:38:21 +08:00
parent 9e41b66e5f
commit 5a1fd83430
2 changed files with 18 additions and 14 deletions

View File

@ -72,7 +72,7 @@ class GatewaySubscribe
GatewayClientService::sendToClient($put->wsClientId, new OutMessage(
WsTypeEnum::System,
WsEventEnum::Notification,
new ElNotification(title: '登录成功,欢迎您', message: "当前登录Ip: $ip"),
new ElNotification(title: '登录成功,欢迎您', message: "当前登录Ip: $ip", position: 'bottom-right'),
));
}

View File

@ -7,8 +7,9 @@ import {
WsEvent,
WsSendEventType
} from '../types';
import { computed, ref } from 'vue';
import { wsEventManager } from './event-manager';
import {computed, ref} from 'vue';
import {wsEventManager} from './event-manager';
import {systemEvent} from '@/plugins/websocket/core/event';
// 创建WebSocket实例
export function createWsInstance(config: WsConfig): WsInstance {
@ -29,7 +30,7 @@ export function createWsInstance(config: WsConfig): WsInstance {
// 内部方法
const addMessage = (content: string, type: WsMessageType) => {
messages.value.push({ content, type, timestamp: new Date() });
messages.value.push({content, type, timestamp: new Date()});
};
const addSystemMessage = (content: string) =>
@ -45,15 +46,19 @@ export function createWsInstance(config: WsConfig): WsInstance {
wsEventManager.emit(WsEvent.CONNECTED, e);
};
const handleMessage = (e: MessageEvent) =>{
addMessage(e.data, WsMessageType.INCOMING);
const handleMessage = (e: MessageEvent) => {
const jsonData = JSON.parse(e.data);
if (jsonData.event == WsEvent.LOCK_CLIENT_SCREEN) {
// 锁屏
wsEventManager.emit(WsEvent.LOCK_CLIENT_SCREEN, e);
/*
* @{type: "system", event: "事件名称", data: {}}
*/
if (jsonData.type == WsMessageType.SYSTEM) {
systemEvent.trigger(jsonData.event, jsonData.data);
}
}
// if (jsonData.event == WsEvent.LOCK_CLIENT_SCREEN) {
// // 锁屏
// wsEventManager.emit(WsEvent.LOCK_CLIENT_SCREEN, e);
// }
};
const handleError = (e: Event) => {
isConnecting.value = false;
@ -117,7 +122,7 @@ export function createWsInstance(config: WsConfig): WsInstance {
const sendMessage = (event: WsSendEventType, data: any) => {
if (!socket.value || !isConnected.value || !data) return;
const payload = JSON.stringify({ event, data });
const payload = JSON.stringify({event, data});
socket.value.send(payload);
addMessage(payload, WsMessageType.OUTGOING);
};
@ -128,7 +133,7 @@ export function createWsInstance(config: WsConfig): WsInstance {
*/
const sendSystemMessage = (event: WsSendEventType, data: any) => {
if (!socket.value || !isConnected.value || !data) return;
const payload = JSON.stringify({ type: 'system', event, data });
const payload = JSON.stringify({type: 'system', event, data});
socket.value.send(payload);
};
@ -161,4 +166,3 @@ export let wsInstance: WsInstance | null = null;
export function initWs(config: WsConfig): WsInstance {
return (wsInstance = wsInstance || createWsInstance(config));
}