up. 优化代码内容
This commit is contained in:
parent
9e41b66e5f
commit
5a1fd83430
@ -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'),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@ -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));
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user