TF-3323 Fix web socket still not reconnect when network disconnected
This commit is contained in:
@@ -46,6 +46,8 @@ class NetworkConnectionController extends GetxController {
|
|||||||
super.onClose();
|
super.onClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Connectivity get connectivity => _connectivity;
|
||||||
|
|
||||||
void _getCurrentNetworkConnectionState() async {
|
void _getCurrentNetworkConnectionState() async {
|
||||||
final listConnectionResult = await Future.wait([
|
final listConnectionResult = await Future.wait([
|
||||||
_connectivity.checkConnectivity(),
|
_connectivity.checkConnectivity(),
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ class NetworkConnectionController extends GetxController {
|
|||||||
super.onClose();
|
super.onClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Connectivity get connectivity => _connectivity;
|
||||||
|
|
||||||
void _getCurrentNetworkConnectionState() async {
|
void _getCurrentNetworkConnectionState() async {
|
||||||
final connectionResult = await _connectivity.checkConnectivity();
|
final connectionResult = await _connectivity.checkConnectivity();
|
||||||
log('NetworkConnectionController::_getCurrentNetworkConnectionState():connectionResult: $connectionResult');
|
log('NetworkConnectionController::_getCurrentNetworkConnectionState():connectionResult: $connectionResult');
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'dart:async';
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||||
import 'package:core/presentation/state/failure.dart';
|
import 'package:core/presentation/state/failure.dart';
|
||||||
import 'package:core/presentation/state/success.dart';
|
import 'package:core/presentation/state/success.dart';
|
||||||
import 'package:core/utils/app_logger.dart';
|
import 'package:core/utils/app_logger.dart';
|
||||||
@@ -13,6 +14,8 @@ import 'package:jmap_dart_client/jmap/account_id.dart';
|
|||||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||||
import 'package:jmap_dart_client/jmap/push/state_change.dart';
|
import 'package:jmap_dart_client/jmap/push/state_change.dart';
|
||||||
import 'package:model/extensions/account_id_extensions.dart';
|
import 'package:model/extensions/account_id_extensions.dart';
|
||||||
|
import 'package:tmail_ui_user/features/network_connection/presentation/network_connection_controller.dart'
|
||||||
|
if (dart.library.html) 'package:tmail_ui_user/features/network_connection/presentation/web_network_connection_controller.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/data/model/web_socket_echo_request.dart';
|
import 'package:tmail_ui_user/features/push_notification/data/model/web_socket_echo_request.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/data/model/web_socket_push_enable_request.dart';
|
import 'package:tmail_ui_user/features/push_notification/data/model/web_socket_push_enable_request.dart';
|
||||||
import 'package:tmail_ui_user/features/push_notification/domain/state/web_socket_push_state.dart';
|
import 'package:tmail_ui_user/features/push_notification/domain/state/web_socket_push_state.dart';
|
||||||
@@ -34,6 +37,8 @@ class WebSocketController extends PushBaseController {
|
|||||||
static WebSocketController get instance => _instance;
|
static WebSocketController get instance => _instance;
|
||||||
|
|
||||||
ConnectWebSocketInteractor? _connectWebSocketInteractor;
|
ConnectWebSocketInteractor? _connectWebSocketInteractor;
|
||||||
|
NetworkConnectionController? _networkConnectionController;
|
||||||
|
StreamSubscription<ConnectivityResult>? _connectivitySubscription;
|
||||||
|
|
||||||
int _retryRemained = 3;
|
int _retryRemained = 3;
|
||||||
bool _isConnecting = false;
|
bool _isConnecting = false;
|
||||||
@@ -72,11 +77,16 @@ class WebSocketController extends PushBaseController {
|
|||||||
|
|
||||||
_connectWebSocket();
|
_connectWebSocket();
|
||||||
_listenToAppLifeCycle();
|
_listenToAppLifeCycle();
|
||||||
|
if (PlatformInfo.isWeb) {
|
||||||
|
_monitorNetwork();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onClose() {
|
void onClose() {
|
||||||
_cleanUpWebSocketResources();
|
_cleanUpWebSocketResources();
|
||||||
|
_connectivitySubscription?.cancel();
|
||||||
|
_connectivitySubscription = null;
|
||||||
_appLifecycleListener?.dispose();
|
_appLifecycleListener?.dispose();
|
||||||
_appLifecycleListener = null;
|
_appLifecycleListener = null;
|
||||||
super.onClose();
|
super.onClose();
|
||||||
@@ -89,6 +99,7 @@ class WebSocketController extends PushBaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _handleAppLifecycleStateChange(AppLifecycleState appLifecycleState) async {
|
void _handleAppLifecycleStateChange(AppLifecycleState appLifecycleState) async {
|
||||||
|
log('WebSocketController::_handleAppLifecycleStateChange:appLifecycleState = $appLifecycleState');
|
||||||
switch (appLifecycleState) {
|
switch (appLifecycleState) {
|
||||||
case AppLifecycleState.resumed:
|
case AppLifecycleState.resumed:
|
||||||
if (PlatformInfo.isMobile) {
|
if (PlatformInfo.isMobile) {
|
||||||
@@ -143,6 +154,7 @@ class WebSocketController extends PushBaseController {
|
|||||||
log('WebSocketController::_cleanUpWebSocketResources:');
|
log('WebSocketController::_cleanUpWebSocketResources:');
|
||||||
_isConnecting = false;
|
_isConnecting = false;
|
||||||
_webSocketSubscription?.cancel();
|
_webSocketSubscription?.cancel();
|
||||||
|
_webSocketChannel?.sink.close();
|
||||||
_webSocketChannel = null;
|
_webSocketChannel = null;
|
||||||
_webSocketPingTimer?.cancel();
|
_webSocketPingTimer?.cancel();
|
||||||
_stateChangeDebouncer?.cancel();
|
_stateChangeDebouncer?.cancel();
|
||||||
@@ -239,4 +251,19 @@ class WebSocketController extends PushBaseController {
|
|||||||
logError('WebSocketController::_handleStateChange:Exception = $e');
|
logError('WebSocketController::_handleStateChange:Exception = $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _monitorNetwork() {
|
||||||
|
_networkConnectionController = getBinding<NetworkConnectionController>();
|
||||||
|
_connectivitySubscription = _networkConnectionController
|
||||||
|
?.connectivity
|
||||||
|
.onConnectivityChanged.listen((status) {
|
||||||
|
if (status == ConnectivityResult.none) {
|
||||||
|
log('WebSocketController::_monitorNetwork:No network connection');
|
||||||
|
_cleanUpWebSocketResources();
|
||||||
|
} else {
|
||||||
|
log('WebSocketController::_monitorNetwork:Network connection restore');
|
||||||
|
_connectWebSocket();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user