Class NostrRTCSocket

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, NostrTURN.Listener, org.ngengine.platform.transport.RTCTransportListener

    public class NostrRTCSocket
    extends java.lang.Object
    implements org.ngengine.platform.transport.RTCTransportListener, NostrTURN.Listener, java.io.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 Detail

      • NostrRTCSocket

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

      • 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 java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • 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​(org.ngengine.platform.transport.RTCTransportIceCandidate 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​(java.lang.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​(java.nio.ByteBuffer bbf)
        Specified by:
        onRTCBinaryMessage in interface org.ngengine.platform.transport.RTCTransportListener
      • write

        public org.ngengine.platform.AsyncTask<java.lang.Void> write​(java.nio.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:
        java.lang.IllegalStateException - If the socket is not connected.
        java.lang.IllegalArgumentException - If the data is null.
      • onRTCChannelError

        public void onRTCChannelError​(java.lang.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 -