Reconnect the network when we switch network types (#8034)

This commit is contained in:
Daniel Espino García 2024-10-29 10:14:31 +01:00 committed by GitHub
parent 3a712350a8
commit 2e8c9ce9a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 10 deletions

View file

@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import NetInfo, {type NetInfoState, type NetInfoSubscription} from '@react-native-community/netinfo';
import NetInfo, {NetInfoStateType, type NetInfoState, type NetInfoSubscription} from '@react-native-community/netinfo';
import {AppState, type AppStateStatus, type NativeEventSubscription} from 'react-native';
import BackgroundTimer from 'react-native-background-timer';
import {BehaviorSubject} from 'rxjs';
@ -30,6 +30,7 @@ class WebsocketManager {
private connectionTimerIDs: Record<string, NodeJS.Timeout> = {};
private isBackgroundTimerRunning = false;
private netConnected = false;
private netType: NetInfoStateType = NetInfoStateType.none;
private previousActiveState: boolean;
private statusUpdatesIntervalIDs: Record<string, NodeJS.Timeout> = {};
private backgroundIntervalId: number | undefined;
@ -43,7 +44,9 @@ class WebsocketManager {
}
public init = async (serverCredentials: ServerCredential[]) => {
this.netConnected = Boolean((await NetInfo.fetch()).isConnected);
const netInfo = await NetInfo.fetch();
this.netConnected = Boolean(netInfo.isConnected);
this.netType = netInfo.type;
serverCredentials.forEach(
({serverUrl, token}) => {
try {
@ -101,6 +104,7 @@ class WebsocketManager {
for (const url of Object.keys(this.clients)) {
const client = this.clients[url];
client.close(true);
client.invalidate();
this.getConnectedSubject(url).next('not_connected');
}
};
@ -227,35 +231,40 @@ class WebsocketManager {
}
const isActive = appState === 'active';
this.handleStateChange(this.netConnected, isActive);
this.handleStateChange(this.netConnected, this.netType, isActive);
};
private onNetStateChange = (netState: NetInfoState) => {
const newState = Boolean(netState.isConnected);
if (this.netConnected === newState) {
return;
}
this.handleStateChange(newState, this.previousActiveState);
this.handleStateChange(newState, netState.type, this.previousActiveState);
};
private handleStateChange = (currentIsConnected: boolean, currentIsActive: boolean) => {
if (currentIsActive === this.previousActiveState && currentIsConnected === this.netConnected) {
private handleStateChange = (currentIsConnected: boolean, currentNetType: NetInfoStateType, currentIsActive: boolean) => {
if (currentIsActive === this.previousActiveState && currentIsConnected === this.netConnected && currentNetType === this.netType) {
return;
}
this.cancelConnectTimers();
const wentBackground = this.previousActiveState && !currentIsActive;
const switchedNetworks = currentIsConnected && currentNetType !== this.netType && this.netType !== 'none';
this.previousActiveState = currentIsActive;
this.netConnected = currentIsConnected;
this.netType = currentNetType;
if (!currentIsConnected) {
this.closeAll();
return;
}
if (switchedNetworks) {
// Close all connections when we switch from (for example) vpn to wifi
// to ensure we are using the right network and doesn't get stuck on
// retries.
this.closeAll();
}
if (currentIsActive) {
if (this.isBackgroundTimerRunning) {
BackgroundTimer.clearInterval(this.backgroundIntervalId!);

View file

@ -0,0 +1,13 @@
diff --git a/node_modules/@mattermost/react-native-network-client/android/src/main/java/com/mattermost/networkclient/WebSocketClientModuleImpl.kt b/node_modules/@mattermost/react-native-network-client/android/src/main/java/com/mattermost/networkclient/WebSocketClientModuleImpl.kt
index 42f620b..d03cf08 100644
--- a/node_modules/@mattermost/react-native-network-client/android/src/main/java/com/mattermost/networkclient/WebSocketClientModuleImpl.kt
+++ b/node_modules/@mattermost/react-native-network-client/android/src/main/java/com/mattermost/networkclient/WebSocketClientModuleImpl.kt
@@ -113,7 +113,7 @@ class WebSocketClientModuleImpl(reactApplicationContext: ReactApplicationContext
}
try {
- clients[wsUri]!!.webSocket!!.close(1000, "manual")
+ clients[wsUri]!!.webSocket!!.cancel()
} catch (error: Exception) {
promise.reject(error)
}