Parcourir la source

Merge branch 'master' of http://git.ycjcjy.com/welcome/screen

wangfei il y a 6 ans
Parent
révision
c4b00fdcd9
2 fichiers modifiés avec 108 ajouts et 2 suppressions
  1. 13
    2
      src/pages/page3.vue
  2. 95
    0
      src/util/websocket.js

+ 13
- 2
src/pages/page3.vue Voir le fichier

@@ -56,7 +56,7 @@
56 56
         </div>
57 57
         <div :hidden="showSlide">
58 58
           <ul class="centerLabel userList">
59
-            <userList></userList>
59
+            <!-- <userList></userList> -->
60 60
           </ul>
61 61
         </div>
62 62
       </div>
@@ -70,11 +70,13 @@ import 'swiper/dist/css/swiper.css'
70 70
 import { swiper, swiperSlide } from 'vue-awesome-swiper'
71 71
 import solarLunar from 'solarLunar'
72 72
 import userList from '../components/userList'
73
+import XWebSocket from '../util/websocket'
73 74
 const { mapState: mapPageState, mapActions: mapPageActions, mapMutations: mapPageMutations } = createNamespacedHelpers('newIndex')
74 75
 export default {
75 76
   name: 'projectIndex',
76 77
   data () {
77 78
     return {
79
+      ws: null,
78 80
       userList: [{
79 81
         firstName: '111',
80 82
         gender: '1',
@@ -135,12 +137,18 @@ export default {
135 137
     userList
136 138
   },
137 139
   created () {
140
+    this.ws = new XWebSocket({
141
+      message: this.receviedMsg.bind(this),
142
+      delay: 10,
143
+      autoConnect: true
144
+    })
145
+    this.ws.connect(`ws://192.168.0.11/api/websocket/${this.$route.query.id}`)
138 146
     this.init()
139 147
     this.pageTimer = window.setInterval(() => {
140 148
       let curMin = new Date().getMinutes() - 0
141 149
       let curHour = new Date().getHours() - 0
142 150
       let curSec = new Date().getSeconds() - 0
143
-      if (!curMin) { // 整点刷新请求
151
+      if (!curMin && !curSec) { // 整点刷新请求
144 152
         if (curHour === 1 && !curSec) { // 1点刷新页面
145 153
           window.clearInterval(this.pageTimer)
146 154
           window.location.reload()
@@ -160,6 +168,9 @@ export default {
160 168
     ...mapPageMutations([
161 169
       'emptyPageDetail'
162 170
     ]),
171
+    receviedMsg (e) {
172
+      console.log('-----------------------', e, '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
173
+    },
163 174
     init () { // 初始化
164 175
       this.emptyPageDetail()
165 176
       this.getDate()

+ 95
- 0
src/util/websocket.js Voir le fichier

@@ -0,0 +1,95 @@
1
+// const WsConnecting = 0
2
+const WsOpen = 1
3
+// const WsClosing = 2
4
+// const WsClosed = 3
5
+
6
+export default class XWebSocket {
7
+  addr = ''
8
+  client = null
9
+  autoConnect = false
10
+  interval = undefined
11
+  delay = 60
12
+  on = { open: null, message: null, error: null, close: null }
13
+
14
+  // constructor
15
+  constructor ({ open, message, error, close, delay, autoConnect }) {
16
+    this.on = { open, message, error, close }
17
+
18
+    if (delay >= 0) {
19
+      this.delay = delay
20
+    }
21
+
22
+    this.autoConnect = autoConnect
23
+  }
24
+
25
+  // 连接
26
+  connect (addr) {
27
+    this.addr = addr
28
+    this._wsInit()
29
+  }
30
+
31
+  // 发送
32
+  send (data) {
33
+    if (this.client && this.client.readyState === WsOpen) {
34
+      this.client.send(data)
35
+    }
36
+  }
37
+
38
+  // 关闭
39
+  close () {
40
+    if (!this.client) return
41
+
42
+    this._breforeClose(false)
43
+
44
+    this.client.close()
45
+    this.client = null
46
+  }
47
+
48
+  _breforeClose (autoConnect) {
49
+    this._wsClearHeartbeat()
50
+
51
+    if (this.client) {
52
+      if (!autoConnect) {
53
+        // 防止 onclose 中实现 断线重连
54
+        this.client.onclose = undefined
55
+      }
56
+    }
57
+  }
58
+
59
+  _wsInit () {
60
+    const _self = this
61
+
62
+    this.client = new window.WebSocket(this.addr)
63
+    this.client.onopen = this.on.open
64
+    this.client.onmessage = this.on.message
65
+    this.client.onerror = this.on.error
66
+    this.client.onclose = e => {
67
+      if (_self.on.close) {
68
+        _self.on.close(e)
69
+      }
70
+
71
+      _self._breforeClose(_self.autoConnect)
72
+      _self.client = null
73
+
74
+      if (_self.autoConnect) {
75
+        window.setTimeout(() => _self._wsInit(), _self.delay * 1000)
76
+      }
77
+    }
78
+
79
+    this._wsHeartbeat()
80
+  }
81
+
82
+  _wsHeartbeat () {
83
+    this.interval = window.setInterval(() => {
84
+      if (this.client && this.client.readyState === WsOpen) {
85
+        this.client.send(10)
86
+      }
87
+    }, 60 * 1000)
88
+  }
89
+
90
+  _wsClearHeartbeat () {
91
+    if (this.interval !== undefined) {
92
+      window.clearInterval(this.interval)
93
+    }
94
+  }
95
+}