<template>
  <div id="app">
    <router-view />
    <!-- <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/> -->
  </div>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'

export default {
  name: "App",
  // components: {
  //   HelloWorld
  // }

  created() {
    //初始化方法
    this.localSocket();



    

    //在页面加载时读取sessionStorage里的状态信息
    if (sessionStorage.getItem("store")) {
      this.$store.replaceState(
        Object.assign(
          {},
          this.$store.state,
          JSON.parse(sessionStorage.getItem("store"))
        )
      );
    }
    //在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener("beforeunload", () => {
      sessionStorage.setItem("store", JSON.stringify(this.$store.state));
    });





  },
  methods: {
    localSocket() {
      let that = this;
      if ("WebSocket" in window) {
        // console.log("您的浏览器支持 WebSocket!");
        // location.host
        // that.global.ws = new WebSocket("ws://172.16.120.210:6001"); //后台本机ip
        // that.global.ws = new WebSocket("ws://121.199.52.195:6001");  //线上测试ip
        that.global.ws = new WebSocket("ws://172.16.120.79:6001");  //虚拟机ip
        that.global.setWs(that.global.ws);
        that.global.ws.onopen = function () {
          console.log("websocket连接成功");
        };

        that.global.ws.onmessage = (res) => {
          this.$store.commit("wsInfoMiT", res);
          this.$store.commit("pusTimer", 30);
        };

        that.global.ws.onclose = function () {
          // 关闭 websocket
          console.log("连接已关闭...");

          //断线重新连接
          setTimeout(() => {
            that.localSocket();
          }, 2000);
        };
      } else {
        // 浏览器不支持 WebSocket
        console.log("您的浏览器不支持 WebSocket!");
        this.openNotificationWithIcon(
          "error",
          "浏览器",
          "您的浏览器不支持显示消息请更换",
          1,
          1
        );
      }
      console.log(that.global.ws);
    },
  },
};
</script>

<style>
</style>
