Explorar o código

socket重连优化

zhengxin %!s(int64=5) %!d(string=hai) anos
pai
achega
4fdaa0e8a7
Modificáronse 1 ficheiros con 13 adicións e 9 borrados
  1. 13 9
      ghs/src/utils/socket.js

+ 13 - 9
ghs/src/utils/socket.js

@@ -27,15 +27,7 @@ class SocketUtil {
 		this.reconnectHandler = null;
 		this.lockReconnect = false;
 
-		this.socket.onOpen(this.openSocket);
-		// 监听消息
-
-		this.socket.onMessage(this.onMessageSocket);
-
-		// 监听Socket的关闭
-		this.socket.onClose(this.closeSocket);
-
-		this.socket.onError(this.errorSocket);
+		this.bindHandlerEvent();
 
 		// uni.onSocketOpen(function(res) {
 		// 	console.log("WebSocket连接已打开!");
@@ -47,6 +39,17 @@ class SocketUtil {
 		// 	uni.closeSocket();
 		// });
 	}
+	bindHandlerEvent() {
+		this.socket.onOpen(this.openSocket);
+		// 监听消息
+
+		this.socket.onMessage(this.onMessageSocket);
+
+		// 监听Socket的关闭
+		this.socket.onClose(this.closeSocket);
+
+		this.socket.onError(this.errorSocket);
+	}
 	connectSocket(url) {
 		let path = `ws://${url}`;
 		// #ifdef  MP-WEIXIN
@@ -104,6 +107,7 @@ class SocketUtil {
 		if (this.retryTime < SocketConfig.retry) {
 			this.reconnectHandler = setTimeout(() => {
 				this.socket = this.connectSocket(this.url);
+				this.bindHandlerEvent();
 				this.lockReconnect = false;
 			}, 5000);
 			this.retryTime++;