Changes to websocket

This commit is contained in:
Jonttuuu 2023-07-30 15:34:07 +03:00
parent 76aad82883
commit 71b03cb87d

View File

@ -1,6 +1,7 @@
package fi.flexplex.lib; package fi.flexplex.lib;
import java.io.IOException; import java.io.IOException;
import java.net.SocketException;
import com.neovisionaries.ws.client.WebSocket; import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter; import com.neovisionaries.ws.client.WebSocketAdapter;
@ -19,11 +20,8 @@ public final class WebSocketClient {
protected void connect() { protected void connect() {
if (!webSocket.isOpen()) { if (!webSocket.isOpen()) {
try { this.webSocket.connectAsynchronously();
this.webSocket.connect(); this.setWebSocketSettings();
} catch (final WebSocketException e) {
flexLib.getLogger().warning("WebSocket connection to FlexPlex server failed.");
}
} }
} }
@ -31,6 +29,15 @@ public final class WebSocketClient {
this.webSocket.disconnect(); this.webSocket.disconnect();
} }
private void setWebSocketSettings() {
try {
this.webSocket.getConnectedSocket().setSoTimeout(0);
this.webSocket.getConnectedSocket().setKeepAlive(true);
} catch (final SocketException | WebSocketException e) {
this.flexLib.getLogger().warning("Failed to set WebSocket settings");
}
}
private WebSocket createWebSocket(final String url, final String token) throws IOException { private WebSocket createWebSocket(final String url, final String token) throws IOException {
return new WebSocketFactory() return new WebSocketFactory()
.createSocket(url) .createSocket(url)