You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
47 lines
1.3 KiB
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)
|
|
}
|
|
})
|
|
}
|