Package org.ngengine.nostr4j.rtc
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 Summary
ConstructorsConstructorDescriptionNostrRTCSocket
(org.ngengine.platform.AsyncExecutor executor, NostrRTCLocalPeer localPeer, String connectionId, org.ngengine.platform.RTCSettings settings, NostrTURNSettings turnSettings) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(NostrRTCSocketListener listener) void
close()
Close the socket.Get the local peer.Get the remote peer if connected, otherwise null.boolean
Return true if the connection is established.boolean
void
mergeRemoteRTCIceCandidate
(NostrRTCIceCandidate candidate) Merge remote ICE candidates with the already tracked candidates.void
onLocalRTCIceCandidate
(String candidateString) void
void
void
void
onRTCDisconnected
(String reason) void
onTurnPacket
(NostrRTCPeer peer, ByteBuffer data) void
removeListener
(NostrRTCSocketListener listener) void
setForceTURN
(boolean forceTURN) Force the usage of TURN server.void
useTURN
(boolean use) Set the socket to use the turn server.org.ngengine.platform.AsyncTask
<Void> write
(ByteBuffer bbf) Send some data to the remote peer.
-
Constructor Details
-
NostrRTCSocket
public NostrRTCSocket(org.ngengine.platform.AsyncExecutor executor, NostrRTCLocalPeer localPeer, String connectionId, org.ngengine.platform.RTCSettings settings, NostrTURNSettings turnSettings)
-
-
Method Details
-
getLocalPeer
Get the local peer.- Returns:
- The local peer.
-
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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
addListener
-
removeListener
-
mergeRemoteRTCIceCandidate
Merge remote ICE candidates with the already tracked candidates.- Parameters:
candidate
- The remote ICE candidates.
-
onLocalRTCIceCandidate
- Specified by:
onLocalRTCIceCandidate
in interfaceorg.ngengine.platform.transport.RTCTransportListener
-
onRTCConnected
public void onRTCConnected()- Specified by:
onRTCConnected
in interfaceorg.ngengine.platform.transport.RTCTransportListener
-
onRTCDisconnected
- Specified by:
onRTCDisconnected
in interfaceorg.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
- Specified by:
onRTCBinaryMessage
in interfaceorg.ngengine.platform.transport.RTCTransportListener
-
onTurnPacket
- Specified by:
onTurnPacket
in interfaceNostrTURN.Listener
-
write
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
- Specified by:
onRTCChannelError
in interfaceorg.ngengine.platform.transport.RTCTransportListener
-
setForceTURN
public void setForceTURN(boolean forceTURN) Force the usage of TURN server.- Parameters:
forceTURN
-
-