fuliqi
2025-01-08 f828b1576669f6b4a301e8dec18b81ebbb5e5204
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<template>
  <div class="app-container home">
    <!-- <data-view></data-view> -->
    <data-wrapper></data-wrapper>
  </div>
</template>
 
<script>
import DataView from './home/data-view/index.vue';
import DataWrapper from './home/data-wrapper/index.vue';
import { getToken } from '@/utils/auth'
 
export default {
  name: "Index",
  components: {
    DataView,
    DataWrapper
  },
  data() {
    return {
 
    };
  },
  methods: {
    senMsg(msg) {
      // 发送文本消息
      this.$websocket.send(msg);
    },
    // 发送心跳消息
    sendHeartbeat() {
      if (this.$websocket.readyState === WebSocket.OPEN) {
        // 发送心跳消息,可以是任意格式的字符串,用于表示心跳
        this.senMsg('ping');
      }
    },
    // 开始心跳定时器
    startHeartbeat() {
      this.heartbeatInterval = setInterval(() => {
        this.sendHeartbeat();
      }, 10000); // 每 10 秒发送一次心跳
    },
    // 停止心跳定时器
    stopHeartbeat() {
      clearInterval(this.heartbeatInterval);
    },
    initWebsocket() {
      this.$websocket = new WebSocket(process.env.VUE_APP_WEB_SOCKET_URL)
      // 监听 WebSocket 连接成功事件
      this.$websocket.onopen = event => {
        console.log('WebSocket 连接成功', event);
        let msg = {
          "token": getToken()
        }
        // 发送身份认证
        this.senMsg(JSON.stringify(msg))
        // 设置心跳定时器,定期发送心跳消息
        this.startHeartbeat();
      };
 
      // 监听 WebSocket 接收消息事件
      this.$websocket.onmessage = event => {
        const message = event.data;
        console.log('接收到消息:', message);
      };
 
      // 监听 WebSocket 连接关闭事件
      this.$websocket.onclose = event => {
        console.log('WebSocket 连接关闭', event);
        // 清除心跳定时器
        this.stopHeartbeat();
      };
    },
  },
  mounted() {
    this.initWebsocket();
  }
};
</script>
 
<style scoped lang="scss">
.home {
  blockquote {
    padding: 10px 20px;
    margin: 0 0 20px;
    font-size: 17.5px;
    border-left: 5px solid #eee;
  }
 
  hr {
    margin-top: 20px;
    margin-bottom: 20px;
    border: 0;
    border-top: 1px solid #eee;
  }
 
  .col-item {
    margin-bottom: 20px;
  }
 
  ul {
    padding: 0;
    margin: 0;
  }
 
  font-family: "open sans",
  "Helvetica Neue",
  Helvetica,
  Arial,
  sans-serif;
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;
 
  ul {
    list-style-type: none;
  }
 
  h4 {
    margin-top: 0px;
  }
 
  h2 {
    margin-top: 10px;
    font-size: 26px;
    font-weight: 100;
  }
 
  p {
    margin-top: 10px;
 
    b {
      font-weight: 700;
    }
  }
 
  .update-log {
    ol {
      display: block;
      list-style-type: decimal;
      margin-block-start: 1em;
      margin-block-end: 1em;
      margin-inline-start: 0;
      margin-inline-end: 0;
      padding-inline-start: 40px;
    }
  }
}
</style>