ObsWebSocket constructor Null safety

ObsWebSocket(
  1. {required WebSocketChannel channel,
  2. Function? fallbackEvent}
)

When the object is created we open the websocket connection and create a broadcast stream so that we can have multiple listeners providing responses to commands. channel is an existing WebSocketChannel.

Implementation

ObsWebSocket({required this.channel, Function? fallbackEvent}) {
  broadcast = channel.stream.asBroadcastStream();

  // if (fallbackEvent != null) {
  //   addFallbackListener(fallbackEvent);
  // }

  broadcast.listen((jsonEvent) {
    final Map<String, dynamic> rawEvent = jsonDecode(jsonEvent);

    if (!rawEvent.containsKey('message-id')) {
      _handleEvent(BaseEvent.fromJson(rawEvent));
    }
  }, cancelOnError: true);
}