齐鲁医院物联网系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

153 lines
4.4 KiB

<template>
<div id="app">
<router-view />
</div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
name: "app",
data() {
return {
// socket参数
socket: null,
timeout: 45 * 1000, // 45秒一次心跳
timeoutObj: null, // 心跳心跳倒计时
serverTimeoutObj: null, // 心跳倒计时
timeoutnum: null, // 断开 重连倒计时
lockReconnect: false, // 防止
websocket: null,
isClose: false,//是否关闭不重连
};
},
computed: {
...mapGetters([
"token",
"userInfo"
])
},
watch: {
token: {
handler (newVal) {
console.log('token', newVal ? true : false)
if(newVal) {
this.isClose = false;
if(this.userInfo) {
this.initWebSocket(this.userInfo.user_id);
}
}else{
this.isClose = true;
this.closeWebSocket();
}
}
}
},
created() {
window.addEventListener('beforeunload', e => this.closeWebSocket(e));
},
mounted() {
// this.initWebSocket('pc');
},
beforeDestroy() {
},
methods: {
initWebSocket(supplierId) {
// WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
let wsUrl = `ws://192.168.3.32:81/websocket/${supplierId}`;//`ws://171.16.8.51:81/websocket/${supplierId}`//
this.websocket = new WebSocket(wsUrl);
this.websocket.onopen = this.websocketonopen;
this.websocket.onerror = this.websocketonerror;
this.websocket.onmessage = this.setOnmessageMessage;
this.websocket.onclose = this.websocketClose();
},
start() {
// console.log('start');
//清除延时器
this.timeoutObj && clearTimeout(this.timeoutObj);
this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
this.timeoutObj = setTimeout(() => {
if (this.websocket && this.websocket.readyState == 1) {
this.websocket.send('heartBath');//发送消息,服务端返回信息,即表示连接良好,可以在socket的onmessage事件重置心跳机制函数
} else {
this.reconnect();
}
//定义一个延时器等待服务器响应,若超时,则关闭连接,重新请求server建立socket连接
this.serverTimeoutObj = setTimeout(() => {
this.websocketclose();
}, this.timeout)
}, this.timeout)
},
reset() { // 重置心跳
// 清除时间
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
// 重启心跳
this.start();
},
// 重新连接
reconnect() {
if (this.lockReconnect) return
this.lockReconnect = true;
//没连接上会一直重连,设置延迟避免请求过多
this.timeoutnum && clearTimeout(this.timeoutnum);
this.timeoutnum = setTimeout(() => {
this.initWebSocket(this.userInfo.user_id);
this.lockReconnect = false;
}, 5000)
},
async setOnmessageMessage(event) {
// console.log(event.data, '获得消息');
this.reset();
// 自定义全局监听事件
window.dispatchEvent(new CustomEvent('onmessageWS', {
detail: {
data: event.data
}
}))
// //发现消息进入 开始处理前端触发逻辑
// if (event.data === 'success' || event.data === 'heartBath') return
},
websocketClose() {
console.log("WebSocket连接关闭===============");
},
websocketonopen() {
//开启心跳
this.start();
console.log("WebSocket连接成功!!!" + new Date() + "----" + this.websocket.readyState);
},
websocketonerror(e) {
console.log("WebSocket连接发生错误" + e);
},
websocketclose(e) {
// this.websocket.close()
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
console.log("WebSocket连接关闭重连");
this.reconnect();//重连
},
websocketsend(messsage) {
this.websocket.send(messsage)
},
closeWebSocket() { // 关闭websocket
if(this.websocket) {
console.log("WebSocket连接关闭");
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
this.websocket.close();
}
},
},
};
</script>
<style lang="scss">
#app {
width: 100%;
height: 100%;
overflow: hidden;
}
.avue--detail .el-col {
margin-bottom: 0;
}
</style>