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

45 lines
1.1 KiB
JavaScript

import Vue from 'vue';
import StompClient from '@/utils/sock';
import store from '@/store';
export const displayTopic = '/user/queue/simulation'; // 其他仿真topic
export const perpetualTopic = '/user/topic/message'; // 公用topic
export const roomTopic = '/user/queue/room'; // 房间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) {
var data = JSON.parse(Response.body);
// console.log(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();
}
}