openConnectionAs method

Future<RelayConnectivity?> openConnectionAs(
  1. String url,
  2. Account account, {
  3. ConnectionSource connectionSource = ConnectionSource.explicit,
  4. int connectTimeout = DEFAULT_WEB_SOCKET_CONNECT_TIMEOUT,
  5. TimeoutPausable? pausing,
})

Opens the connection towards url bound to account. The binding says which identity this socket may ever assume, not that it is already authenticated: relays are free to send their challenge whenever they want, and some only send it once a request needs it.

A new binding is first put to authHandler, with pausing paused while it decides. A connection that already exists was agreed to when it was first opened, so reconnecting it does not ask again.

Implementation

Future<RelayConnectivity?> openConnectionAs(
  String url,
  Account account, {
  ConnectionSource connectionSource = ConnectionSource.explicit,
  int connectTimeout = DEFAULT_WEB_SOCKET_CONNECT_TIMEOUT,
  TimeoutPausable? pausing,
}) async {
  if (!account.signer.canSign()) {
    Logger.log.w(() => "Cannot bind a connection to ${account.pubkey}");
    return null;
  }
  final key = RelayConnectionKey.authenticated(url, account.pubkey);
  if (globalState.relays[key] == null &&
      !await _consent(key, pausing: pausing)) {
    Logger.log.d(() => "Not allowed to open $key");
    return null;
  }
  _boundAccounts[key] = account;

  if (isConnectionOpen(key)) {
    return globalState.relays[key];
  }

  final connected = await connectRelay(
    dirtyUrl: url,
    connectionSource: connectionSource,
    authPubkey: account.pubkey,
    connectTimeout: connectTimeout,
  );
  final connectivity = globalState.relays[key];
  if (!connected.first || connectivity == null) {
    Logger.log.w(() => "Could not open $key: ${connected.second}");
    return null;
  }
  return connectivity;
}