listen method Null safety

void listen()

Starts listening for data.

Implementation

void listen() {
  _socket?.stream.listen(
      (data) {
        _onData.add(data);

        if (data is WebSocketChannelException) {
          _onWebSocketChannelException.add(data);
        } else if (data is String) {
          var jsons = json.decode(data);

          if (jsons is Map) {
            var event = WebSocketEvent.fromJson(jsons);

            if (event.eventName?.isNotEmpty == true) {
              _onAllEvents.add(event);
              on._getStream(event.eventName)!.add(event);
            }

            if (event.eventName == errorEvent) {
              var error =
                  AngelHttpException.fromMap((event.data ?? {}) as Map);
              _onError.add(error);
            } else if (event.eventName == authenticatedEvent) {
              var authResult = AngelAuthResult.fromMap(event.data as Map?);
              _onAuthenticated.add(authResult);
            } else if (event.eventName?.isNotEmpty == true) {
              var split = event.eventName!
                  .split('::')
                  .where((str) => str.isNotEmpty)
                  .toList();

              if (split.length >= 2) {
                var serviceName = split[0], eventName = split[1];
                _onServiceEvent
                    .add({serviceName: event..eventName = eventName});
              }
            }
          }
        }
      },
      cancelOnError: true,
      onDone: () {
        _socket = null;
        if (reconnectOnClose == true) {
          Timer.periodic(reconnectInterval!, (Timer timer) async {
            var result;

            try {
              result = await connect(timeout: reconnectInterval);
            } catch (e) {
              //
            }

            if (result != null) timer.cancel();
          });
        }
      });
}