From e7ea152bf9056a0cf718d6d6eb2c7dcb5e0dff6f Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 31 Jul 2024 22:49:49 +0100 Subject: [PATCH] maybe fix disconnects from websockets? --- .../controller/ScoresController.java | 1 - .../platform/impl/ScoreSaberPlatform.java | 6 --- .../cc/fascinated/websocket/Websocket.java | 50 +++++++++++++++++-- .../websocket/impl/ScoreSaberWebsocket.java | 7 ++- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/main/java/cc/fascinated/controller/ScoresController.java b/src/main/java/cc/fascinated/controller/ScoresController.java index c618c68..fd2ca64 100644 --- a/src/main/java/cc/fascinated/controller/ScoresController.java +++ b/src/main/java/cc/fascinated/controller/ScoresController.java @@ -1,7 +1,6 @@ package cc.fascinated.controller; import cc.fascinated.exception.impl.BadRequestException; -import cc.fascinated.model.score.TrackedScore; import cc.fascinated.platform.Platform; import cc.fascinated.services.TrackedScoreService; import lombok.NonNull; diff --git a/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java b/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java index 6583812..1ded54c 100644 --- a/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java +++ b/src/main/java/cc/fascinated/platform/impl/ScoreSaberPlatform.java @@ -21,7 +21,6 @@ import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; - /** * @author Fascinated (fascinated7) */ @@ -38,11 +37,6 @@ public class ScoreSaberPlatform extends Platform { */ private final double starMultiplier = 42.11; - /** - * The weight coefficient for the platform. - */ - private final double weightCoefficient = 0.965; - /** * The ScoreSaber service to use */ diff --git a/src/main/java/cc/fascinated/websocket/Websocket.java b/src/main/java/cc/fascinated/websocket/Websocket.java index 2a81515..1a221c6 100644 --- a/src/main/java/cc/fascinated/websocket/Websocket.java +++ b/src/main/java/cc/fascinated/websocket/Websocket.java @@ -5,16 +5,21 @@ import lombok.NonNull; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; import org.springframework.web.socket.CloseStatus; +import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.handler.TextWebSocketHandler; +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + /** * @author Fascinated (fascinated7) */ @Log4j2 @Getter -public class Websocket extends TextWebSocketHandler { +public abstract class Websocket extends TextWebSocketHandler { /** * The name of the WebSocket. */ @@ -25,19 +30,58 @@ public class Websocket extends TextWebSocketHandler { */ private final String url; + /** + * The WebSocket session. + */ + private WebSocketSession webSocketSession; + + /** + * The last message received + */ + private Date lastMessage; + public Websocket(@NonNull String name, @NonNull String url) { this.name = name; this.url = url; connectWebSocket(); // Connect to the WebSocket. + + new Timer().scheduleAtFixedRate(new TimerTask() { + @Override @SneakyThrows + public void run() { + if (lastMessage == null) { + return; + } + + long difference = new Date().getTime() - lastMessage.getTime(); + if (difference > 10000) { + log.error("The {} WebSocket has not received a message in over 10 seconds, reconnecting...", getName()); + webSocketSession.close(); // Close the WebSocket session. + connectWebSocket(); // Reconnect to the WebSocket. + } + } + }, 0, 5000); + } + + /** + * Handles a message received from the WebSocket. + * + * @param message the message received + */ + public abstract void handleMessage(@NonNull TextMessage message); + + @Override + protected final void handleTextMessage(@NonNull WebSocketSession session, @NonNull TextMessage message) { + this.lastMessage = new Date(); + this.handleMessage(message); } /** * Connects to the ScoreSaber WebSocket. */ @SneakyThrows - private void connectWebSocket() { + private final void connectWebSocket() { log.info("Connecting to the {}", this.getName()); - new StandardWebSocketClient().execute(this, this.getUrl()).get(); + this.webSocketSession = new StandardWebSocketClient().execute(this, this.getUrl()).get(); } @Override diff --git a/src/main/java/cc/fascinated/websocket/impl/ScoreSaberWebsocket.java b/src/main/java/cc/fascinated/websocket/impl/ScoreSaberWebsocket.java index f1ddb1e..ebeaac8 100644 --- a/src/main/java/cc/fascinated/websocket/impl/ScoreSaberWebsocket.java +++ b/src/main/java/cc/fascinated/websocket/impl/ScoreSaberWebsocket.java @@ -14,11 +14,9 @@ import io.questdb.client.Sender; import lombok.NonNull; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; -import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.socket.TextMessage; -import org.springframework.web.socket.WebSocketSession; /** * @author Fascinated (fascinated7) @@ -50,7 +48,7 @@ public class ScoreSaberWebsocket extends Websocket { } @Override @SneakyThrows - protected void handleTextMessage(@NotNull WebSocketSession session, @NotNull TextMessage message) { + public void handleMessage(@NonNull TextMessage message) { String payload = message.getPayload(); if (payload.equals("Connected to the ScoreSaber WSS")) { // Ignore the connection message return; @@ -96,6 +94,7 @@ public class ScoreSaberWebsocket extends Websocket { .stringColumn("difficulty", difficulty) .atNow(); } - log.info("Tracked score for '%s' on '%s'".formatted(player.getName(), leaderboard.getSongName())); + log.info("Tracked score for {} with a score of {} and {}pp on {} with a rank of {}", + player.getId(), score.getBaseScore(), score.getPp(), leaderboard.getId(), score.getRank()); } }