Browse Source

任务,消息改为websocket

王通 2 years ago
parent
commit
741c8acef3
2 changed files with 119 additions and 13 deletions
  1. 118 13
      src/components/vBottomMenu.vue
  2. 1 0
      src/views/forest.vue

+ 118 - 13
src/components/vBottomMenu.vue

@@ -347,16 +347,16 @@
 </template>
 
 <script>
-	import {
-		selectTaskBO,
-		getEventDetail,
-		selectTaskDtpts,
-		receiveTask,
-		refusedTask,
-    selectMessageCount,
-    selectMessageList,
-    selectMessageById
-	} from '@/api/forest'
+import {
+  selectTaskBO,
+  getEventDetail,
+  selectTaskDtpts,
+  receiveTask,
+  refusedTask,
+  selectMessageCount,
+  selectMessageList,
+  selectMessageById, getEventPush
+} from '@/api/forest'
 	import {
 		selectFarmByDeptId
 	} from '@/api/vBottomMenu'
@@ -409,9 +409,16 @@
 
 				taskCount: 0, //任务数量
 				taskList: 0, //任务列表
-
+        weosocket: false,
+        websock: '',
+        wsuri:'ws://192.168.3.60:10012/messagePush/',
+        wsuri2:'ws://192.168.3.60:10005/taskPush/',
+        setIntervalWesocketPush: null,
+        websockSid: {
+          userId: ''
+        },
         messageCount: 0, //消息数量
-        messageList: 0, //消息列表
+        messageList: [], //消息列表
 
 				btmTipIndent: '', //图例收起弹出
 				eventLocationVisible: false,
@@ -500,13 +507,19 @@
 				eventWarn: false, //webSocket事件问题警报标记
 			}
 		},
+    beforeDestroy() {
+      // this.websock.close();
+    },
 		created() {
 			/** 查询部门树结构 */
 			deptTreeselect().then(response => {
 				this.deptOptionsLiandong = response.data
 			})
     },
-		methods: {
+    mounted() {
+      this.websockSid.userId=Cookies.get("userId")
+    },
+    methods: {
 			filterbanNode(value, data) {
 				//树搜索
 				if (!value) return true
@@ -690,12 +703,103 @@
 					}, 1000)
 				})
 			},
+      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)
+      },
+
 			selectTaskList: async function() {
 				//获取任务列表
 				selectTaskBO().then(res => {
 					this.taskList = res.data
 					this.taskCount = res.data.length
 				})
+        this.initWebSocket(this.wsuri2,this.websockSid.userId)
 			},
       selectMessageById(id) {
         selectMessageById(id).then(response => {
@@ -715,6 +819,7 @@
           this.messageList = response.data
           this.messageCount = response.data.length
         });
+        this.initWebSocket(this.wsuri,this.websockSid.userId)
       },
 			handleCheckedCitiesChangeBan(value) {
 				this.choseLayerSwitchingList(this.banCheckList)

+ 1 - 0
src/views/forest.vue

@@ -626,6 +626,7 @@
       //     }
       //   })
       // },
+
       dropLocation(lat, lng,eventCode) {
         this.$refs.supermap.dropLocation(lat, lng,18)
         this.showEventDialog(eventCode)