connectionForBroadcast method

Future<RelayConnectivity?> connectionForBroadcast(
  1. String url,
  2. AuthPolicy? auth, {
  3. ConnectionSource connectionSource = ConnectionSource.broadcastSpecific,
  4. int connectTimeout = DEFAULT_WEB_SOCKET_CONNECT_TIMEOUT,
})

The connection a broadcast under auth must go out on towards url, opening it when it is not there yet. Null means nothing may be sent.

Mirrors connectionForRequest for the write path, and owns the connecting too: a broadcast that requires an identity must not even open the anonymous connection, because a socket we opened is one the relay saw, whether or not anything was sent on it.

Implementation

Future<RelayConnectivity?> connectionForBroadcast(
  String url,
  AuthPolicy? auth, {
  ConnectionSource connectionSource = ConnectionSource.broadcastSpecific,
  int connectTimeout = DEFAULT_WEB_SOCKET_CONNECT_TIMEOUT,
}) async {
  if (auth is AuthPolicyRequire) {
    if (!auth.account.signer.canSign()) {
      Logger.log.w(
        () => "Broadcast requires ${auth.account.pubkey}, which cannot sign",
      );
      return null;
    }
    return openConnectionAs(
      url,
      auth.account,
      connectionSource: connectionSource,
      connectTimeout: connectTimeout,
    );
  }

  if (!isRelayConnected(url)) {
    final connected = await connectRelay(
      dirtyUrl: url,
      connectionSource: connectionSource,
      connectTimeout: connectTimeout,
    );
    if (!connected.first) {
      Logger.log.w(() => "Could not connect to $url: ${connected.second}");
      return null;
    }
  }
  return getRelayConnectivity(url);
}