From 8a1a8689a2d7e236a733ffb822da503d16fcb88e Mon Sep 17 00:00:00 2001
From: qingyiay <2386314947@qq.com>
Date: 星期一, 10 七月 2023 08:45:49 +0800
Subject: [PATCH] websocket增加重连

---
 store/index.js |   93 ++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 84 insertions(+), 9 deletions(-)

diff --git a/store/index.js b/store/index.js
index 1ce1db3..dff5e8e 100644
--- a/store/index.js
+++ b/store/index.js
@@ -78,6 +78,9 @@
 		socketTask: null,
 		websocketData: null, // 瀛樻斁浠庡悗绔帴鏀跺埌鐨剋ebsocket鏁版嵁
 		globalIntervalId: null,
+		is_open_socket: false, //閬垮厤閲嶅杩炴帴
+		connectNum: 1, //閲嶈繛娆℃暟,
+		reconnectTimeOut: null
 	},
 	mutations: {
 		lengthchange(state, payload) {
@@ -131,7 +134,19 @@
 		},
 		setWebsocketData(state, data) {
 			state.websocketData = data
-		}
+		},
+		// 鏀瑰彉閲嶈繛鐘舵��
+		changereconnectState(state, payload) {
+			state.is_open_socket = payload
+		},
+		// 鏀瑰彉閲嶈繛娆℃暟
+		changereconnectNum(state, payload) {
+			if (state.connectNum < 6) {
+				state.connectNum += payload
+			} else {
+				state.connectNum = payload
+			}
+		},
 	},
 	actions: {
 		websocketInit({ state, dispatch, commit }) {
@@ -139,20 +154,29 @@
 			state.socketTast = uni.connectSocket({
 				url: wsUrl,
 				header: { CLIENT_TOC: 'Y' },
-				complete: res => {
-					console.log(res, 'socket缁撴灉')
+				success: res => {
+					console.log(res, 'socketSuccess')
 					if (res.errMsg === 'connectSocket:ok') {
 						commit('changeisconnect', true)
+						commit('changereconnectState', true)
 					}
+				},
+				fail: res => {
+					console.log(res, 'socketFail')
+					console.log('ws澶辫触');
+					commit('changereconnectState', false)
+					dispatch('reconnect')
 				}
 			})
 			state.socketTast.onOpen(() => dispatch('websocketOnOpen'))
 			state.socketTast.onMessage(result => dispatch('websocketOnMessage', result.data))
 			state.socketTast.onClose(e => dispatch('websocketOnClose', e))
-			state.socketTast.onError(e => dispatch('websocketOnError'))
+			state.socketTast.onError(e => dispatch('websocketOnError', e))
 		},
 		websocketOnOpen({ state, commit }) {
-			console.log('onOpen')
+			console.log('ws鎵撳紑')
+			clearInterval(state.reconnectTimeOut)
+			clearInterval(state.globalIntervalId)
 			state.globalIntervalId = setInterval(() => {
 				state.socketTast.send({
 					data: JSON.stringify({ type: 'ping' }),
@@ -162,19 +186,70 @@
 				})
 			}, 30000)
 		},
-		websocketOnClose({ state, commit }) {
+		websocketOnClose({ state, commit, dispatch }, e) {
 			if (!state.socketTast) return
+			console.log('ws鍏抽棴', e)
 			state.socketTast.close(e => {
 				commit('changeisconnect', false)
 			})
-			console.log('ws鍏抽棴')
+			clearInterval(state.globalIntervalId)
+			clearInterval(state.reconnectTimeOut)
+			state.socketTast = null
+			commit('changereconnectState', false)
+			if (state.connectNum < 6 && state.globalisLogin) {
+				uni.showToast({
+					title: `杩炴帴澶辫触锛屾鍦ㄥ皾璇曠${state.connectNum}娆¤繛鎺,
+					icon: 'none'
+				})
+				commit('changereconnectNum', 1)
+				dispatch('reconnect')
+			} else if (state.connectNum > 6 && state.globalisLogin) {
+				commit('changereconnectNum', 1)
+				uni.showToast({
+					title: '缃戠粶寮傚父,璇风◢鍚庨噸璇�',
+					icon: 'none'
+				})
+			} else {
+				return
+			}
 		},
-		websocketOnError(e) {
-			console.log('socket鎶ラ敊', e)
+		websocketOnError({ state, commit, dispatch }, e) {
+			// 濡傛灉閲嶈繛鐘舵�佷负false鍒欎笉杩涜閲嶈繛锛屼负true鎵嶄細
+			console.log('ws鎶ラ敊', e)
+			clearInterval(state.globalIntervalId)
+			clearInterval(state.reconnectTimeOut)
+			state.socketTast = null
+			commit('changereconnectState', false)
+			if (state.connectNum < 6 && state.globalisLogin) {
+				uni.showToast({
+					title: `杩炴帴澶辫触锛屾鍦ㄥ皾璇曠${state.connectNum}娆¤繛鎺,
+					icon: 'none'
+				})
+				commit('changereconnectNum', 1)
+				dispatch('reconnect')
+			} else if (state.connectNum > 6 && state.globalisLogin) {
+				commit('changereconnectNum', 1)
+				uni.showToast({
+					title: '缃戠粶寮傚父,璇风◢鍚庨噸璇�',
+					icon: 'none'
+				})
+			} else {
+				return
+			}
 		},
 		// 鎺ユ敹鏁版嵁
 		websocketOnMessage({ state, commit }, result) {
 			commit('setWebsocketData', result)
+		},
+		// 閲嶆柊杩炴帴
+		reconnect({ state, commit, dispatch }) {
+			console.log(state.is_open_socket, '閲嶆柊杩炴帴socket鐘舵��');
+			clearInterval(state.globalIntervalId)
+			if (!state.is_open_socket && state.globalisLogin) {
+				state.reconnectTimeOut = setInterval(() => {
+					dispatch('websocketInit')
+				}, 5000)
+			}
 		}
 	}
 })

--
Gitblit v1.9.1