import getWsUrl from './getWsUrl';
|
|
/**
|
* @param {...any} urls 地址可以传多个 用来初始化多个不同的websocket实例
|
* 对应的websocket对象及事件都带上对应的系号 this.SOCKET1 onWSMessage1 等
|
* @returns minxins对象
|
*/
|
export default function (...urls) {
|
// 重连时间间隔 默认10秒
|
const reConnectDelay = 10 * 1000;
|
let data = {},
|
computed = {},
|
ourl = {},
|
methods = {},
|
len = urls.length;
|
const fn = () => { };
|
for (let i = 0; i < len; i++) {
|
let idx = i + 1;
|
data['Stimer' + idx] = null;
|
data['SOCKET' + idx] = null;
|
ourl[idx] = getWsUrl(urls[i]);
|
computed['isWSOpen' + idx] = {
|
cache: false,
|
get () {
|
return this['SOCKET' + idx] && 1 == this['SOCKET' + idx].readyState;
|
}
|
};
|
methods['onWSMessage' + idx] = fn;
|
methods['onWSOpen' + idx] = fn;
|
methods['onWSError' + idx] = ((idx) => {
|
return function (Event) {
|
console.log('链接失败', ourl[idx]);
|
this['WSClose' + idx](Event);
|
}
|
})(idx);
|
methods['reConnect' + idx] = function () {
|
this['Stimer' + idx] = setTimeout(() => {
|
this['Stimer' + idx] = null;
|
this.WSInit();
|
}, this.reConnectDelay);
|
};
|
methods['WSClose' + idx] = function (Event) {
|
let socket = this['SOCKET' + idx];
|
const wsUri = ourl[idx];
|
if (socket) {
|
switch (socket.readyState) {
|
case 0:
|
socket.onopen = () => {
|
socket.close();
|
console.log('链接关闭', wsUri, 'close事件对象:', Event);
|
this['SOCKET' + idx] = null;
|
// 没有event对象 则为手动关闭
|
if (Event) {
|
this['reConnect' + idx]();
|
}
|
}
|
break;
|
case 1:
|
socket.close();
|
console.log('链接关闭', wsUri, 'close事件对象:', Event);
|
this['SOCKET' + idx] = null;
|
// 没有event对象 则为手动关闭
|
if (Event) {
|
this['reConnect' + idx]();
|
}
|
break;
|
case 2:
|
socket.onclose = () => {
|
console.log('链接关闭', wsUri, 'close事件对象:', Event);
|
this['SOCKET' + idx] = null;
|
// 没有event对象 则为手动关闭
|
if (Event) {
|
this['reConnect' + idx]();
|
}
|
}
|
break;
|
case 3:
|
console.log('链接关闭', wsUri, 'close事件对象:', Event);
|
this['SOCKET' + idx] = null;
|
// 没有event对象 则为手动关闭
|
if (Event) {
|
this['reConnect' + idx]();
|
}
|
break;
|
}
|
}
|
}
|
}
|
return {
|
data() {
|
return {
|
...data,
|
reConnectDelay
|
}
|
},
|
computed: {
|
...computed
|
},
|
methods: {
|
...methods,
|
// 打开链接
|
openSocket() {
|
// 初始化WebSocket 如果已经初始化 则什么都不做
|
let flag = true;
|
for (let i = 0; i < len; i++) {
|
let idx = i + 1;
|
// 如果有任何一个socket为null 则重新初始化
|
if (!this['SOCKET' + idx]) {
|
flag = false;
|
break;
|
}
|
}
|
if (flag) {
|
return false;
|
}
|
this.WSClose();
|
this.WSInit();
|
},
|
// 初始化
|
WSInit() {
|
// 未被初始化初始化
|
for (let i = 0; i < len; i++) {
|
let idx = i + 1;
|
if (!this['isWSOpen' + idx]) {
|
console.log('=====WSinit, url:', ourl[idx]);
|
let SOCKET = new WebSocket(ourl[idx]);
|
this['SOCKET' + idx] = SOCKET;
|
this['SOCKET' + idx].onmessage = this['onWSMessage' + idx];
|
this['SOCKET' + idx].onopen = this['onWSOpen' + idx];
|
this['SOCKET' + idx].onerror = this['onWSError' + idx];
|
this['SOCKET' + idx].onclose = this['WSClose' + idx];
|
}
|
}
|
},
|
WSClose() {
|
for (let i = 0; i < len; i++) {
|
let idx = i + 1;
|
this['WSClose' + idx]();
|
}
|
},
|
},
|
mounted() {
|
this.openSocket();
|
},
|
beforeDestroy() {
|
this.WSClose();
|
}
|
}
|
}
|