From b141602ddf97d884ae0fa88f6c9817a2a063a2ae Mon Sep 17 00:00:00 2001 From: rtm516 Date: Tue, 20 Aug 2024 21:53:44 +0100 Subject: [PATCH] Add RTC websocket heartbeat --- .../core/SessionManagerCore.java | 2 +- .../core/webrtc/RtcWebsocketClient.java | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java index 5f25538..ac938a7 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java @@ -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(); } diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java index 5243eff..4148b7a 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/RtcWebsocketClient.java @@ -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; @@ -55,6 +58,7 @@ public class RtcWebsocketClient extends WebSocketClient { } private final Logger logger; + private final ScheduledExecutorService scheduledExecutorService; private RTCConfiguration rtcConfig; public PeerConnectionFactory peerFactory; @@ -62,13 +66,15 @@ public class RtcWebsocketClient extends WebSocketClient { private Component component; private Map 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 @@ -76,6 +82,7 @@ public RtcWebsocketClient(String authenticationToken, ExpandedSessionInfo sessio addHeader("Request-Id", UUID.randomUUID().toString()); this.logger = logger; + this.scheduledExecutorService = scheduledExecutorService; this.peerFactory = new PeerConnectionFactory(); } @@ -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); } /** @@ -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 + ")"); }