浏览代码

任务,消息改为websocket

王通 2 年之前
父节点
当前提交
a7d3fce543
共有 1 个文件被更改,包括 101 次插入1 次删除
  1. 101 1
      src/components/vBottomMenu.vue

+ 101 - 1
src/components/vBottomMenu.vue

@@ -497,7 +497,13 @@ export default {
       searchFB: '',
       filterchang: '', //林场搜索
       btmCurrent: '',
-
+      websock: '',
+      wsuri:'ws://127.0.0.1:10012/messagePush/',
+      wsuri2:'ws://127.0.0.1:10005/taskPush/',
+      setIntervalWesocketPush: null,
+      websockSid: {
+        userId: ''
+      },
       eventWarn: false //webSocket事件问题警报标记
     }
   },
@@ -507,7 +513,99 @@ export default {
       this.deptOptionsLiandong = response.data
     })
   },
+  mounted() {
+    this.websockSid.userId=Cookies.get("userId")
+  },
   methods: {
+    initWebSocket(wsurl,userId) {        //初始化weosocket
+      //const wsuri = 'ws://127.0.0.1:10003/eventPush/' + userId + '/' + eventTypeDl + '/' + eventType
+      const wsuri = wsurl+userId
+      // const wsuri = 'ws://172.28.20.82:10012/taskPush/'+userId
+      this.websock = new WebSocket(wsuri)
+      console.log('建立websocket连接'+wsuri)
+      this.websock.onopen = this.websocketonopen
+      this.websock.onmessage = this.websocketonmessage
+      this.websock.onerror = this.websocketonerror
+    },
+    websocketonopen() { //连接建立之后执行send方法发送数据
+      console.log('websocket连接成功')
+      this.weosocket = true
+      this.sendPing()
+    },
+    websocketonerror() { //连接建立失败重连
+      this.initWebSocket(this.wsuri,this.websockSid.userId)
+    },
+    websocketonmessage(e) { //数据接收
+      console.log('接收数据', e.data)
+      // let data = "{\"fromId\":\"farming\"}";
+      // 处理收到的消息
+      this.handleWebSoceketEvent(e.data)
+    },
+    handleWebSoceketEvent(val) {
+      let that = this
+      let data = JSON.parse(val)
+      console.log('数据数据数据数据',data)
+      /**
+       * that.markersList.filter( item => data.eventCode == item.parameter).length == 0 如果地图中不存在当前事件则添加
+       * eventPush: 事件列表消息
+       * */
+      let  message=data.centermessageTPushrecord
+      let message2=data.centertaskTTask
+      if (null!=message)
+      {
+        console.log(message)
+        this.messageList.push(message)
+        this.messageCount++
+      }else if (null!=message2)
+      {
+        this.taskList.push(message2)
+        this.taskCount ++
+      }
+
+
+
+      // 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)
+      //     }
+      //   })
+      // }
+
+      console.log(data)
+    },
+
+
+    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': 'farming'
+    }) {
+      clearInterval(this.setIntervalWesocketPush)
+      this.setIntervalWesocketPush = setInterval(() => {
+        if (this.weosocket) {
+          this.websock.send(JSON.stringify(ping))
+        } else {
+          // this.initWebSocket()
+        }
+      }, time)
+    },
     filterbanNode(value, data) {
       //树搜索
       if (!value) return true
@@ -697,6 +795,7 @@ export default {
         this.taskList = res.data
         this.taskCount = res.data.length
       })
+      this.initWebSocket(this.wsuri2,this.websockSid.userId)
     },
     selectMessageById(id) {
       selectMessageById(id).then(response => {
@@ -716,6 +815,7 @@ export default {
         this.messageList = response.data
         this.messageCount = response.data.length
       });
+      this.initWebSocket(this.wsuri,this.websockSid.userId)
     },
     handleCheckedCitiesChangeBan(value) {
       this.choseLayerSwitchingList(this.banCheckList)