import { getWebsocketUrl } from '@/websocket/util/config'
|
import { useConnectWebSocket } from '@/utils/websocket/connect-websocket'
|
|
/**
|
* 使用无人机WebSocket连接,根据工作区ID动态管理WebSocket连接。
|
*
|
* @param {Ref<string>} workspaceIdRef - 工作区ID的引用,用于动态更新WebSocket连接。
|
* @returns {Object} - 包含wsInfo和removeWS的响应式对象。
|
*/
|
export const useDroneWS = workspaceIdRef => {
|
const wsInfo = ref({})
|
let droneWebSocket = null
|
|
// ws消息钩子 todo后面加节流参数处理
|
const messageHandler = result => {
|
let payload = JSON.parse(result)
|
if (payload.biz_code) {
|
wsInfo.value[payload.biz_code] = payload
|
}
|
}
|
|
function init() {
|
let webSocketUrl = getWebsocketUrl() + '&workspace-id=' + workspaceIdRef.value
|
// 监听ws 消息
|
droneWebSocket = useConnectWebSocket(messageHandler, webSocketUrl)
|
}
|
|
watch(workspaceIdRef, (newValue, oldValue) => {
|
removeWS()
|
if (workspaceIdRef.value) {
|
init()
|
}
|
})
|
|
// 移除ws 事件
|
function removeWS() {
|
droneWebSocket?.close()
|
droneWebSocket = null
|
wsInfo.value = {}
|
}
|
|
onBeforeUnmount(() => {
|
removeWS()
|
})
|
|
return {wsInfo,removeWS}
|
}
|