mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Listen to WebSocket close events
This commit is contained in:
		
							
								
								
									
										32
									
								
								lib/helpers/events_helper.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								lib/helpers/events_helper.dart
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					import 'dart:async';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import 'package:event_bus/event_bus.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Events helper
 | 
				
			||||||
 | 
					///
 | 
				
			||||||
 | 
					/// @author Pierre Hubert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Main WebSocket closed
 | 
				
			||||||
 | 
					class WSClosedEvent {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class EventsHelper {
 | 
				
			||||||
 | 
					  static EventBus _mgr = EventBus();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Listen to event
 | 
				
			||||||
 | 
					  ///
 | 
				
			||||||
 | 
					  /// Do not use this method directly. You should instead prefer to use
 | 
				
			||||||
 | 
					  /// [SafeState.listen] to handle safely widgets lifecycle...
 | 
				
			||||||
 | 
					  ///
 | 
				
			||||||
 | 
					  /// You can not register to global events
 | 
				
			||||||
 | 
					  static StreamSubscription<T> on<T>(void onData(T event)) {
 | 
				
			||||||
 | 
					    if (T == dynamic) throw Exception("Do not register to all events!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    final stream = _mgr.on<T>();
 | 
				
			||||||
 | 
					    return stream.listen(onData);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Propagate an event
 | 
				
			||||||
 | 
					  static void emit<T>(T event) {
 | 
				
			||||||
 | 
					    _mgr.fire(event);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					import 'package:comunic/helpers/events_helper.dart';
 | 
				
			||||||
import 'package:comunic/models/api_request.dart';
 | 
					import 'package:comunic/models/api_request.dart';
 | 
				
			||||||
import 'package:comunic/models/config.dart';
 | 
					import 'package:comunic/models/config.dart';
 | 
				
			||||||
import 'package:web_socket_channel/web_socket_channel.dart';
 | 
					import 'package:web_socket_channel/web_socket_channel.dart';
 | 
				
			||||||
@@ -28,7 +29,7 @@ class WebSocketHelper {
 | 
				
			|||||||
    // First, get an access token
 | 
					    // First, get an access token
 | 
				
			||||||
    final token = await _getWsToken();
 | 
					    final token = await _getWsToken();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Determine websocket URI
 | 
					    // Determine WebSocket URI
 | 
				
			||||||
    final wsURL =
 | 
					    final wsURL =
 | 
				
			||||||
        "${(config().apiServerSecure ? "wss" : "ws")}://${config()
 | 
					        "${(config().apiServerSecure ? "wss" : "ws")}://${config()
 | 
				
			||||||
        .apiServerName}${config().apiServerUri}ws?token=$token";
 | 
					        .apiServerName}${config().apiServerUri}ws?token=$token";
 | 
				
			||||||
@@ -48,7 +49,10 @@ class WebSocketHelper {
 | 
				
			|||||||
      },
 | 
					      },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Notify when the channel is closed
 | 
					      // Notify when the channel is closed
 | 
				
			||||||
      onDone: () => print("WS Channel closed"),
 | 
					      onDone: () {
 | 
				
			||||||
 | 
					        print("WS Channel closed");
 | 
				
			||||||
 | 
					        EventsHelper.emit(WSClosedEvent());
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,7 @@
 | 
				
			|||||||
 | 
					import 'package:comunic/helpers/events_helper.dart';
 | 
				
			||||||
import 'package:comunic/helpers/websocket_helper.dart';
 | 
					import 'package:comunic/helpers/websocket_helper.dart';
 | 
				
			||||||
import 'package:comunic/ui/routes/main_route.dart';
 | 
					import 'package:comunic/ui/routes/main_route.dart';
 | 
				
			||||||
 | 
					import 'package:comunic/ui/widgets/safe_state.dart';
 | 
				
			||||||
import 'package:comunic/utils/intl_utils.dart';
 | 
					import 'package:comunic/utils/intl_utils.dart';
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -17,7 +19,7 @@ class InitializeWidget extends StatefulWidget {
 | 
				
			|||||||
  _InitializeWidgetState createState() => _InitializeWidgetState();
 | 
					  _InitializeWidgetState createState() => _InitializeWidgetState();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class _InitializeWidgetState extends State<InitializeWidget> {
 | 
					class _InitializeWidgetState extends SafeState<InitializeWidget> {
 | 
				
			||||||
  bool _error = false;
 | 
					  bool _error = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
@@ -25,6 +27,9 @@ class _InitializeWidgetState extends State<InitializeWidget> {
 | 
				
			|||||||
    _tryConnect();
 | 
					    _tryConnect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    super.initState();
 | 
					    super.initState();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Listen to WebSocket close event
 | 
				
			||||||
 | 
					    super.listen<WSClosedEvent>((e) => _tryConnect());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Try to connect to server
 | 
					  /// Try to connect to server
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,3 +1,6 @@
 | 
				
			|||||||
 | 
					import 'dart:async';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import 'package:comunic/helpers/events_helper.dart';
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Little State hack to avoid issues
 | 
					/// Little State hack to avoid issues
 | 
				
			||||||
@@ -5,9 +8,26 @@ import 'package:flutter/material.dart';
 | 
				
			|||||||
/// @author Pierre HUBERT
 | 
					/// @author Pierre HUBERT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
abstract class SafeState<T extends StatefulWidget> extends State<T> {
 | 
					abstract class SafeState<T extends StatefulWidget> extends State<T> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  final _subscriptions = List<StreamSubscription>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  void dispose() {
 | 
				
			||||||
 | 
					    // Close subscriptions
 | 
				
			||||||
 | 
					    _subscriptions.forEach((f) => f.cancel());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    super.dispose();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  void setState(fn) {
 | 
					  void setState(fn) {
 | 
				
			||||||
    if(mounted)
 | 
					    if(mounted)
 | 
				
			||||||
      super.setState(fn);
 | 
					      super.setState(fn);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Register to a new subscription
 | 
				
			||||||
 | 
					  @protected
 | 
				
			||||||
 | 
					  void listen<T>(void onEvent(T event)) {
 | 
				
			||||||
 | 
					    _subscriptions.add(EventsHelper.on<T>(onEvent));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -106,6 +106,13 @@ packages:
 | 
				
			|||||||
      url: "https://pub.dartlang.org"
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
    source: hosted
 | 
					    source: hosted
 | 
				
			||||||
    version: "3.0.9"
 | 
					    version: "3.0.9"
 | 
				
			||||||
 | 
					  event_bus:
 | 
				
			||||||
 | 
					    dependency: "direct main"
 | 
				
			||||||
 | 
					    description:
 | 
				
			||||||
 | 
					      name: event_bus
 | 
				
			||||||
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
 | 
					    source: hosted
 | 
				
			||||||
 | 
					    version: "1.1.1"
 | 
				
			||||||
  flutter:
 | 
					  flutter:
 | 
				
			||||||
    dependency: "direct main"
 | 
					    dependency: "direct main"
 | 
				
			||||||
    description: flutter
 | 
					    description: flutter
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -72,6 +72,9 @@ dependencies:
 | 
				
			|||||||
  # Establish WebSocket connections
 | 
					  # Establish WebSocket connections
 | 
				
			||||||
  web_socket_channel: ^1.1.0
 | 
					  web_socket_channel: ^1.1.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # Events manager
 | 
				
			||||||
 | 
					  event_bus: ^1.1.1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
dev_dependencies:
 | 
					dev_dependencies:
 | 
				
			||||||
  flutter_test:
 | 
					  flutter_test:
 | 
				
			||||||
    sdk: flutter
 | 
					    sdk: flutter
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user