import { createWebSocket } from 'libs/socket' import { nanoid } from 'nanoid' import { useSystemStore } from '@/stores/systemStore' const wsClient = createWebSocket() export async function sendCmd(resParams: System.SendCmdParams) { const { className, fnName, params = {} } = resParams const systemStore = useSystemStore() systemStore.updateConnected(wsClient.isConnected.value) const res = await wsClient.waitAndSend({ messageType: 'Command', fnName, className, messageId: `msg_${nanoid()}`, params, }) if (res.ackcode === 0) { return res.rely } else { throw new Error(res.message) } } export async function syncSendCmd(resParams: System.SendCmdParams) { const { className, fnName, params = {} } = resParams const systemStore = useSystemStore() systemStore.updateConnected(wsClient.isConnected.value) return await wsClient.sendRequest({ messageType: 'Command', fnName, className, messageId: `msg_${nanoid()}`, params, }) } // 业务状态订阅 export async function subscribeEvent(fromFn: string | '*', callback: (response: Socket.WebSocketResponse) => void) { wsClient.socket.addEventListener('message', (event) => { const data = JSON.parse(event.data) if (data.messageType === 'Report' && data.fromFn === fromFn) { callback && callback(data) } }) }