1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00

Listen to WebSocket close events

This commit is contained in:
Pierre HUBERT 2020-04-18 13:48:21 +02:00
parent de3cd9c7b7
commit 36f89a9a53
6 changed files with 74 additions and 3 deletions

View 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);
}
}

View File

@ -1,3 +1,4 @@
import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/config.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
@ -28,7 +29,7 @@ class WebSocketHelper {
// First, get an access token
final token = await _getWsToken();
// Determine websocket URI
// Determine WebSocket URI
final wsURL =
"${(config().apiServerSecure ? "wss" : "ws")}://${config()
.apiServerName}${config().apiServerUri}ws?token=$token";
@ -48,7 +49,10 @@ class WebSocketHelper {
},
// Notify when the channel is closed
onDone: () => print("WS Channel closed"),
onDone: () {
print("WS Channel closed");
EventsHelper.emit(WSClosedEvent());
},
);
}
}

View File

@ -1,5 +1,7 @@
import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/helpers/websocket_helper.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:flutter/material.dart';
@ -17,7 +19,7 @@ class InitializeWidget extends StatefulWidget {
_InitializeWidgetState createState() => _InitializeWidgetState();
}
class _InitializeWidgetState extends State<InitializeWidget> {
class _InitializeWidgetState extends SafeState<InitializeWidget> {
bool _error = false;
@override
@ -25,6 +27,9 @@ class _InitializeWidgetState extends State<InitializeWidget> {
_tryConnect();
super.initState();
// Listen to WebSocket close event
super.listen<WSClosedEvent>((e) => _tryConnect());
}
/// Try to connect to server

View File

@ -1,3 +1,6 @@
import 'dart:async';
import 'package:comunic/helpers/events_helper.dart';
import 'package:flutter/material.dart';
/// Little State hack to avoid issues
@ -5,9 +8,26 @@ import 'package:flutter/material.dart';
/// @author Pierre HUBERT
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
void setState(fn) {
if(mounted)
super.setState(fn);
}
/// Register to a new subscription
@protected
void listen<T>(void onEvent(T event)) {
_subscriptions.add(EventsHelper.on<T>(onEvent));
}
}

View File

@ -106,6 +106,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: "direct main"
description: flutter

View File

@ -72,6 +72,9 @@ dependencies:
# Establish WebSocket connections
web_socket_channel: ^1.1.0
# Events manager
event_bus: ^1.1.1
dev_dependencies:
flutter_test:
sdk: flutter