heyang 4 years ago
parent
commit
ea37d344c6
2 changed files with 52 additions and 33 deletions
  1. 25 15
      screenAndroid/WXdraw.js
  2. 27 18
      screenAndroid/WXtrialInterface.html

+ 25 - 15
screenAndroid/WXdraw.js

@@ -33,8 +33,20 @@ form.domainName = parameters["domainName"];
 var isWSS = false;
 var cUrl = isWSS ? "wss://" + form.domainName + "/card?cardIp=" + form.ip : "ws://" + form.domainName + "/card?cardIp=" + form.ip;
 
-var wsss;
+var wsss,errorTime = 0;
 doConnect();
+function throttle(fn, delay) {
+	let flag = true;
+	errorTime += delay;
+	return () => {
+		if (!flag) return;
+		flag = false;
+		timer = setTimeout(() => {
+		fn();
+		flag = true;
+		}, delay);
+	};
+}
 
 function doConnect() {
 	wsss = new WebSocket(cUrl);
@@ -56,24 +68,22 @@ function doConnect() {
 	wsss.onclose = function (evt) {
 		console.log(evt);
 		ws.close();
-		doConnect();
-		// $.confirm({
-		// 	title: '提示',
-		// 	text: '连接断开,是否准备重连?',
-		// 	onOK: function () {
-		// 		doConnect();
-		// 	},
-		// 	onCancel: function () {
-		// 		wx.miniProgram.switchTab({
-		// 			url: '/pages/home/home'
-		// 		})
-		// 	}
-		// });
+		throttle(doConnect,500);
+		if(errorTime > 5000){
+			wx.miniProgram.switchTab({
+				url: '/pages/home/home'
+			})
+		}
 	};
 	wsss.onerror = function (evt) {
 		console.log(evt);
 		ws.close();
-		doConnect();
+		throttle(doConnect,500);
+		if(errorTime > 5000){
+			wx.miniProgram.switchTab({
+				url: '/pages/home/home'
+			})
+		}
 		// $.confirm({
 		// 	title: '提示',
 		// 	text: '连接断开,是否准备重连?',

+ 27 - 18
screenAndroid/WXtrialInterface.html

@@ -222,29 +222,34 @@
 				ExexuteKeyDown(e.keyCode);
 			}
 		}
-		var ws;
+		var ws,errorTime = 0;
 		doConnect();
+		// 节流
+		// 设置一个标志
+		function throttle(fn, delay) {
+			let flag = true;
+			errorTime += delay;
+			return () => {
+				if (!flag) return;
+				flag = false;
+				timer = setTimeout(() => {
+				fn();
+				flag = true;
+				}, delay);
+			};
+		}
 		function doConnect() {
 			ws = new WebSocket(socketURL);
 			ws.binaryType = 'arraybuffer';
 
 			ws.onclose = function (e) {
 				ws.close();
-				doConnect();
-				// console.log(e);
-				// $.confirm({
-				// 	title: '提示',
-				// 	text: '连接断开,是否准备重连?',
-				// 	onOK: function () {
-				// 		window.location.reload();
-				// 		// doConnect();
-				// 	},
-				// 	onCancel: function () {
-				// 		wx.miniProgram.switchTab({
-				// 			url: '/pages/home/home'
-				// 		})
-				// 	}
-				// });
+				throttle(doConnect,500);
+				if(errorTime > 5000){
+					wx.miniProgram.switchTab({
+						url: '/pages/home/home'
+					})
+				}
 			}
 
 			ws.addEventListener('open', function (event) {
@@ -252,9 +257,13 @@
 			});
 
 			ws.addEventListener('error', function (event) {
-				console.log(event);
 				ws.close();
-				doConnect();
+				throttle(doConnect,500);
+				if(errorTime > 5000){
+					wx.miniProgram.switchTab({
+						url: '/pages/home/home'
+					})
+				}
 				// $.confirm({
 				// 	title: '提示',
 				// 	text: '连接失败,是否准备重连?',