Class NostrRTCSocket

java.lang.Object
org.ngengine.nostr4j.rtc.NostrRTCSocket
All Implemented Interfaces:
Closeable, AutoCloseable, NostrTURN.Listener, org.ngengine.platform.transport.RTCTransportListener

public class NostrRTCSocket extends Object implements org.ngengine.platform.transport.RTCTransportListener, NostrTURN.Listener, Closeable
An RTC socket between two peers. This class will try to establish a direct connection between the two peers, when not possible it will fallback to a TURN server. Note: isConnected() will return true as long as any kind of connection is possible, this includes turn if the turn servers are provided. This also means that due to the nature of the TURN connection, the socket will always result connected as long as the TURN relay is reachable, even if the peer is long gone. This is because, to avoid inefficiencies, the keep-alive mechanism is implemented only in the signaling protocol: when the signaling announce is stale, the socket should be closed using close(). So keep in mind that you need to handle keep-alive youself, if you want to use this class by itself (without the signaling protocol).
  • Constructor Details

    • NostrRTCSocket

      public NostrRTCSocket(org.ngengine.platform.AsyncExecutor executor, NostrRTCLocalPeer localPeer, String connectionId, org.ngengine.platform.RTCSettings settings, NostrTURNSettings turnSettings)
  • Method Details

    • getLocalPeer

      public NostrRTCLocalPeer getLocalPeer()
      Get the local peer.
      Returns:
      The local peer.
    • getRemotePeer

      public NostrRTCPeer getRemotePeer()
      Get the remote peer if connected, otherwise null.
      Returns:
      The remote peer or null.
    • isConnected

      public boolean isConnected()
      Return true if the connection is established.
      Returns:
      True if the connection is established.
    • close

      public void close()
      Close the socket.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • addListener

      public void addListener(NostrRTCSocketListener listener)
    • removeListener

      public void removeListener(NostrRTCSocketListener listener)
    • mergeRemoteRTCIceCandidate

      public void mergeRemoteRTCIceCandidate(NostrRTCIceCandidate candidate)
      Merge remote ICE candidates with the already tracked candidates.
      Parameters:
      candidate - The remote ICE candidates.
    • onLocalRTCIceCandidate

      public void onLocalRTCIceCandidate(String candidateString)
      Specified by:
      onLocalRTCIceCandidate in interface org.ngengine.platform.transport.RTCTransportListener
    • onRTCConnected

      public void onRTCConnected()
      Specified by:
      onRTCConnected in interface org.ngengine.platform.transport.RTCTransportListener
    • onRTCDisconnected

      public void onRTCDisconnected(String reason)
      Specified by:
      onRTCDisconnected in interface org.ngengine.platform.transport.RTCTransportListener
    • useTURN

      public void useTURN(boolean use)
      Set the socket to use the turn server.
      Parameters:
      use -
    • isUsingTURN

      public boolean isUsingTURN()
    • onRTCBinaryMessage

      public void onRTCBinaryMessage(ByteBuffer bbf)
      Specified by:
      onRTCBinaryMessage in interface org.ngengine.platform.transport.RTCTransportListener
    • onTurnPacket

      public void onTurnPacket(NostrRTCPeer peer, ByteBuffer data)
      Specified by:
      onTurnPacket in interface NostrTURN.Listener
    • write

      public org.ngengine.platform.AsyncTask<Void> write(ByteBuffer bbf)
      Send some data to the remote peer.
      Parameters:
      bbf - The data to send (use Direct Buffers for performance).
      Returns:
      An async task that resolves when the data is sent.
      Throws:
      IllegalStateException - If the socket is not connected.
      IllegalArgumentException - If the data is null.
    • onRTCChannelError

      public void onRTCChannelError(Throwable e)
      Specified by:
      onRTCChannelError in interface org.ngengine.platform.transport.RTCTransportListener
    • setForceTURN

      public void setForceTURN(boolean forceTURN)
      Force the usage of TURN server.
      Parameters:
      forceTURN -