瀏覽代碼

长连接修改

wy 1 年之前
父節點
當前提交
e3016bb063
共有 4 個文件被更改,包括 128 次插入53 次删除
  1. 51 24
      pages/equipment-monitoring/index.vue
  2. 36 7
      pages/index/index.vue
  3. 41 22
      pages/power-changer/index.vue
  4. 0 0
      unpackage/dist/dev/app-plus/manifest.json

+ 51 - 24
pages/equipment-monitoring/index.vue

@@ -15,10 +15,10 @@
 		</view>
 		<view class="main">
 			<view class="main_one" v-if="isSlecte">
-				<automatic :robotInfoVo = "robotInfoVo" :robotcode = 'robotcode' ></automatic>
+				<automatic :robotInfoVo="robotInfoVo" :robotcode='robotcode'></automatic>
 			</view>
 			<view class="main_two" v-if="!isSlecte">
-				<manual :storeInfoLists = "storeInfoLists"></manual>
+				<manual :storeInfoLists="storeInfoLists"></manual>
 			</view>
 		</view>
 	</view>
@@ -34,26 +34,46 @@
 		},
 		data() {
 			return {
-				isSlecte: true,//切换页面
-				getStationCode: null,//这个暂时没有
-				ws: null,//websuokit 链接器
-				timer: null,//记录链接的方法   方便清除
-				storeInfoLists:[],//储充设备数据
-				robotInfoVo:{},//机器人数据
-				robotcode:0,//机器人模式
+				isSlecte: true, //切换页面
+				getStationCode: null, //这个暂时没有
+				ws: null, //websuokit 链接器
+				timer: null, //记录链接的方法   方便清除
+				storeInfoLists: [], //储充设备数据
+				robotInfoVo: {}, //机器人数据
+				robotcode: 0, //机器人模式
+				heartbeatTimer: null,
+				onMessageTime: null,
 			}
 		},
 		mounted() {
-
+			this.onMessageTime = new Date();
 			this.InitWs()
+			this.heartbeatTimer = setInterval(() => {
+				let newDate = new Date();
+				if (newDate - this.onMessageTime > 3000) {
+					if (this.ws != null) {
+						this.ws.close()
+						this.ws = null
+					}
+				}
+			}, 3000)
 
 		},
 
 		onUnload() {
 			console.log('onUnload')
-			clearInterval(this.timer)
-			this.timer = null
-			this.ws.close()
+			if(this.timer!=null){
+				clearInterval(this.timer)
+				this.timer = null
+			}
+			if(this.ws!=null){
+				this.ws.close()
+				this.ws=null
+			}
+			if(this.heartbeatTimer!=null){
+				this.heartbeatTimer.close()
+				this.heartbeatTimer=null
+			}
 		},
 		computed: {
 			pageState() {
@@ -78,7 +98,7 @@
 			}
 		},
 		methods: {
-			
+
 			//判断websocket是否链接上
 			InitWs() {
 				let token = this.$storage.getJson("token");
@@ -107,14 +127,17 @@
 					// uni.showLoading({
 					// 	title: '数据加载中...'
 					// });
-					 let wsMessage={
-					        type: 'page',
-					        payload: 'equipment-monitoring'
-					      }
-						  let data = JSON.stringify(wsMessage)
-					      this.ws.send({data})
+					let wsMessage = {
+						type: 'page',
+						payload: 'equipment-monitoring'
+					}
+					let data = JSON.stringify(wsMessage)
+					this.ws.send({
+						data
+					})
 				})
 				this.ws.onMessage((res) => {
+					this.onMessageTime = new Date();
 					let data = JSON.parse(res.data)
 					// console.log(data,'resws')
 					if (res) {
@@ -139,12 +162,16 @@
 				this.ws.onError(() => {
 					console.log("WebSocket连接错误")
 				})
-				if (this.timer == null) {
+				if(this.timer == null){
+					//检查断开重连
 					this.timer = setInterval(() => {
-						if (this.ws.readyState != 1) {
-							clearInterval(this.timer)
-							this.timer = null
+						if(this.ws==null){
+							console.log('ws为null,检查断开重连')
+							this.InitWs()
+						}else if(this.ws.readyState != 1){
 							this.ws.close()
+							this.ws = null
+							console.log('未连接成功,检查断开重连')
 							this.InitWs()
 						}
 					}, 2000)

+ 36 - 7
pages/index/index.vue

@@ -155,10 +155,22 @@
 				dataView: {},
 				robotState: 0,
 				stateList: [],
+				heartbeatTimer:null,
+				onMessageTime:null,
 			}
 		},
 		mounted() {
+			this.onMessageTime = new Date();
 			this.InitWs()
+			this.heartbeatTimer = setInterval(()=>{
+				let newDate = new Date();
+				if(newDate-this.onMessageTime>3000){
+					if(this.ws!=null){
+						this.ws.close()
+						this.ws=null
+					}
+				}
+			},3000)
 			this.getDataView()
 		},
 		computed: {
@@ -232,6 +244,7 @@
 					})
 				})
 				this.ws.onMessage((res) => {
+					this.onMessageTime = new Date();
 					let d = JSON.parse(res.data)
 					// console.log(d)
 					this.stateList = d.stateList
@@ -245,21 +258,37 @@
 				this.ws.onError(() => {
 					console.log("WebSocket连接错误")
 				})
-				if (this.timer == null) {
+				if(this.timer == null){
+					//检查断开重连
 					this.timer = setInterval(() => {
-						if (this.ws.readyState != 1) {
-							clearInterval(this.timer)
-							this.timer = null
+						if(this.ws==null){
+							console.log('ws为null,检查断开重连')
+							this.InitWs()
+						}else if(this.ws.readyState != 1){
 							this.ws.close()
+							this.ws = null
+							console.log('未连接成功,检查断开重连')
 							this.InitWs()
 						}
 					}, 2000)
 				}
 			},
 		},
-		onLoad() {
-
-		}
+		onUnload() {
+			console.log('onUnload')
+			if(this.timer!=null){
+				clearInterval(this.timer)
+				this.timer = null
+			}
+			if(this.ws!=null){
+				this.ws.close()
+				this.ws=null
+			}
+			if(this.heartbeatTimer!=null){
+				this.heartbeatTimer.close()
+				this.heartbeatTimer=null
+			}
+		},
 	}
 </script>
 

+ 41 - 22
pages/power-changer/index.vue

@@ -51,7 +51,7 @@
 						</view>
 						<view class="carRight">
 							<PannelTitle words="车牌识别" :value="vehicleInfo.plateState ? '成功' : '未识别'"
-								:flag="vehicleInfo.plateComState == 1" />
+								:flag="vehicleInfo.plateComState" />
 						</view>
 
 					</view>
@@ -318,17 +318,37 @@
 				showStore: 0, //0换电展示 1调仓展示  2换仓展示
 				TcInfoVoData: {}, //调仓信息对象
 				xfTargetCodeData: {}, //消防换仓对象
+				heartbeatTimer:null,
+				onMessageTime:null,
 			}
 		},
 		mounted() {
+			this.onMessageTime = new Date();
 			this.InitWs()
-			
+			this.heartbeatTimer = setInterval(()=>{
+				let newDate = new Date();
+				if(newDate-this.onMessageTime>3000){
+					if(this.ws!=null){
+						this.ws.close()
+						this.ws=null
+					}
+				}
+			},3000)
 		},
 		onUnload() {
 			console.log('onUnload')
-			clearInterval(this.timer)
-			this.timer = null
-			this.ws.close()
+			if(this.timer!=null){
+				clearInterval(this.timer)
+				this.timer = null
+			}
+			if(this.ws!=null){
+				this.ws.close()
+				this.ws=null
+			}
+			if(this.heartbeatTimer!=null){
+				this.heartbeatTimer.close()
+				this.heartbeatTimer=null
+			}
 		},
 		computed: {
 			//换电时长
@@ -752,18 +772,6 @@
 					complete: () => {}
 				})
 				this.ws.onOpen(() => {
-					// let msg = {
-					// 	type: "subscribeExchange",
-					// 	payLoad: {
-					// 		exchangeNo: exchangeNo
-					// 	}
-					// }
-					// this.ws.send({
-					// 	data: JSON.stringify(msg)
-					// })
-					// uni.showLoading({
-					// 	title: '数据加载中...'
-					// });
 					console.log('socket连接成功')
 					let msg = {
 						type: "page",
@@ -774,6 +782,8 @@
 					})
 				})
 				this.ws.onMessage((res) => {
+					this.onMessageTime = new Date();
+					
 					let d = JSON.parse(res.data)
 					console.log(d)
 					// uni.hideLoading()
@@ -833,7 +843,7 @@
 				})
 				// 监听WebSocket关闭事件
 				this.ws.onClose(() => {
-					console.log('WebSocket连接已关闭!');
+					console.log('WebSocket连接已关闭!')
 				})
 				this.ws.onError(() => {
 					console.log("WebSocket连接错误")
@@ -841,15 +851,18 @@
 				if(this.timer == null){
 					//检查断开重连
 					this.timer = setInterval(() => {
-						if (this.ws.readyState != 1) {
-							clearInterval(this.timer)
-							this.timer = null
+						if(this.ws==null){
+							console.log('ws为null,检查断开重连')
+							this.InitWs()
+						}else if(this.ws.readyState != 1){
 							this.ws.close()
-							console.log('检查断开重连')
+							this.ws = null
+							console.log('未连接成功,检查断开重连')
 							this.InitWs()
 						}
 					}, 2000)
 				}
+				
 			},
 		},
 	}
@@ -1127,4 +1140,10 @@
 			}
 		}
 	}
+	.carLeft{
+		color: #fff !important;
+	}
+	.carRight{
+		color: #fff !important;
+	}
 </style>

文件差異過大導致無法顯示
+ 0 - 0
unpackage/dist/dev/app-plus/manifest.json


部分文件因文件數量過多而無法顯示