1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2025-06-19 00:05:16 +00:00

Listen to WebSocket close events

This commit is contained in:
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());
},
);
}
}