open method

dynamic open(
  1. String url
)
override

建立 WebSocket 连接.

Implementation

open(String url) {
  this.close();

  this._openStatus = _OpenStatus.Pending;
  this._socket = html.WebSocket(url);
  this._socket!.binaryType = "arraybuffer";

  this._socket!.onOpen.first.then((value) {
    this._openStatus = _OpenStatus.Success;
    if (_onSuccess != null) {
      _onSuccess!();
    }
  });

  this._socket!.onError.first.then((value) {
    this._openStatus = _OpenStatus.Failed;
    if (_onFailed != null) {
      _onFailed!();
    }
    this._socket = null;
  });

  this._socket!.onClose.first.then((value) {
    if (_onClose != null && this._openStatus != _OpenStatus.Failed) {
      _onClose!();
    }
    this._socket = null;
  });

  this._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);
      }
    }
  });
}