rt-sim-training-client/src/utils/stomp.js

98 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-07-02 16:29:52 +08:00
import Vue from 'vue';
import StompClient from '@/utils/sock';
2020-12-15 11:18:15 +08:00
import store from '@/store/index';
2019-07-02 16:29:52 +08:00
2019-11-15 16:32:23 +08:00
export const displayTopic = '/user/queue/simulation'; // 其他仿真topic
2021-02-04 17:17:45 +08:00
2019-11-12 17:27:30 +08:00
export const perpetualTopic = '/user/topic/message'; // 公用topic
2020-01-15 13:20:48 +08:00
export const roomTopic = '/user/queue/room'; // 房间topic
export const jl3dTopic = '/user/queue/simulation/jl3d'; // 三维topic
2021-02-04 17:17:45 +08:00
export const LPFTopic = '/user/queue/simulation/passenger'; // 客流topic
2021-09-10 13:09:46 +08:00
// export const iscsTopic = '/topic/simulation/iscs'; // iscs topic
2019-07-02 16:29:52 +08:00
2021-10-11 17:45:15 +08:00
export function getTopic(type, group, stationCode) {
2021-04-27 10:17:51 +08:00
let topic = '';
switch (type) {
case 'SYSTIME':
2021-05-31 15:50:35 +08:00
topic = `/queue/simulation/${group}/sysTime`;
2021-04-27 10:17:51 +08:00
break;
case 'ATS':
2021-05-31 15:50:35 +08:00
topic = `/queue/simulation/${group}/ats`;
2021-04-27 10:17:51 +08:00
break;
case 'STATE':
2021-05-31 15:50:35 +08:00
topic = `/queue/simulation/${group}/state`;
2021-04-27 10:17:51 +08:00
break;
2021-09-10 13:53:25 +08:00
case 'ISCSPSD':
2021-10-11 17:45:15 +08:00
topic = `/queue/simulation/${group}/iscs/psd/${stationCode}`;
2021-09-09 17:22:47 +08:00
break;
2022-01-18 14:43:51 +08:00
case 'ISCSPA':
topic = `/queue/simulation/${group}/iscs/pa`;
break;
case 'ISCSPIS':
topic = `/queue/simulation/${group}/iscs/pis`;
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;
2021-04-27 10:17:51 +08:00
}
2021-04-27 10:17:51 +08:00
return topic;
}
2019-07-02 16:29:52 +08:00
// 建立连接并订阅地址
export function creatSubscribe(topic, header) {
2019-11-05 16:40:36 +08:00
try {
if (!Vue.prototype.$stomp) {
Vue.prototype.$stomp = new StompClient();
}
2019-11-18 14:44:01 +08:00
Vue.prototype.$stomp.subscribe(topic, callback, header);
2019-11-05 16:40:36 +08:00
} catch (error) {
2022-08-02 18:01:42 +08:00
console.error('websocket订阅失败', error, topic, header);
2019-11-05 16:40:36 +08:00
}
2019-07-02 16:29:52 +08:00
}
// 回调函数
function callback(Response) {
2019-11-05 16:40:36 +08:00
if (store) {
2021-04-23 09:01:12 +08:00
if (Response.headers.destination.includes('ats')) {
2021-04-23 17:43:45 +08:00
const data = {res:JSON.parse(Response.body), type:'ats' };
store.dispatch('socket/handleSock', data);
2021-04-26 17:59:23 +08:00
} else if (Response.headers.destination.includes('sysTime')) {
store.dispatch('socket/setSimulationTimeSync', Number.parseInt(Response.body));
2021-04-28 16:58:29 +08:00
} else if (Response.headers.destination.includes('state')) {
store.dispatch('socket/handleSimulationState', Number.parseInt(Response.body));
2022-01-18 14:43:51 +08:00
} else if (Response.headers.destination.includes('iscs/psd')) {
2021-09-09 17:22:47 +08:00
store.dispatch('socket/handleIscsState', JSON.parse(Response.body));
2022-01-18 14:43:51 +08:00
} else if (Response.headers.destination.includes('iscs/pa')) {
store.dispatch('socket/handleIscsPaState', JSON.parse(Response.body));
2022-01-18 15:51:24 +08:00
} else if (Response.headers.destination.includes('iscs/pis')) {
store.dispatch('socket/handleIscsPisState', JSON.parse(Response.body));
2021-04-23 09:01:12 +08:00
} else {
const data = JSON.parse(Response.body);
store.dispatch('socket/setStomp', data);
}
2019-11-05 16:40:36 +08:00
} else {
callback(Response);
}
2019-07-02 16:29:52 +08:00
}
// 删除订阅路径
export function clearSubscribe(topic) {
2019-11-05 16:40:36 +08:00
if (Vue.prototype.$stomp) {
Vue.prototype.$stomp.unsubscribe(topic);
}
2019-08-06 10:11:32 +08:00
}
// 断开连接
export function disconnect() {
if (Vue.prototype.$stomp) {
Vue.prototype.$stomp.disconnect();
}
}