rt-sim-training-client/src/utils/stomp.js
2022-09-28 16:57:35 +08:00

113 lines
4.1 KiB
JavaScript

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, param) {
let topic = '';
switch (type) {
case 'SYSTIME':
topic = `/queue/simulation/${group}/sysTime`;
break;
case 'ATS':
topic = `/queue/simulation/${group}/ats`;
break;
case 'STATE':
topic = `/queue/simulation/${group}/state`;
break;
case 'ISCSPSD':
topic = `/queue/simulation/${group}/iscs/psd/${param.stationCode}`;
break;
case 'ISCSPA':
topic = `/queue/simulation/${group}/iscs/pa`;
break;
case 'ISCSPIS':
topic = `/queue/simulation/${group}/iscs/pis`;
break;
case 'ISCSGATE':
topic = `/queue/simulation/${group}/iscs/gate/${param.stationCode}`;
break;
case 'PIS_STAND':
topic = `/queue/simulation/${group}/standPis/${param.standCode}`;
break;
case 'PIS_TRAIN':
topic = `/queue/simulation/${group}/onboardPis/${param.groupNumber}`;
break;
case 'CTC':
topic = `/user/queue/simulation/${group}/ctc`;
break;
// case 'CTC_RAILWAY':
// topic = `/queue/simulation/${group}/railway`;
// break;
case 'CTC_MANAGE':
// topic = `/user/queue/simulation/${group}/ctc/manage`;
topic = `/user/queue/simulation/${group}/ctcManage`;
break;
}
return topic;
}
// 建立连接并订阅地址
export function creatSubscribe(topic, header) {
try {
if (!Vue.prototype.$stomp) {
Vue.prototype.$stomp = new StompClient();
}
if (!Vue.prototype.$stomp.subscribeMap.has(topic)) {
Vue.prototype.$stomp.subscribe(topic, callback, header);
}
} catch (error) {
console.error('websocket订阅失败', error, topic, header);
}
}
// 回调函数
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/psd') || Response.headers.destination.includes('iscs/gate')) {
store.dispatch('socket/handleIscsState', JSON.parse(Response.body));
} else if (Response.headers.destination.includes('iscs/pa')) {
store.dispatch('socket/handleIscsPaState', JSON.parse(Response.body));
} else if (Response.headers.destination.includes('iscs/pis')) {
store.dispatch('socket/handleIscsPisState', JSON.parse(Response.body));
} else if (Response.headers.destination.includes('standPis')) {
store.dispatch('socket/handleStandPisState', JSON.parse(Response.body));
} else if (Response.headers.destination.includes('onboardPis')) {
store.dispatch('socket/handleOnboardPisState', 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();
}
}