registerSession method

Future<void> registerSession()

Implementation

Future<void> registerSession() async {
  if (_isClosed) {
    return;
  }

  final bridgeBase = _bridgeUrl;
  var bridgeUrl = '$bridgeBase/$ssePath?client_id=$_sessionId';

  final lastEventId = await _storage.getItem(key: IStorage.keyLastEventId);
  if (lastEventId != null) {
    bridgeUrl += '&last_event_id=$lastEventId';
  }

  logger.d('Bridge url -> $bridgeUrl');

  if (_eventSource != null) {
    _eventSource!.close();
  }

  _eventSource = EventSource(bridgeUrl);

  _eventSource!.onError.listen(_errorsHandler);
  _eventSource!.onOpen.listen((_) {
    resolve.complete(true);
  });

  _eventSource!.onMessage.listen((event) async {
    try {
      await _messagesHandler(event);
    } on TimeoutException {
      logger.e('Bridge error -> TimeoutError');
    } on ClientException {
      logger.e('Bridge error -> ClientConnectionError');
    } catch (e) {
      logger.e('Bridge error -> Unknown');
    }
  });
}