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

38 lines
1008 B
JavaScript
Raw Normal View History

2019-07-02 16:29:52 +08:00
import Vue from 'vue';
import StompClient from '@/utils/sock';
import store from '@/store';
2019-11-15 16:32:23 +08:00
export const displayTopic = '/user/queue/simulation'; // 其他仿真topic
2019-11-12 17:27:30 +08:00
export const perpetualTopic = '/user/topic/message'; // 公用topic
export const commonTopic = '/topic/message'; // 公共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-12 17:27:30 +08:00
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) {
console.error('websocket订阅失败');
}
2019-07-02 16:29:52 +08:00
}
// 回调函数
function callback(Response) {
2019-11-05 16:40:36 +08:00
if (store) {
var data = JSON.parse(Response.body);
store.dispatch('socket/setStomp', data);
} 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
}