Переглянути джерело

修改WebSocket接收事件列表事件

JX.LI 2 роки тому
батько
коміт
d86d5bac4c
1 змінених файлів з 69 додано та 47 видалено
  1. 69 47
      src/views/forest.vue

+ 69 - 47
src/views/forest.vue

@@ -1158,9 +1158,8 @@ export default {
       })
     },
     /** ----------------------------------weosocket开始------------------------------------- */
-    initWebSocket() { //初始化weosocket
-      // const wsuri = 'ws://127.0.0.1:10003/webSocket/' + Cookies.get('username')
-      const wsuri = 'ws://127.0.0.1:10003/webSocket/admin'
+    initWebSocket(userId, eventTypeDl, eventType) {        //初始化weosocket
+      const wsuri = 'ws://127.0.0.1:10003/eventPush/' + userId + '/' + eventTypeDl + '/' + eventType
       this.websock = new WebSocket(wsuri)
       console.log('建立websocket连接')
       this.websock.onopen = this.websocketonopen
@@ -1173,57 +1172,80 @@ export default {
       this.sendPing()
     },
     websocketonerror() { //连接建立失败重连
-      this.initWebSocket()
+      this.initWebSocket(this.websockSid.userId, this.websockSid.eventTypeDl, this.websockSid.eventType,)
     },
     websocketonmessage(e) { //数据接收
       console.log('接收数据', e.data)
-      // let data = "{\"fromId\":\"forest\",\"fromUserId\":\""+Cookies.get('username')+"\",\"toUserId\":\""+Cookies.get('username')+"\"}";
-      let data = "{\"fromId\":\"forest\",\"fromUserId\":\"admin\",\"toUserId\":\"admin\"}";
+      let data = "{\"fromId\":\"forest\",\"fromUserId\":\""+Cookies.get('username')+"\",\"toUserId\":\""+Cookies.get('username')+"\"}";
       if (this.calendarDay == this.getCurrentDataStr() && data != e.data) {
-        this.getWebSocketEvent(e.data);
+        // 处理收到的消息
+        this.handleWebSoceketEvent(e.data)
 
-          // this.getEventListNew();
-          // this.getTodayEvents(this.getCurrentDataStr());
-          // this.getDeptEventCount(this.getCurrentDataStr());
-          // this.getEventByEventType(this.getCurrentDataStr());
-          // this.getEventByReportorOrder(this.getCurrentDataStr());
+        // this.getEventListNew();
+        // this.getTodayEvents(this.getCurrentDataStr());
+        // this.getDeptEventCount(this.getCurrentDataStr());
+        // this.getEventByEventType(this.getCurrentDataStr());
+        // this.getEventByReportorOrder(this.getCurrentDataStr());
 
-          // this.$refs.bottomMenu.updateAlert();
-          // this.$refs.up.play();
-          // thes.$refs.up.pause();//停止播放音乐
-        }
-      },
-      websocketsend(Data) { //数据发送
-        this.websock.send(Data)
-      },
-      websocketclose(e) { //关闭
-        console.log('断开连接', e)
-        // clearInterval(this.setIntervalWesocketPush)
-        this.weosocket = false
-      },
-      /**发送心跳
-       * @param {number} time 心跳间隔毫秒 默认5000
-       * @param {string} ping 心跳名称 默认字符串ping
-       */
-      sendPing(time = 60000, ping = {
-        'fromId': 'forest',
-        'fromUserId': Cookies.get('username'),
-        'toUserId': Cookies.get('username')
-      }) {
-        clearInterval(this.setIntervalWesocketPush)
-        this.setIntervalWesocketPush = setInterval(() => {
-          if (this.weosocket) {
-            this.websock.send(JSON.stringify(ping))
-          } else {
-            this.initWebSocket()
+        // this.$refs.bottomMenu.updateAlert();
+        // this.$refs.up.play();
+        // thes.$refs.up.pause();//停止播放音乐
+      }
+    },
+    // 处理WebSocket事件
+    handleWebSoceketEvent(val) {
+      let that = this
+      let data = JSON.parse(val)
+      /**
+       * that.markersList.filter( item => data.eventCode == item.parameter).length == 0 如果地图中不存在当前事件则添加
+       * eventPush: 事件列表消息
+       * */
+      if (data.tag == "eventPush" && that.markersList.filter(item => data.eventCode == item.parameter).length == 0) {
+        getEventPush({ eventCode: data.eventCode }).then((res) => {
+          if (res.data != undefined) {
+            //插入到第一条
+            this.eventList.unshift(res.data)
+            // 插入后删除最后一条 保证列表中为10条数据
+            if (this.eventList.length > 9)
+              this.eventList.splice(10, 1)
+            // 将收到的数据在地图上添加
+            this.getWebSocketEvent(res.data)
           }
-        }, time)
-      },
-      stopAudio() {
-        this.$refs.up.pause(); //停止播放音乐
-        this.$refs.up.currentTime = 0;
-      },
-      /** ----------------------------------weosocket结束------------------------------------- */
+        })
+      }
+      console.log(that.eventList[0])
+    },
+    websocketsend(Data) { //数据发送
+      this.websock.send(Data)
+    },
+    websocketclose(e) { //关闭
+      console.log('断开连接', e)
+      // clearInterval(this.setIntervalWesocketPush)
+      this.weosocket = false
+    },
+    /**发送心跳
+     * @param {number} time 心跳间隔毫秒 默认5000
+     * @param {string} ping 心跳名称 默认字符串ping
+     */
+    sendPing(time = 60000, ping = {
+      'fromId': 'forest',
+      'fromUserId': Cookies.get('username'),
+      'toUserId': Cookies.get('username')
+    }) {
+      clearInterval(this.setIntervalWesocketPush)
+      this.setIntervalWesocketPush = setInterval(() => {
+        if (this.weosocket) {
+          this.websock.send(JSON.stringify(ping))
+        } else {
+          // this.initWebSocket()
+        }
+      }, time)
+    },
+    stopAudio() {
+      this.$refs.up.pause(); //停止播放音乐
+      this.$refs.up.currentTime = 0;
+    },
+    /** ----------------------------------weosocket结束------------------------------------- */
 
       /** ----------------------------------底部按钮公用组件开始------------------------------------- */
       bottomMenuList() {