2019-11-14 13:59:33 +08:00
|
|
|
|
import { getToken } from '@/utils/auth';
|
2019-07-02 16:29:52 +08:00
|
|
|
|
import { checkLoginLine } from '@/api/login';
|
2019-08-12 11:01:00 +08:00
|
|
|
|
import { getBaseUrl } from '@/utils/baseUrl';
|
2020-07-10 11:26:15 +08:00
|
|
|
|
import { MessageBox } from 'element-ui';
|
2020-04-01 13:47:29 +08:00
|
|
|
|
import store from '@/store/index_APP_TARGET';
|
2019-11-18 14:29:43 +08:00
|
|
|
|
import SockJS from 'sockjs-client';
|
|
|
|
|
import Stomp from 'stompjs';
|
2019-07-02 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
const isDev = process.env.NODE_ENV === 'development';
|
2019-11-22 02:11:24 +08:00
|
|
|
|
const isTest = process.env.NODE_ENV === 'test';
|
2019-07-26 17:13:03 +08:00
|
|
|
|
const websocketUrl = `${getBaseUrl()}/joylink-websocket?token=`;
|
2020-01-15 15:58:30 +08:00
|
|
|
|
const reconnectInterval = [1000, 3000, 5000, 10000, 30000, 60000];
|
2020-01-14 16:48:49 +08:00
|
|
|
|
let notFirstConnect = false;
|
2019-07-02 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
var StompClient = function (headers) {
|
2019-11-14 13:59:33 +08:00
|
|
|
|
this.url = websocketUrl + getToken();
|
2019-11-05 16:40:36 +08:00
|
|
|
|
this.headers = headers || {};
|
2020-01-14 15:13:56 +08:00
|
|
|
|
this.subscribeMap = new Map(); // 已订阅,对象{dest:'', handler:function, sub: Object}
|
2019-11-05 16:40:36 +08:00
|
|
|
|
this.connect();
|
2019-07-02 16:29:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
StompClient.prototype = {
|
2019-11-05 16:40:36 +08:00
|
|
|
|
socket: null,
|
|
|
|
|
clientIns: null,
|
|
|
|
|
url: '',
|
|
|
|
|
status: false,
|
|
|
|
|
sockStatus: 0,
|
|
|
|
|
headers: {
|
2019-11-14 13:59:33 +08:00
|
|
|
|
// 'X-Token': getToken()
|
2019-11-05 16:40:36 +08:00
|
|
|
|
},
|
2020-04-24 15:34:09 +08:00
|
|
|
|
isPassived:true, // 是否被动断线重连
|
|
|
|
|
// isconnect:true,
|
2019-11-05 16:40:36 +08:00
|
|
|
|
count: 0,
|
|
|
|
|
topic: '',
|
|
|
|
|
onmessage: null,
|
|
|
|
|
|
|
|
|
|
// 连接服务端
|
|
|
|
|
connect() {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
try {
|
|
|
|
|
// 建立连接对象(还未发起连接)
|
2020-01-14 18:18:54 +08:00
|
|
|
|
// (null, {transports:['websocket']})虽可以解决连接关闭后xhr_streaming无限循环
|
|
|
|
|
// 但是该参数只会允许websockets,若不支持,直接报错
|
|
|
|
|
// 注意sockjs会做降级处理,优先处理websockets,若浏览器不支持websockets
|
|
|
|
|
// 则会降级为xhr_streaming(轮询),继续不支持的时候会降级为xhr-polling
|
|
|
|
|
this.socket = new SockJS(websocketUrl + getToken());
|
2020-04-17 09:23:11 +08:00
|
|
|
|
|
2019-11-05 16:40:36 +08:00
|
|
|
|
// 获取 STOMP 子协议的客户端对象
|
|
|
|
|
this.clientIns = Stomp.over(this.socket);
|
|
|
|
|
|
|
|
|
|
this.closeStompDebug();
|
|
|
|
|
|
2020-04-24 15:34:09 +08:00
|
|
|
|
// const oldtransportClose = SockJS.prototype._transportClose;
|
|
|
|
|
// SockJS.prototype._transportClose = function(code, reason) {
|
|
|
|
|
// // To do
|
|
|
|
|
// // ...
|
|
|
|
|
// console.log(reason + '-----------------', this._transport);
|
|
|
|
|
// if (this._transport && this._transport.close) {
|
|
|
|
|
// this._transport.close();
|
|
|
|
|
// }
|
|
|
|
|
// return oldtransportClose.call(this, code, reason);
|
|
|
|
|
// };
|
|
|
|
|
|
2019-11-05 16:40:36 +08:00
|
|
|
|
// 向服务器发起websocket连接并发送CONNECT帧
|
2020-07-14 16:51:22 +08:00
|
|
|
|
this.websocketConnect(resolve);
|
2019-11-05 16:40:36 +08:00
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reject(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
},
|
2020-07-14 16:51:22 +08:00
|
|
|
|
websocketConnect(resolve) {
|
|
|
|
|
const that = this;
|
|
|
|
|
this.clientIns.connect({ 'X-Token': getToken() }, () => {
|
|
|
|
|
if (notFirstConnect) {
|
|
|
|
|
// setTimeout(()=>{ Message.success('连接成功'); });
|
|
|
|
|
}
|
|
|
|
|
console.info('连接成功.');
|
|
|
|
|
that.count = 0;
|
|
|
|
|
that.status = true;
|
|
|
|
|
|
|
|
|
|
// 恢复订阅
|
|
|
|
|
that.subscribeMap.forEach((subscribe) => {
|
|
|
|
|
that.subscribe(subscribe.dest, subscribe.handler);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// sock断开回调
|
|
|
|
|
that.clientIns.ws.onclose = () => {
|
|
|
|
|
// Message.error(`通信连接已断开!`);
|
|
|
|
|
console.info(`通信连接已断开!`);
|
|
|
|
|
notFirstConnect = true;
|
|
|
|
|
checkLoginLine().then(() => {
|
|
|
|
|
that.status = false;
|
|
|
|
|
that.count++;
|
|
|
|
|
that.reconnect(that.count, resolve);
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
that.logOut(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
resolve(this);
|
|
|
|
|
}, () => {
|
|
|
|
|
if (this.isPassived) {
|
|
|
|
|
checkLoginLine().then(() => {
|
|
|
|
|
this.count++;
|
|
|
|
|
this.reconnect(this.count, resolve);
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
this.logOut(err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
2019-11-05 16:40:36 +08:00
|
|
|
|
logOut(err) {
|
2020-01-14 15:13:56 +08:00
|
|
|
|
console.info(err);
|
2020-07-14 11:01:22 +08:00
|
|
|
|
// 退出登陆前断开连接
|
|
|
|
|
store.dispatch('disconnect').then(()=>{
|
|
|
|
|
MessageBox.confirm('你已被登出,请重新登录', '确定登出', {
|
|
|
|
|
confirmButtonText: '重新登录',
|
|
|
|
|
showCancelButton: false,
|
|
|
|
|
type: 'warning'
|
|
|
|
|
}).then(() => {
|
|
|
|
|
store.dispatch('FedLogOut').then(() => {
|
|
|
|
|
location.reload();// 为了重新实例化vue-router对象 避免bug
|
|
|
|
|
});
|
2019-11-05 16:40:36 +08:00
|
|
|
|
});
|
2020-01-14 15:13:56 +08:00
|
|
|
|
});
|
2019-11-05 16:40:36 +08:00
|
|
|
|
},
|
|
|
|
|
// 恢复链接
|
2020-07-14 16:51:22 +08:00
|
|
|
|
reconnect(count, resolve) {
|
2019-11-05 16:40:36 +08:00
|
|
|
|
console.info(`尝试第${count || 1}次连接.`);
|
2020-07-10 11:26:15 +08:00
|
|
|
|
// Message.warning(`正在尝试第${count || 1}次通讯连接。`);
|
2020-01-14 15:13:56 +08:00
|
|
|
|
const that = this;
|
|
|
|
|
setTimeout(() => {
|
2020-07-14 16:51:22 +08:00
|
|
|
|
that.websocketConnect(resolve);
|
2020-01-14 15:13:56 +08:00
|
|
|
|
}, reconnectInterval[that.count] || reconnectInterval[reconnectInterval.length - 1] );
|
2019-11-05 16:40:36 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
closeStompDebug() {
|
|
|
|
|
if (this.clientIns) {
|
2019-11-22 02:11:24 +08:00
|
|
|
|
if (isDev || isTest) {
|
2019-11-05 16:40:36 +08:00
|
|
|
|
this.clientIns.debug = function (message) {
|
|
|
|
|
console.debug(message);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 订阅指定的topic
|
|
|
|
|
subscribe(topic, onmessage, headers) {
|
2020-01-14 15:19:31 +08:00
|
|
|
|
// const timeDelay = [1000, 2000, 5000, 10000];
|
2019-11-05 16:40:36 +08:00
|
|
|
|
this.topic = topic;
|
|
|
|
|
this.onmessage = onmessage;
|
|
|
|
|
this.headers = headers;
|
2020-01-14 15:13:56 +08:00
|
|
|
|
if (!this.status) {
|
|
|
|
|
this.subscribeMap.set(topic, {dest: topic, handler: onmessage, sub: null});
|
2019-11-05 16:40:36 +08:00
|
|
|
|
} else {
|
2020-01-14 15:13:56 +08:00
|
|
|
|
const sub = this.clientIns.subscribe(topic, onmessage);
|
|
|
|
|
this.subscribeMap.set(topic, {dest: topic, handler: onmessage, sub: sub});
|
2019-11-05 16:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
unsubscribe(topic) {
|
2020-01-14 15:13:56 +08:00
|
|
|
|
const subscription = this.subscribeMap.get(topic);
|
|
|
|
|
if (subscription && subscription.sub) {
|
|
|
|
|
subscription.sub.unsubscribe();
|
|
|
|
|
this.subscribeMap.delete(topic);
|
2020-01-15 13:20:48 +08:00
|
|
|
|
console.log('取消订阅:' + topic);
|
2020-01-14 15:13:56 +08:00
|
|
|
|
} else if (subscription) {
|
|
|
|
|
this.subscribeMap.delete(topic);
|
2019-11-05 16:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 发送消息
|
|
|
|
|
send(url, msg) {
|
|
|
|
|
if (this.status) {
|
|
|
|
|
if (msg) {
|
|
|
|
|
msg = JSON.stringify(msg);
|
|
|
|
|
this.clientIns.send(url, {}, msg);
|
2020-01-14 15:13:56 +08:00
|
|
|
|
} else {
|
|
|
|
|
console.error('发送消息为空');
|
2019-11-05 16:40:36 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.send(url, msg);
|
|
|
|
|
}, 300);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
disconnect() {
|
2020-04-24 15:34:09 +08:00
|
|
|
|
if (this.clientIns) {
|
|
|
|
|
if (this.clientIns.connected) {
|
|
|
|
|
this.clientIns.disconnect();
|
|
|
|
|
}
|
|
|
|
|
this.isPassived = false;
|
2020-04-17 09:13:40 +08:00
|
|
|
|
this.socket.close();
|
2020-04-24 15:34:09 +08:00
|
|
|
|
this.socket = null;
|
2019-11-05 16:40:36 +08:00
|
|
|
|
this.clientIns = null;
|
|
|
|
|
}
|
|
|
|
|
this.status = false;
|
2020-01-14 15:13:56 +08:00
|
|
|
|
console.info('断开连接');
|
2019-11-05 16:40:36 +08:00
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default StompClient;
|