dynamic initWebsocket([bool reconnect = true ])

Source

initWebsocket([bool reconnect = true]) {
  if (_closed) return;
  String wsUrl = '$_wsUpdateUri&auth=${_nonce.hashSalt(
      salts[0])}&format=$format';
  var socket = new WebSocket(wsUrl);
  _wsConnection =
  new WebSocketConnection(socket, this, enableAck: enableAck, onConnect: () {
    if (!_onConnectedCompleter.isCompleted) {
      _onConnectedCompleter.complete();
    }
  }, useCodec: DsCodec.getCodec(format));

  if (responder != null) {
    responder.connection = _wsConnection.responderChannel;
  }

  if (requester != null) {
    _wsConnection.onRequesterReady.then((channel) {
      if (_closed) return;
      requester.connection = channel;
      if (!_onRequesterReadyCompleter.isCompleted) {
        _onRequesterReadyCompleter.complete(requester);
      }
    });
  }
  _wsConnection.onDisconnected.then((authError) {
    logger.info('Disconnected');
    if (_closed) return;

    if (_wsConnection._opened) {
      _wsDelay = 1;
      if (authError) {
        connect();
      } else {
        initWebsocket(false);
      }
    } else if (reconnect) {
      if (authError) {
        connect();
      } else {
        DsTimer.timerOnceAfter(initWebsocket, _wsDelay * 1000);
        if (_wsDelay < 60) _wsDelay++;
      }
    } else {
      _wsDelay = 5;
      DsTimer.timerOnceAfter(initWebsocket, 5000);
    }
  });
}