Reconnect the network when we switch network types (#8034)
This commit is contained in:
parent
3a712350a8
commit
2e8c9ce9a5
2 changed files with 32 additions and 10 deletions
|
|
@ -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!);
|
||||
|
|
|
|||
13
patches/@mattermost+react-native-network-client+1.6.0.patch
Normal file
13
patches/@mattermost+react-native-network-client+1.6.0.patch
Normal 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)
|
||||
}
|
||||
Loading…
Reference in a new issue