Skip to content

Commit

Permalink
Add RTC websocket heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Aug 20, 2024
1 parent 4e0f7f2 commit b141602
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ protected void setupRtaWebsocket(String token) {
}

protected void setupRtcWebsocket(String token) {
rtcWebsocket = new RtcWebsocketClient(token, sessionInfo, logger);
rtcWebsocket = new RtcWebsocketClient(token, sessionInfo, logger, scheduledThread());
rtcWebsocket.connect();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.sdp.Attribute;
import javax.sdp.MediaDescription;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
Expand Down Expand Up @@ -55,27 +58,31 @@ public class RtcWebsocketClient extends WebSocketClient {
}

private final Logger logger;
private final ScheduledExecutorService scheduledExecutorService;

private RTCConfiguration rtcConfig;
public PeerConnectionFactory peerFactory;
private Agent agent;
private Component component;
private Map<String, PeerSession> activeSessions = new HashMap<>();
private PeerSession pendingSession;
private ScheduledFuture<?> heartbeatFuture;

/**
* Create a new websocket and add the Authorization header
*
* @param authenticationToken The token to use for authentication
* @param authenticationToken The token to use for authentication
* @param scheduledExecutorService
*/
public RtcWebsocketClient(String authenticationToken, ExpandedSessionInfo sessionInfo, Logger logger) {
public RtcWebsocketClient(String authenticationToken, ExpandedSessionInfo sessionInfo, Logger logger, ScheduledExecutorService scheduledExecutorService) {
super(URI.create(Constants.RTC_WEBSOCKET_FORMAT.formatted(sessionInfo.getWebrtcNetworkId())));
addHeader("Authorization", authenticationToken);
// both seem random
addHeader("Session-Id", UUID.randomUUID().toString());
addHeader("Request-Id", UUID.randomUUID().toString());

this.logger = logger;
this.scheduledExecutorService = scheduledExecutorService;

this.peerFactory = new PeerConnectionFactory();
}
Expand All @@ -89,6 +96,10 @@ public RtcWebsocketClient(String authenticationToken, ExpandedSessionInfo sessio
*/
@Override
public void onOpen(ServerHandshake serverHandshake) {
// Set up the heartbeat
heartbeatFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
send(Constants.GSON.toJson(new WsToMessage(0, null, null)));
}, 60, 60, TimeUnit.SECONDS);
}

/**
Expand Down Expand Up @@ -322,6 +333,8 @@ private void initialize(JsonObject message) {

@Override
public void onClose(int code, String reason, boolean remote) {
heartbeatFuture.cancel(true);

logger.info("RTCWebsocket disconnected: " + reason + " (" + code + ")");
}

Expand Down

0 comments on commit b141602

Please sign in to comment.