2023-03-11 01:07:10 +08:00

57 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SockJS from 'sockjs-client';
import Stomp from './stomp-index';
import ws from './websocket_config';
import Bus from '@/assets/js/bus'
export function toConnection(username,token) {
connect(username, token);
// 断开重连机制,尝试发送消息,捕获异常发生时重连
ws.timer = setInterval(() => {
try {
if(ws.isConnected){
console.log('isConnected', ws.isConnected)
ws.stompClient.send("test");
}else{
connect(username, token);
}
} catch (err) {
connect(username, token);
}
}, 10000);
}
function connect(username,token){
//地址+端点路径,构建websocket链接地址,注意,对应config配置里的addEndpoint
let socket = new SockJS(ws.url + '/mq' + '?name='+username+'&jmal-token='+token);
// 获取STOMP子协议的客户端对象
ws.stompClient = Stomp.over(socket);
// 定义客户端的认证信息,按需求配置
ws.headers = {
Authorization:token
};
// 向服务器发起websocket连接
ws.stompClient.connect(ws.headers,() => {
ws.isConnected = true;
// 订阅消息
ws.stompClient.subscribe('/user/queue/update', (msg) => {
const url = msg.headers.url
if (url === 'synced') {
Bus.$emit('msg/synced', msg)
} else {
Bus.$emit('msg/file/change', msg)
}
}, ws.headers);
}, (err) => {
ws.isConnected = false;
// 连接发生错误时的处理函数
});
}
export function disconnect() {
if (ws.stompClient) {
ws.stompClient.disconnect();
}
clearInterval(ws.timer);
}