فهرست منبع

websocket修改

王通 2 سال پیش
والد
کامیت
1244607b62
1فایلهای تغییر یافته به همراه106 افزوده شده و 2 حذف شده
  1. 106 2
      src/components/vBottomMenu.vue

+ 106 - 2
src/components/vBottomMenu.vue

@@ -396,6 +396,9 @@ export default {
       this.$refs.changDept.filter(val)
     }
   },
+  mounted() {
+    this.websockSid.userId=Cookies.get("userId")
+  },
   data() {
     return {
       playVideo: '',//视频预览地址
@@ -414,7 +417,9 @@ export default {
       taskStatusButton: null,//任务按钮
       visuForestCloudYuAnBo: null,
       address: null,//事件上报地址
-
+      websockSid: {
+        userId: ''
+      },
       taskId: null,//任务ID
       showDeptConfirm: false,//任务领取选择部门弹窗
       deptOptions: [],//任务领取部门列表
@@ -425,7 +430,8 @@ export default {
       taskList: 0,//任务列表
       messageCount: 0,//任务数量
       messageList: [],//任务列表
-
+      wsurl:'',
+      postName:'',
       btmTipIndent: '', //图例收起弹出
       eventLocationVisible: false,
       showChild: false,
@@ -595,6 +601,7 @@ export default {
         }
       })
     },
+
     refusedTask(taskId, eventCode) {
       let param = { taskId: taskId, eventCode: eventCode, centerTaskTaskDepts: this.centerTaskTaskDepts }
       refusedTask(param).then(res => {
@@ -610,6 +617,101 @@ export default {
         }
       })
     },
+    initWebSocket(wsurl,postName,userId) {
+      this.postName=postName
+      this.wsurl=wsurl
+      selectConfigKey(wsurl).then(res => {
+        //初始化weosocket
+        //const wsuri = 'ws://127.0.0.1:10003/eventPush/' + userId + '/' + eventTypeDl + '/' + eventType
+        const wsuri = res.data +postName+ 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.wsurl,this.postName,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)
+    },
+
     selectTaskDtpts(taskId, eventCode, state) {
       this.taskId = taskId
       this.eventCode = eventCode
@@ -640,6 +742,7 @@ export default {
         this.messageList = response.data
         this.messageCount = response.data.length
       })
+      this.initWebSocket('MESSAGE_SOCKET','/messagePush/',this.websockSid.userId)
     },
     cancelEventShow() {
       console.log('关闭事件弹窗')
@@ -713,6 +816,7 @@ export default {
         this.taskList = res.data
         this.taskCount = res.data.length
       })
+      this.initWebSocket('TASK_SOCKET','/taskPush/',this.websockSid.userId)
     },
     handleCheckedCitiesChangeBan(value) {
       this.choseLayerSwitchingList(this.banCheckList)