pingPeer method

Future<bool> pingPeer(
  1. PeerId peerId
)
inherited

Pings a peer to check if it is online.

This method sends a ping message to the specified peer and waits for a response. If a response is received within the timeout period, the peer is considered online. Otherwise, the peer is considered offline.

The peerId parameter specifies the ID of the peer to ping.

Returns true if the peer is online, false otherwise.

Implementation

Future<bool> pingPeer(PeerId peerId) async {
  // If the peer is the same as the self ID, return true.
  if (peerId == selfId) {
    return true;
  }

  // Try to send a message to the peer.
  try {
    // Send a confirmable message to the peer and wait for an
    // acknowledgement.
    await sendMessage(isConfirmable: true, dstPeerId: peerId);

    // If the message was sent successfully, update the last seen
    // controller to indicate that the peer is online.
    _lastSeenController.add((peerId: peerId, isOnline: true));

    // Return true to indicate that the peer is online.
    return true;
  } on Object catch (_) {
    // Ignore any errors that occur during message sending.
  }

  // If the message was not sent successfully, update the last seen
  // controller to indicate that the peer is offline.
  _lastSeenController.add((
    peerId: peerId,
    isOnline: getPeerStatus(peerId),
  ));

  // Return false to indicate that the peer is offline.
  return false;
}