Skip to content

Commit 9b65a39

Browse files
committed
Connect to alternative WS URL if main WS URL fails
1 parent fac30e9 commit 9b65a39

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

core/src/main/java/org/geysermc/geyser/Constants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
public final class Constants {
3232
public static final URI GLOBAL_API_WS_URI;
33+
public static final URI GLOBAL_API_WS_URI_ALT;
3334

3435
public static final String NEWS_OVERVIEW_URL = "https://api.geysermc.org/v2/news/";
3536
public static final String NEWS_PROJECT_NAME = "geyser";
@@ -54,5 +55,12 @@ public final class Constants {
5455
e.printStackTrace();
5556
}
5657
GLOBAL_API_WS_URI = wsUri;
58+
59+
URI wsUriAlt = null;
60+
try {
61+
wsUriAlt = new URI("wss://ws.geysermc.org/ws");
62+
} catch (URISyntaxException ignored) {
63+
}
64+
GLOBAL_API_WS_URI_ALT = wsUriAlt;
5765
}
5866
}

core/src/main/java/org/geysermc/geyser/skin/FloodgateSkinUploader.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.geysermc.geyser.util.JsonUtils;
5050
import org.geysermc.geyser.util.PluginMessageUtils;
5151
import org.java_websocket.client.WebSocketClient;
52+
import org.java_websocket.framing.CloseFrame;
5253
import org.java_websocket.handshake.ServerHandshake;
5354

5455
public final class FloodgateSkinUploader {
@@ -79,6 +80,8 @@ public final class FloodgateSkinUploader {
7980
@Getter private boolean allowSubscribers = false;
8081
private int subscribersCount;
8182

83+
private int protocolErrorCount = 0;
84+
8285
public FloodgateSkinUploader(GeyserImpl geyser) {
8386
this.logger = geyser.getLogger();
8487
this.client = new WebSocketClient(Constants.GLOBAL_API_WS_URI) {
@@ -180,6 +183,22 @@ public void onMessage(String message) {
180183
public void onClose(int code, String reason, boolean remote) {
181184
allowSubscribers = false;
182185

186+
// If we seem to get protocol errors quite a bit, try our alternative url.
187+
// But if that one fails too for a few times, just revert back to our main url.
188+
if (code == CloseFrame.PROTOCOL_ERROR) {
189+
boolean shouldSwitch = false;
190+
if (protocolErrorCount < 5) {
191+
shouldSwitch = ++protocolErrorCount == 5;
192+
}
193+
if (protocolErrorCount >= 5 && Constants.GLOBAL_API_WS_URI_ALT != null) {
194+
if (shouldSwitch) {
195+
uri = Constants.GLOBAL_API_WS_URI_ALT;
196+
} else if (++protocolErrorCount == 10) {
197+
uri = Constants.GLOBAL_API_WS_URI;
198+
}
199+
}
200+
}
201+
183202
if (reason != null && !reason.isEmpty()) {
184203
try {
185204
JsonObject node = JsonUtils.parseJson(reason);

0 commit comments

Comments
 (0)