吉安感知网项目-前端
罗广辉
2026-01-26 beb95fb5fc166804056abafd70fc01ac27de7621
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 集群调度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 }
}