Commit 738ef8d7ec782b26cbe91b84814391181b0672f7
1 parent
8e013295
fix(src/hooks/): 修复ws绑定组件,然后删除组件,ws还在发送信息问题
Showing
1 changed file
with
37 additions
and
11 deletions
1 | 1 | import { RequestContentTypeEnum } from "@/enums/external/httpEnum"; |
2 | 2 | import { CreateComponentType } from "@/packages/index.d"; |
3 | -import { useSocketStore } from "@/store/external/modules/socketStore"; | |
4 | -import { SocketReceiveMessageType } from "@/store/external/modules/socketStore.d"; | |
3 | +import { useSocketStore} from "@/store/external/modules/socketStore"; | |
4 | +import { SocketReceiveMessageType,SocketSendMessageType } from "@/store/external/modules/socketStore.d"; | |
5 | 5 | import { useChartEditStore } from "@/store/modules/chartEditStore/chartEditStore"; |
6 | 6 | import { getJwtToken, getShareJwtToken } from "@/utils/external/auth"; |
7 | 7 | import { useWebSocket, WebSocketResult } from "@vueuse/core"; |
... | ... | @@ -10,14 +10,20 @@ import { ExtraRequestConfigType } from "@/store/external/modules/extraComponentI |
10 | 10 | import { isShareMode } from "@/views/share/hook"; |
11 | 11 | |
12 | 12 | |
13 | + | |
13 | 14 | interface SocketConnectionPoolType { |
14 | 15 | ws: WebSocketResult<SocketReceiveMessageType> |
15 | 16 | url: string |
16 | 17 | } |
17 | 18 | |
19 | +interface SaveHistoryWsMessage{ | |
20 | + id: string | |
21 | + message: SocketSendMessageType | |
22 | +} | |
23 | + | |
18 | 24 | const socketConnectionPool: SocketConnectionPoolType[] = [] |
19 | 25 | |
20 | -const saveHistoryWsMessage=ref() | |
26 | +const saveHistoryWsMessage=ref([] as SaveHistoryWsMessage[]) | |
21 | 27 | |
22 | 28 | const parse = (value: string) => { |
23 | 29 | try { |
... | ... | @@ -90,19 +96,39 @@ export const useChartDataSocket = () => { |
90 | 96 | const { subscribeMessage } = message |
91 | 97 | const { send, data } = getSocketInstance(request) |
92 | 98 | send(JSON.stringify(subscribeMessage)) |
93 | - saveHistoryWsMessage.value=subscribeMessage | |
99 | + /** | |
100 | + * 修改删除组件时,ws还在发送信息问题 | |
101 | + */ | |
102 | + saveHistoryWsMessage.value.push({ | |
103 | + id: targetComponent.id, | |
104 | + message: subscribeMessage | |
105 | + }) | |
106 | + // | |
94 | 107 | return socketStore.getComponentValueByKeys(targetComponent, parse(unref(data))) |
95 | 108 | } |
96 | 109 | |
110 | + /** | |
111 | + * 修改删除组件时,ws还在发送信息问题 | |
112 | + * @param targetComponent | |
113 | + */ | |
97 | 114 | //删除组件 发送断开消息 |
98 | 115 | const disconnectWs=(targetComponent:CreateComponentType)=>{ |
99 | - const { request } = unref(targetComponent) | |
100 | - const message = socketStore.subscribe(unref(targetComponent)) | |
101 | - if (!message) return | |
102 | - if((request.requestContentType as RequestContentTypeEnum) !== RequestContentTypeEnum.WEB_SOCKET)return | |
103 | - const { send } = getSocketInstance(request) | |
104 | - saveHistoryWsMessage.value.tsSubCmds[0].unsubscribe=true | |
105 | - send(JSON.stringify(saveHistoryWsMessage.value)) | |
116 | + try{ | |
117 | + saveHistoryWsMessage.value.forEach((item)=>{ | |
118 | + if(item.id===targetComponent.id){ | |
119 | + const { request } = unref(targetComponent) | |
120 | + if((request.requestContentType as RequestContentTypeEnum) !== RequestContentTypeEnum.WEB_SOCKET)return | |
121 | + const { send } = getSocketInstance(request) | |
122 | + item.message.tsSubCmds[0].unsubscribe=true | |
123 | + send(JSON.stringify(item.message)) | |
124 | + } | |
125 | + }) | |
126 | + }catch (e) { | |
127 | + console.log(` | |
128 | + 错误位置:src/hooks/external/useChartDataSocket.ts | |
129 | + 错误原因:${e} | |
130 | + `) | |
131 | + } | |
106 | 132 | } |
107 | 133 | |
108 | 134 | return { | ... | ... |