Future connect()

Source

connect() async {
  if (_closed) return;
  lockCryptoProvider();
  String connUrl = '$_conn?dsId=$dsId';
  if (tokenHash != null) {
    connUrl = '$connUrl$tokenHash';
  }
  Uri connUri = Uri.parse(connUrl);
  logger.info('Connecting: $connUri');
  try {
    Map requestJson = {
      'publicKey': privateKey.publicKey.qBase64,
      'isRequester': requester != null,
      'isResponder': responder != null,
      'formats': formats,
      'version': DSA_VERSION,
      'enableWebSocketCompression': true
    };
    HttpRequest request = await HttpRequest.request(connUrl,
        method: 'POST',
        withCredentials: false,
        mimeType: 'application/json',
        sendData: DsJson.encode(requestJson));
    Map serverConfig = DsJson.decode(request.responseText);
    saltNameMap.forEach((name, idx) {
      //read salts
      salts[idx] = serverConfig[name];
    });
    String tempKey = serverConfig['tempKey'];
    _nonce = await privateKey.getSecret(tempKey);

    if (serverConfig['wsUri'] is String) {
      _wsUpdateUri = '${connUri.resolve(serverConfig['wsUri'])}?dsId=$dsId'
          .replaceFirst('http', 'ws');
      if (tokenHash != null) {
        _wsUpdateUri = '$_wsUpdateUri$tokenHash';
      }
    }

    // server start to support version since 1.0.4
    // and this is the version ack is added
    enableAck = serverConfig.containsKey('version');
    if (serverConfig['format'] is String) {
      format = serverConfig['format'];
    }
    initWebsocket(false);
    _connDelay = 1;
    _wsDelay = 1;
  } catch (err) {
    DsTimer.timerOnceAfter(connect, _connDelay * 1000);
    if (_connDelay < 60) _connDelay++;
  }
}