init method

void init(
  1. WebSocketChannel webSocketChannel
)

Initializer method called by webSocketHandler. You don't need to call this method manually.

Implementation

void init(WebSocketChannel webSocketChannel) {
  channel = webSocketChannel;

  /// Lifecycle of a websocket
  try {
    /// Open
    onOpen?.call(this);

    /// While connected
    channel.stream.listen((dynamic data) {
      onMessage?.call(this, data);
    }, onDone: () {
      /// On close
      onClose?.call(this);
    }, onError: (dynamic error) {
      /// On error
      onError?.call(this, error);
    });
  } catch (e) {
    /// If something goes wrong
    onError?.call(this, e);
    try {
      channel.sink.close();
      // ignore: empty_catches
    } catch (e) {}
  }
}