Hotline constructor Null safety

Hotline(
  1. String url,
  2. {required Function onConnect,
  3. required Function onDisconnect,
  4. Map<String, String>? headers,
  5. Iterable<String>? protocols,
  6. Duration? pingInterval,
  7. Function? onConnectionRefused}
)

Implementation

Hotline(this.url, {required this.onConnect, required this.onDisconnect, this.headers, this.protocols, this.pingInterval, this.onConnectionRefused}) {
  socketChannel   = IOWebSocketChannel.connect(url);
  connectionState = HotlineSocketConnectionState(onConnect: _onConnect, onDisconnect: _onDisconnect);
  connectionState.stateType = HotlineSocketConnectionType.connecting;

  stream = socketChannel.stream.listen((data) {
    final payload = jsonDecode(data);

    if(payload['type'] != null) {
      // if the payload has a type attribute we can assume it's a protocol message
      _dispatchProtocolMessage(payload);
    }else {
      // handle the broadcast....
      _dispatchDataMessage(payload);
    }
  }, onError: (_)  {
    _onDisconnect();
  });

  // initialise the subscription manager for this connection
  subscriptions = HotlineSubscriptionManager(this);
}