一、vue2 + webSocket
webSocket.js
export default {
data() {
return {
websock: null,
lockReconnect: false,
timeout: 1000 * 5,
timeoutObj: null,
serverTimeoutObj: null,
timeoutnum: null,
wsId: Math.ceil(Math.random() * 9999) + 1000,
notification: null,
};
},
created() {
this.initWebSocket();
},
destroyed() {
this.websock.close();
this.lockReconnect = true;
if (this.timeoutnum)
clearTimeout(this.timeoutnum);
if (this.timeoutObj)
clearTimeout(this.timeoutObj);
if (this.serverTimeoutObj)
clearTimeout(this.serverTimeoutObj);
},
methods: {
initWebSocket() {
const nowLocation = window.location;
const protocol = nowLocation.protocol;
const host = nowLocation.host;
const wsuri = ((protocol.startsWith('https')) ? 'wss://' : 'ws://') + host + this.staticUrl + ((protocol.startsWith('https')) ? '/wssApi' : '/wsApi') + "/webSocket/" + this.wsId;
this.websock = new WebSocket(wsuri);
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onmessage = this.websocketonmessage;
this.websock.onclose = this.websocketclose;
},
websocketonopen() {
this.websocketsend("heartbeat");
this.start();
},
websocketonerror(e) {
console.log("WebSocket连接发生错误");
this.reconnect();
},
websocketclose(e) {
this.websock.close();
this.reconnect();
},
websocketonmessage(event) {
const data = JSON.parse(event.data)
switch (data.message) {
case 'generateDataPacket':
this.$showLoadMsg(data.data)
break
case 'importPackage-progress':
if (!this.notification) {
this.notification = this.$notify({
title: '提示',
message: '导入进度:' + data.data,
duration: 0,
customClass: 'notify_top',
});
} else {
this.notification.message = '导入进度:' + data.data
}
break
case 'importPackage-success':
this.$message.success(data.data)
this.notification.close()
this.notification = null
break
case 'importPackage-over':
this.$message.error(data.data)
break
case 'importPackage-repeat':
this.repeatDataVisible = true
data.data.id = data.data.newData.id
data.data.loadRow = false
this.repeatDataList.push(data.data)
break
}
this.reset();
},
websocketsend(msg) {
this.websock.send(msg);
},
reconnect() {
var that = this;
if (that.lockReconnect) {
return;
}
that.lockReconnect = true;
that.timeoutnum && clearTimeout(that.timeoutnum);
that.timeoutnum = setTimeout(function () {
that.initWebSocket();
that.lockReconnect = false;
}, 5000);
},
reset() {
var that = this;
clearTimeout(that.timeoutObj);
clearTimeout(that.serverTimeoutObj);
that.start();
},
start() {
var self = this;
self.timeoutObj && clearTimeout(self.timeoutObj);
self.serverTimeoutObj && clearTimeout(self.serverTimeoutObj);
self.timeoutObj = setTimeout(function () {
if (self.websock.readyState == 1) {
self.websock.send("heartbeat");
} else {
self.reconnect();
}
self.serverTimeoutObj = setTimeout(function () {
self.websock.close();
}, self.timeout);
}, self.timeout);
},
},
};
vue2
import webSocketMixin from '@/components/common/mixins/webSocket.js'
export default {
mixins: [webSocketMixin],
data() {
return {
repeatDataVisible: false,
repeatDataList: [],
}
},
mounted() {
this.initList()
},
methods: {
leadingOutDataPacket(row) {
window.location.href = `${this.staticUrl}/globalApi/export?path=${encodeURIComponent(row.path)}`
},
leadingInDataPacket(row) {
this.$confirm('  导入数据会进行较长时间,期间系统将无法正常使用,请耐心等待。<br /><br />  确定要导入数据吗?', '警告!', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
dangerouslyUseHTMLString: true
}).then(() => {
let param = {
operate: 'importPackage',
data: {
path: row.path
}
}
param = JSON.stringify(param)
this.websock.send(param);
}).catch(() => {
this.$message.info('已取消')
})
},
},
}
二、vue3 + webSocket + ts
webSocket.ts
代码
vue3
代码