<!--
 * @Author: Robin.lau
 * @Email: 329080237@qq.com
 * @Wechat: 15618110227
-->
<template>
  <div id="app">
    <router-view v-if="isRouterAlive" :key="key"></router-view>
  </div>
</template>

<script>
  export default {
    name: 'App',
    provide() {
      return {
        reload: this.reload,
      }
    },
    data() {
      return {
        isRouterAlive: true,
        userInfo: JSON.parse(localStorage.getItem("userInfo")),
      }
    },
    created() {
      if(this.userInfo){
        this.initSocket()
      }
    },
    computed: {
      key() {
        return this.$route.path
      }
    },
    methods: {
      reload() {
        this.isRouterAlive = false
        this.$nextTick(function() {
          this.isRouterAlive = true
        })
      },

      //初始化 websocket连接
      initSocket() {
        let that = this;
        if ("WebSocket" in window) {
          //线上环境
          //that.ws = new WebSocket("ws://wb-web.traumwork.com/ws/client/"+that.userInfo.id+"/");
          //本地环境
          that.ws = new WebSocket("ws://127.0.0.1:8000/ws/client/"+that.userInfo.id+"/");
          that.wsGlobal.setWs(that.ws);
          that.ws.onopen = function () {
            console.log('websocket连接成功');
          };

          that.ws.onclose = function () {
            // 关闭 websocket
            console.log("连接已关闭...");
            //断线重新连接
            // setTimeout(() => {
            //     that.initSocket();
            // }, 3000);
          };
        } else {
          // 浏览器不支持 WebSocket
          console.log("您的浏览器不支持 WebSocket!");
          this.openNotificationWithIcon('error', '浏览器', '您的浏览器不支持显示消息请更换', 1,1)
        }
      }

    }
  }
</script>