disconnectFromRelays method

Future<bool> disconnectFromRelays({
  1. int closeCode(
    1. String relayUrl
    )?,
  2. String closeReason(
    1. String relayUrl
    )?,
  3. void onRelayDisconnect(
    1. String relayUrl,
    2. WebSocketChannel relayWebSOcket,
    3. dynamic webSocketDisconnectionMessage
    )?,
})

Implementation

Future<bool> disconnectFromRelays({
  int Function(String relayUrl)? closeCode,
  String Function(String relayUrl)? closeReason,
  void Function(
    String relayUrl,
    WebSocketChannel relayWebSOcket,
    dynamic webSocketDisconnectionMessage,
  )? onRelayDisconnect,
}) async {
  final webSockets = nostrRegistry.relaysWebSocketsRegistry;
  for (var index = 0; index < webSockets.length; index++) {
    final current = webSockets.entries.elementAt(index);
    final relayUrl = current.key;
    final relayWebSocket = current.value;

    final returnedMessage = await relayWebSocket.sink.close(
      closeCode?.call(relayUrl),
      closeReason?.call(relayUrl),
    );

    onRelayDisconnect?.call(relayUrl, relayWebSocket, returnedMessage);
  }

  nostrRegistry.clearWebSocketsRegistry();
  relaysList = [];

  return true;
}