zhongrj
2024-03-27 cc1c0a396698648256c350f6f0fe2cb2f0b02c7e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<template>
  <div class="avue-logo">
    <transition name="fade">
      <span v-if="keyCollapse"
            class="avue-logo_subtitle"
            key="0">
        <i class="icon-baoan"></i>
      </span>
    </transition>
    <transition-group name="fade">
      <template v-if="!keyCollapse">
        <span class="avue-logo_title"
              key="1">{{website.indexTitle}} </span>
      </template>
    </transition-group>
  </div>
</template>
 
<script>
import { mapGetters } from "vuex";
export default {
  name: "logo",
  data() {
    return {};
  },
  created() {
    // this.websocketStart();
  },
  computed: {
    ...mapGetters(["website", "keyCollapse"])
  },
  methods: {
    //启动websocket
    websocketStart() {
      var that = this;
 
      if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
      }
      if (window.WebSocket) {
        // https
        // window.socket = new WebSocket("wss://web.byisf.com/wss/websocket/");
 
        //http
        window.socket = new WebSocket("ws://localhost:9034/websocket");
 
        window.socket.onopen = function (event) {};
 
        window.socket.onclose = function (event) {};
        window.socket.error = function (event) {
          //执行重连
          that.websocketStart();
        };
      } else {
        console.log("WebSocket连接没有建立成功!!");
      }
 
      setTimeout(function () {
        window.clearTimeout(window.websockPing);
 
        if (!window.WebSocket) {
          return;
        }
        if (window.socket.readyState == WebSocket.OPEN) {
          var userId = JSON.parse(
            window.localStorage.getItem("-userInfo")
          ).content.user_id;
 
          window.socket.send(userId);
 
          //开启心跳传送
          window.websockPing = setInterval(function () {
            if (window.socket.readyState == WebSocket.OPEN) {
              window.socket.send("ping");
            } else {
              console.log("心跳停止,断开重连");
              //断开连接,重连
              window.clearTimeout(window.websockPing);
              //执行重连
              that.websocketStart();
            }
          }, 4000);
        } else {
          console.log("WebSocket连接没有建立成功!!");
          //执行重连
          that.websocketStart();
        }
      }, 1000);
    },
  }
};
</script>
 
<style lang="scss">
.fade-leave-active {
  transition: opacity 0.2s;
}
.fade-enter-active {
  transition: opacity 2.5s;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
.avue-logo {
  position: fixed;
  top: 0;
  left: 0;
  width: 240px;
  height: 64px;
  line-height: 64px;
  background-color: #20222a;
  font-size: 20px;
  overflow: hidden;
  box-sizing: border-box;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.15);
  color: rgba(255, 255, 255, 0.8);
  z-index: 1024;
  &_title {
    display: block;
    text-align: center;
    font-weight: 300;
    font-size: 20px;
  }
  &_subtitle {
    display: block;
    text-align: center;
    font-size: 18px;
    font-weight: bold;
    color: #fff;
  }
}
</style>