import Vue from 'vue'; import StompClient from '@/utils/sock'; import store from '@/store/index'; export const displayTopic = '/user/queue/simulation'; // 其他仿真topic export const perpetualTopic = '/user/topic/message'; // 公用topic export const roomTopic = '/user/queue/room'; // 房间topic export const jl3dTopic = '/user/queue/simulation/jl3d'; // 三维topic export const LPFTopic = '/user/queue/simulation/passenger'; // 客流topic export const iscsTopic = '/topic/simulation/iscs'; // iscs topic export function getTopic(type, group) { let topic = ''; switch (type) { case 'SYSTIME': // topic = `/user/queue/simulation/${group}/sysTime`; topic = `/queue/simulation/${group}/sysTime`; break; case 'ATS': // topic = `/user/queue/simulation/${group}/ats`; topic = `/queue/simulation/${group}/ats`; break; case 'STATE': // topic = `/user/queue/simulation/${group}/state`; topic = `/queue/simulation/${group}/state`; break; case 'ISCS': topic = '/topic/simulation/iscs'; break; } return topic; } // 建立连接并订阅地址 export function creatSubscribe(topic, header) { try { if (!Vue.prototype.$stomp) { Vue.prototype.$stomp = new StompClient(); } Vue.prototype.$stomp.subscribe(topic, callback, header); } catch (error) { console.error('websocket订阅失败'); } } // 回调函数 function callback(Response) { if (store) { if (Response.headers.destination.includes('ats')) { const data = {res:JSON.parse(Response.body), type:'ats' }; store.dispatch('socket/handleSock', data); } else if (Response.headers.destination.includes('sysTime')) { store.dispatch('socket/setSimulationTimeSync', Number.parseInt(Response.body)); } else if (Response.headers.destination.includes('state')) { store.dispatch('socket/handleSimulationState', Number.parseInt(Response.body)); } else if (Response.headers.destination.includes('iscs')) { store.dispatch('socket/handleIscsState', JSON.parse(Response.body)); } else { const data = JSON.parse(Response.body); store.dispatch('socket/setStomp', data); } } else { callback(Response); } } // 删除订阅路径 export function clearSubscribe(topic) { if (Vue.prototype.$stomp) { Vue.prototype.$stomp.unsubscribe(topic); } } // 断开连接 export function disconnect() { if (Vue.prototype.$stomp) { Vue.prototype.$stomp.disconnect(); } }