2023-08-18 08:55:52 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div id="app">
|
|
|
|
|
|
<router-view v-if="isRouterAlive"></router-view>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
2023-08-26 14:56:01 +08:00
|
|
|
|
import { mapGetters } from "vuex";
|
|
|
|
|
|
import { WS_URL } from "./utils/websocketApi";
|
2023-08-18 08:55:52 +08:00
|
|
|
|
export default {
|
2023-08-26 14:56:01 +08:00
|
|
|
|
name: "App",
|
2023-08-18 08:55:52 +08:00
|
|
|
|
computed: {
|
2023-08-26 14:56:01 +08:00
|
|
|
|
...mapGetters(["oidcIsAuthenticated"]),
|
2023-08-18 08:55:52 +08:00
|
|
|
|
},
|
2023-08-26 14:56:01 +08:00
|
|
|
|
data() {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
return {
|
2023-08-26 14:56:01 +08:00
|
|
|
|
isRouterAlive: true,
|
|
|
|
|
|
// socket参数
|
|
|
|
|
|
socket: null,
|
|
|
|
|
|
timeout: 10 * 1000, // 45秒一次心跳
|
|
|
|
|
|
timeoutObj: null, // 心跳心跳倒计时
|
|
|
|
|
|
serverTimeoutObj: null, // 心跳倒计时
|
|
|
|
|
|
timeoutnum: null, // 断开 重连倒计时
|
|
|
|
|
|
lockReconnect: false, // 防止
|
|
|
|
|
|
};
|
2023-08-18 08:55:52 +08:00
|
|
|
|
},
|
2023-08-26 14:56:01 +08:00
|
|
|
|
mounted() {
|
|
|
|
|
|
this.initWebSocket();
|
2023-08-18 08:55:52 +08:00
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
2023-08-26 14:56:01 +08:00
|
|
|
|
initWebSocket() {
|
|
|
|
|
|
// WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
|
|
|
|
|
|
let wsUrl = WS_URL;
|
2023-08-26 18:35:42 +08:00
|
|
|
|
window.websocket = new WebSocket(wsUrl);
|
|
|
|
|
|
window.websocket.onopen = this.websocketonopen;
|
|
|
|
|
|
window.websocket.onerror = this.websocketonerror;
|
|
|
|
|
|
window.websocket.onmessage = this.setOnmessageMessage;
|
|
|
|
|
|
window.websocket.onclose = this.websocketclose;
|
2023-08-26 14:56:01 +08:00
|
|
|
|
// 监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
|
|
|
|
|
|
// window.onbeforeunload = that.onbeforeunload
|
2023-08-18 08:55:52 +08:00
|
|
|
|
},
|
2023-08-26 14:56:01 +08:00
|
|
|
|
start() {
|
2023-08-26 18:35:42 +08:00
|
|
|
|
// console.log("start");
|
2023-08-26 14:56:01 +08:00
|
|
|
|
//清除延时器
|
|
|
|
|
|
this.timeoutObj && clearTimeout(this.timeoutObj);
|
|
|
|
|
|
this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
|
|
|
|
|
|
this.timeoutObj = setTimeout(() => {
|
2023-08-26 18:35:42 +08:00
|
|
|
|
if (window.websocket && window.websocket.readyState == 1) {
|
2023-08-26 14:56:01 +08:00
|
|
|
|
//发送消息,服务端返回信息,即表示连接良好,可以在socket的onmessage事件重置心跳机制函数
|
2023-08-26 18:35:42 +08:00
|
|
|
|
window.websocket.send("heartBath");
|
2023-08-26 14:56:01 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
this.reconnect();
|
|
|
|
|
|
}
|
|
|
|
|
|
//定义一个延时器等待服务器响应,若超时,则关闭连接,重新请求server建立socket连接
|
|
|
|
|
|
this.serverTimeoutObj = setTimeout(() => {
|
2023-08-26 18:35:42 +08:00
|
|
|
|
window.websocket.close();
|
2023-08-26 14:56:01 +08:00
|
|
|
|
}, 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.lockReconnect = false;
|
|
|
|
|
|
}, 5000);
|
|
|
|
|
|
},
|
|
|
|
|
|
async setOnmessageMessage(event) {
|
2023-08-26 18:35:42 +08:00
|
|
|
|
// console.log(event.data, "获得消息");
|
2023-08-26 14:56:01 +08:00
|
|
|
|
this.reset();
|
|
|
|
|
|
// 自定义全局监听事件
|
|
|
|
|
|
window.dispatchEvent(
|
|
|
|
|
|
new CustomEvent("onmessageWS", {
|
|
|
|
|
|
detail: {
|
|
|
|
|
|
data: event.data,
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
websocketonopen() {
|
|
|
|
|
|
//开启心跳
|
|
|
|
|
|
this.start();
|
|
|
|
|
|
console.log(
|
2023-08-26 18:35:42 +08:00
|
|
|
|
"WebSocket连接成功!!!" + new Date() + "----" + window.websocket.readyState
|
2023-08-26 14:56:01 +08:00
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
websocketonerror(e) {
|
|
|
|
|
|
console.log("WebSocket连接发生错误" + e + ",重启socket");
|
|
|
|
|
|
this.initWebSocket();
|
|
|
|
|
|
},
|
|
|
|
|
|
websocketclose(e) {
|
2023-08-26 18:35:42 +08:00
|
|
|
|
window.websocket.close();
|
2023-08-26 14:56:01 +08:00
|
|
|
|
clearTimeout(this.timeoutObj);
|
|
|
|
|
|
clearTimeout(this.serverTimeoutObj);
|
|
|
|
|
|
console.log("WebSocket连接关闭,重启socket");
|
|
|
|
|
|
this.initWebSocket();
|
|
|
|
|
|
},
|
|
|
|
|
|
websocketsend(messsage) {
|
2023-08-26 18:35:42 +08:00
|
|
|
|
window.websocket.send(messsage);
|
2023-08-26 14:56:01 +08:00
|
|
|
|
},
|
|
|
|
|
|
closeWebSocket() {
|
|
|
|
|
|
// 关闭websocket
|
2023-08-26 18:35:42 +08:00
|
|
|
|
window.websocket.close();
|
2023-08-26 14:56:01 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
2023-08-18 08:55:52 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
<style>
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.flex {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.flex-1 {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
flex: 1;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.jc-c {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
justify-content: center;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.jc-sb {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
justify-content: space-between;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.jc-e {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
justify-content: end;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.ai-c {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
align-items: center;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.column {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.fc-w {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
color: #fff;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.max-h {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
height: 100%;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.max-w {
|
2023-08-18 08:55:52 +08:00
|
|
|
|
width: 100%;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.fz-16 {
|
2023-08-21 09:47:30 +08:00
|
|
|
|
font-size: 16px;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.fz-14 {
|
2023-08-21 09:47:30 +08:00
|
|
|
|
font-size: 14px;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.fz-10 {
|
2023-08-21 09:47:30 +08:00
|
|
|
|
font-size: 10px;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.mt-1 {
|
2023-08-21 17:32:19 +08:00
|
|
|
|
margin-top: 10px;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.mt-2 {
|
2023-08-21 17:32:19 +08:00
|
|
|
|
margin-top: 20px;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.mr-1 {
|
2023-08-21 09:47:30 +08:00
|
|
|
|
margin-right: 10px;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.mr-2 {
|
2023-08-21 09:47:30 +08:00
|
|
|
|
margin-right: 20px;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.cursor {
|
2023-08-21 09:47:30 +08:00
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
}
|
2023-08-26 14:56:01 +08:00
|
|
|
|
.fw-b {
|
2023-08-21 09:47:30 +08:00
|
|
|
|
font-weight: bold;
|
|
|
|
|
|
}
|
2023-08-18 08:55:52 +08:00
|
|
|
|
</style>
|