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