diff --git a/src/main/java/fi/flexplex/lib/FlexLib.java b/src/main/java/fi/flexplex/lib/FlexLib.java index 2262d00..3e0f301 100644 --- a/src/main/java/fi/flexplex/lib/FlexLib.java +++ b/src/main/java/fi/flexplex/lib/FlexLib.java @@ -9,8 +9,11 @@ import java.util.Timer; import java.util.TimerTask; import java.util.logging.Logger; +import com.neovisionaries.ws.client.WebSocketException; + public final class FlexLib { + private final FlexLib instance; private final String token; private final Logger logger; private final Timer timer; @@ -37,25 +40,33 @@ public final class FlexLib { */ public FlexLib(final String token, final Logger logger) { logger.info("Initializing FlexLib"); + this.instance = this; this.token = token; this.logger = logger; this.graphQLApi = new GraphQLApi(this, "https://api.flexplex.fi/graphql"); new FlexLibAdapterImpl(this); - try { - this.webSocketClient = new WebSocketClient(this, "wss://api.flexplex.fi/flexlib", this.token); - } catch (final IOException e) { - this.logger.warning("Failed to initialize FlexLib WebSocket client"); - } - this.timer = new Timer(); this.timerTask = new TimerTask() { @Override public void run() { final long removeTime = System.currentTimeMillis() - 10_800_000; playerFriends.entrySet().removeIf(entry -> entry.getValue().getLastUsed() < removeTime); - webSocketClient.connect(); + + // Connect websocket if closed + if (webSocketClient != null && !webSocketClient.isOpen()) { + webSocketClient.disable(); + webSocketClient = null; + } + if (webSocketClient == null) { + try { + webSocketClient = new WebSocketClient(instance, "wss://api.flexplex.fi/flexlib", token); + webSocketClient.connect(); + } catch (final IOException | WebSocketException e) { + logger.warning("Failed to initialize FlexLib WebSocket client"); + } + } } }; this.timer.schedule(this.timerTask, 0, 60_000); diff --git a/src/main/java/fi/flexplex/lib/WebSocketClient.java b/src/main/java/fi/flexplex/lib/WebSocketClient.java index 5eb7b46..53b544f 100644 --- a/src/main/java/fi/flexplex/lib/WebSocketClient.java +++ b/src/main/java/fi/flexplex/lib/WebSocketClient.java @@ -16,20 +16,6 @@ public final class WebSocketClient { protected WebSocketClient(final FlexLib flexLib, final String url, final String token) throws IOException { this.flexLib = flexLib; this.webSocket = this.createWebSocket(url, token); - } - - protected void connect() { - if (!webSocket.isOpen()) { - this.webSocket.connectAsynchronously(); - this.setWebSocketSettings(); - } - } - - protected void disable() { - this.webSocket.disconnect(); - } - - private void setWebSocketSettings() { try { this.webSocket.getConnectedSocket().setSoTimeout(0); this.webSocket.getConnectedSocket().setKeepAlive(true); @@ -38,6 +24,18 @@ public final class WebSocketClient { } } + protected void connect() throws WebSocketException { + this.webSocket.connect(); + } + + protected boolean isOpen() { + return this.webSocket.isOpen(); + } + + protected void disable() { + this.webSocket.disconnect(); + } + private WebSocket createWebSocket(final String url, final String token) throws IOException { return new WebSocketFactory() .createSocket(url) @@ -87,6 +85,7 @@ public final class WebSocketClient { for (final FlexLibAdapter listener : flexLib.getEventListeners()) { listener.onWebSocketConnect(); } + break; default: flexLib.getLogger().warning("Received invalid WebSocket message from FlexPlex. Message: " + args[0]); break;