open method

  1. @override
dynamic open(
  1. String url
)
override

Call this when you want to established a connection

Implementation

@override
open(String url) {

  var openStatus = WebOpenStatus.pending;

  close();
  _socket = html.WebSocket(url);
  _socket!.binaryType = "arraybuffer";

  _socket!.onOpen.first.then((value) {
    openStatus = WebOpenStatus.success;
    if (_onSuccess != null) {
      _onSuccess!();
    }
  });

  _socket!.onError.first.then((value) {
    openStatus = WebOpenStatus.failed;
    if (_onFailure != null) {
      _onFailure!();
    }
    _socket = null;
  });

  _socket!.onClose.first.then((value) {
    if (_onClose != null && openStatus != WebOpenStatus.failed) {
      _onClose!();
    }
    _socket = null;
  });

  _socket!.onMessage.listen((event) {
    if (_onMessage != null) {
      if (event.data is ByteBuffer) {
        var buf = event.data as ByteBuffer;
        _onMessage!(buf.asUint8List());
      } else {
        _onMessage!(event.data);
      }
    }
  });
}