一 、功能点一清除定时器:
clearInterval(this.timer) // 清除定时器
二、功能点二监听了突然关闭浏览器窗口:
mounted() {
window.addEventListener("beforeunload", e => this.beforeunloadHandler(e))
window.addEventListener("unload", e => this.unloadHandler(e))
},
beforeunloadHandler() {
this._beforeUnload_time = new Date().getTime();
},
unloadHandler(e) {
this._gap_time = new Date().getTime() - this._beforeUnload_time;
// debugger
// 关闭或刷新窗口都保存数据到后台
// 判断窗口是关闭还是刷新
if (this._gap_time <= 5) {
// 关闭连接
this.webstock.close()
}
}
destroyed() {
this.$destroy()
clearInterval(this.timer) // 清除定时器
// 页面销毁时关闭长连接
// if (this.webstock !== null) {
// this.webstock.close()
// }
window.removeEventListener("beforeunload", e => this.beforeunloadHandler(e))
window.removeEventListener("unload", e => this.unloadHandler(e))
}
三、功能点三WebSocket连接发生错误的时候,连接错误 需要重连
websocketonerror(e) {
console.log("WebSocket连接发生错误")