diff --git a/src/layouts/LineLayout.vue b/src/layouts/LineLayout.vue index 1103bc8..d00a5c3 100644 --- a/src/layouts/LineLayout.vue +++ b/src/layouts/LineLayout.vue @@ -67,6 +67,7 @@ import { ApiError } from 'src/boot/axios'; import { layerList } from 'src/drawApp/lineScene'; import { IGraphicScene } from 'src/jl-graphic'; import { ISceneName, getSceneName } from 'src/drawApp/lineApp'; +import { useTestManageStore } from 'src/stores/testManage-store'; const $q = useQuasar(); const canvasWidth = ref(0); @@ -75,6 +76,7 @@ const headerHeight = ref(0); const route = useRoute(); const router = useRouter(); const lineStore = useLineStore(); +const testManageStore = useTestManageStore(); const pslDialog = ref(false); const simulationId = (route.query.simulationId as string) || ''; const projectId = (route.query.projectId as string) || ''; @@ -114,6 +116,7 @@ const lineApp = lineStore.initLineApp(); let scene: null | IGraphicScene = null; onMounted(async () => { + testManageStore.socketConnect(); const dom = document.getElementById('line-app-container'); if (dom && defaultMapId && simulationId) { lineStore.setMapId(+defaultMapId); @@ -196,10 +199,29 @@ watch( }); } ); + +watch( + () => testManageStore.socketInfo, + (val) => { + const removeS = val.removeSimulations.find((item) => { + return item.simulationId == simulationId; + }); + if (removeS) { + $q.dialog({ + title: '确认', + message: `【${projectName.value}】项目仿真已经结束,是否确认退出?`, + cancel: true, + }).onOk(() => { + backConfirm(); + }); + } + } +); + function destroySimAndBack() { $q.dialog({ title: '确认', - message: `确认结束项目 "${projectName.value}" 吗?`, + message: `确认结束项目【${projectName.value}】吗?`, cancel: true, }).onOk(async () => { try { diff --git a/src/layouts/MainLayout.vue b/src/layouts/MainLayout.vue index 16dbb47..a54c8c9 100644 --- a/src/layouts/MainLayout.vue +++ b/src/layouts/MainLayout.vue @@ -123,8 +123,10 @@ import { useRouter } from 'vue-router'; import { clearJwtToken } from 'src/configs/TokenManage'; import { Dialog } from 'quasar'; import { useAuthStore } from 'src/stores/auth-store'; +import { useTestManageStore } from 'src/stores/testManage-store'; const router = useRouter(); +const testManageStore = useTestManageStore(); const leftDrawerOpen = ref(false); @@ -169,6 +171,7 @@ function logOut() { cancel: true, persistent: true, }).onOk(() => { + testManageStore.socketClose(); clearJwtToken(); authStore.clearCurrentUser(); router.push({ name: 'login' }); diff --git a/src/pages/TestManage.vue b/src/pages/TestManage.vue index ff48739..7d5115b 100644 --- a/src/pages/TestManage.vue +++ b/src/pages/TestManage.vue @@ -86,7 +86,7 @@ diff --git a/src/stores/testManage-store.ts b/src/stores/testManage-store.ts new file mode 100644 index 0000000..c6ffb02 --- /dev/null +++ b/src/stores/testManage-store.ts @@ -0,0 +1,45 @@ +import { defineStore } from 'pinia'; +import { CentrifugeMessagingClient } from 'src/jl-graphic/message/CentrifugeBroker'; +import { getSimulationChannelName } from 'src/api/Simulation'; +import { getWebsocketUrl } from 'src/configs/UrlManage'; +import { getOnlyToken } from 'src/configs/TokenManage'; +import { state } from 'src/protos/device_state'; + +export const useTestManageStore = defineStore('testManage', { + state: () => ({ + socket: null as CentrifugeMessagingClient | null, // 启动项目的socket + destination: '', // 启动项目的socket订阅路径 + socketInfo: new state.MemoryDataStatus(), + }), + actions: { + socketHandler(message: Uint8Array) { + const storage = state.MemoryDataStatus.deserialize(message); + this.socketInfo = storage; + }, + socketConnect() { + if (this.socket) return; + getSimulationChannelName() + .then((res) => { + this.destination = res; + this.socket = new CentrifugeMessagingClient({ + wsUrl: `${getWebsocketUrl()}`, + token: getOnlyToken() as string, + protocol: 'protobuf', + }); + this.socket.on('connected', () => { + this.socket?.subscribe(this.destination, this.socketHandler); + }); + }) + .catch((err) => { + console.log(err, '获取仿真信息更新通道名称失败!'); + }); + }, + socketClose() { + this.socket?.unsubscribe0(this.destination); + this.socket?.close(); + this.socket = null; + this.destination = ''; + this.socketInfo = new state.MemoryDataStatus(); + }, + }, +});