调整ws连接时机
This commit is contained in:
parent
ea8276e6a0
commit
a3ea4edcf4
@ -28,8 +28,8 @@
|
|||||||
trigger="click"
|
trigger="click"
|
||||||
>
|
>
|
||||||
<div class="avatar-wrapper">
|
<div class="avatar-wrapper">
|
||||||
<img :src="avatar" class="user-avatar" />
|
<img :src="avatar" class="user-avatar" alt="avatar" />
|
||||||
<i class="el-icon-caret-bottom" />
|
<em class="el-icon-caret-bottom" />
|
||||||
</div>
|
</div>
|
||||||
<el-dropdown-menu slot="dropdown">
|
<el-dropdown-menu slot="dropdown">
|
||||||
<router-link to="/user/profile">
|
<router-link to="/user/profile">
|
||||||
@ -51,7 +51,7 @@
|
|||||||
import { mapGetters, mapActions } from "vuex";
|
import { mapGetters, mapActions } from "vuex";
|
||||||
import Breadcrumb from "@/components/Breadcrumb";
|
import Breadcrumb from "@/components/Breadcrumb";
|
||||||
import Hamburger from "@/components/Hamburger";
|
import Hamburger from "@/components/Hamburger";
|
||||||
import { keys } from "@/utils/websocket";
|
import { keys, websocketInit, beforeUnmount } from "@/utils/websocket";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
@ -65,12 +65,14 @@ export default {
|
|||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
this.init();
|
this.init();
|
||||||
|
websocketInit();
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
window.postMessage({ type: keys.GET_UNREAD_COUNT }, window.location.origin);
|
window.postMessage({ type: keys.GET_UNREAD_COUNT }, window.location.origin);
|
||||||
window.addEventListener("message", this.handleOnMessage);
|
window.addEventListener("message", this.handleOnMessage);
|
||||||
},
|
},
|
||||||
unmounted() {
|
unmounted() {
|
||||||
|
beforeUnmount();
|
||||||
window.removeEventListener("message", this.handleOnMessage);
|
window.removeEventListener("message", this.handleOnMessage);
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
@ -21,14 +21,10 @@ import VueScrollTo from "vue-scrollto";
|
|||||||
import VueResource from "vue-resource";
|
import VueResource from "vue-resource";
|
||||||
import HighchartsVue from "highcharts-vue";
|
import HighchartsVue from "highcharts-vue";
|
||||||
import Highcharts from "highcharts";
|
import Highcharts from "highcharts";
|
||||||
import { init } from "@/utils/websocket";
|
|
||||||
//图片导出模块
|
//图片导出模块
|
||||||
import exportingInit from "highcharts/modules/exporting";
|
import exportingInit from "highcharts/modules/exporting";
|
||||||
exportingInit(Highcharts);
|
exportingInit(Highcharts);
|
||||||
|
|
||||||
// websocket 初始化
|
|
||||||
init();
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
addDateRange,
|
addDateRange,
|
||||||
download,
|
download,
|
||||||
|
@ -41,21 +41,31 @@ function connect() {
|
|||||||
// console.log({ event });
|
// console.log({ event });
|
||||||
ws = undefined;
|
ws = undefined;
|
||||||
window.removeEventListener("message", handleOnMessageReceive);
|
window.removeEventListener("message", handleOnMessageReceive);
|
||||||
|
|
||||||
|
websocketInit();
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onclose = event => {
|
ws.onclose = event => {
|
||||||
|
// console.log(event);
|
||||||
ws = undefined;
|
ws = undefined;
|
||||||
window.removeEventListener("message", handleOnMessageReceive);
|
window.removeEventListener("message", handleOnMessageReceive);
|
||||||
|
if (event.reason !== "unmount") {
|
||||||
|
websocketInit();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// console.log(error);
|
// console.log(error);
|
||||||
|
// console.log("浏览器不支持websocket");
|
||||||
ws = undefined;
|
ws = undefined;
|
||||||
init();
|
websocketInit();
|
||||||
console.log("浏览器不支持websocket");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function init() {
|
export function beforeUnmount(code) {
|
||||||
|
ws && ws.close(code, "unmount");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function websocketInit() {
|
||||||
!ws && connect();
|
!ws && connect();
|
||||||
|
|
||||||
!ws &&
|
!ws &&
|
||||||
|
Loading…
x
Reference in New Issue
Block a user