Skip to content

Commit 36b2c34

Browse files
committed
Replace java-websocket with native java 11 implementation
1 parent 6e18f80 commit 36b2c34

File tree

9 files changed

+247
-244
lines changed

9 files changed

+247
-244
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ JSON body you can use `ApiRequest#jsonBodyPublisher(Object)`. This only affects
3636
`ExarotonClient#request(ApiRequest, HttpResponse.BodyHandler)` use the respective body handlers to get an input stream,
3737
string or object.
3838

39+
### WebSockets
40+
The `java-websocket` library has been replaced by the built-in Java 11 websocket implementation. Unless you were using
41+
the `WebSocketClient` or `WebSocketManager` classes directly this shouldn't require any changes to your code.
3942

4043
### Other
4144
- `Server#subscribe` and `Server#unsubscribe` now accept the `StreamName` enum instead of any string
@@ -49,6 +52,7 @@ string or object.
4952
- Added `ApiStatus` annotations to many classes and methods
5053
- Removed debug/error handler from `WebsocketManager` and `WebsocketClient`. Errors and debug messages are now
5154
exclusively logged using SLF4J
55+
- Removed `ExarotonClient#getApiToken()`
5256

5357
## Improvements
5458
- Update dependencies

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dependencies {
1919
compileOnly(libs.jetbrains.annotations)
2020

2121
implementation(libs.gson)
22-
implementation(libs.java.websocket)
2322
implementation(libs.slf4j.api)
2423

2524

gradle/libs.versions.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
[versions]
22
gson = "2.12.1"
3-
java-websocket = "1.6.0"
43
jetbrains-annotations = "26.0.2"
54
slf4j = "2.0.17"
65
# Test dependencies
76
junit = "5.12.0"
87

98
[libraries]
109
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
11-
java-websocket = { module = "org.java-websocket:Java-WebSocket", version.ref = "java-websocket" }
1210
jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "jetbrains-annotations" }
1311
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
1412
# Test dependencies

src/main/java/com/exaroton/api/ExarotonClient.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import com.exaroton.api.request.server.GetServersRequest;
88
import com.exaroton.api.server.Server;
99
import com.exaroton.api.server.config.ConfigOptionTypeAdapterFactory;
10+
import com.exaroton.api.ws.WebSocketConnection;
1011
import com.google.gson.Gson;
1112
import com.google.gson.GsonBuilder;
13+
import org.jetbrains.annotations.ApiStatus;
1214
import org.jetbrains.annotations.NotNull;
1315

1416
import java.io.IOException;
@@ -20,7 +22,6 @@
2022
import java.util.Locale;
2123
import java.util.Objects;
2224
import java.util.concurrent.CompletableFuture;
23-
import java.util.concurrent.CompletionException;
2425

2526
public class ExarotonClient {
2627
/**
@@ -120,13 +121,6 @@ public String getBasePath() {
120121
return basePath;
121122
}
122123

123-
/**
124-
* @return API token
125-
*/
126-
public String getApiToken() {
127-
return apiToken;
128-
}
129-
130124
/**
131125
* Change the protocol used for API requests
132126
* Supported values: http, https
@@ -268,4 +262,21 @@ public Server getCurrentServer() {
268262
if (id == null) return null;
269263
return this.getServer(id);
270264
}
265+
266+
/**
267+
* Create a new websocket connection
268+
* @param server server to connect to
269+
* @param path websocket path
270+
* @return websocket manager
271+
*/
272+
@ApiStatus.Internal
273+
public WebSocketConnection connectToWebSocket(Server server, String path) {
274+
String protocol = this.getProtocol().equals("http") ? "ws" : "wss";
275+
try {
276+
URL url = new URL(protocol, getHost(), getBasePath());
277+
return new WebSocketConnection(httpClient, gson, url.toURI().resolve(path), apiToken, server);
278+
} catch (URISyntaxException | MalformedURLException e) {
279+
throw new RuntimeException(e);
280+
}
281+
}
271282
}

src/main/java/com/exaroton/api/server/Server.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.exaroton.api.server;
22

3-
import com.exaroton.api.APIException;
43
import com.exaroton.api.ExarotonClient;
54
import com.exaroton.api.Initializable;
65
import com.exaroton.api.request.server.*;
7-
import com.exaroton.api.ws.WebSocketManager;
6+
import com.exaroton.api.ws.WebSocketConnection;
87
import com.exaroton.api.ws.stream.StreamName;
98
import com.exaroton.api.ws.subscriber.*;
109
import com.google.gson.Gson;
@@ -22,7 +21,7 @@ public final class Server implements Initializable {
2221
/**
2322
* has this server been fetched from the API yet
2423
*/
25-
private boolean fetched;
24+
private boolean fetched = false;
2625

2726
/**
2827
* Unique server ID
@@ -85,7 +84,7 @@ public final class Server implements Initializable {
8584
/**
8685
* web socket client
8786
*/
88-
private transient WebSocketManager webSocket;
87+
private transient WebSocketConnection webSocket;
8988

9089
/**
9190
* gson instance
@@ -101,7 +100,6 @@ public final class Server implements Initializable {
101100
*/
102101
@ApiStatus.Internal
103102
public Server(@NotNull ExarotonClient client, @NotNull Gson gson, @NotNull String id) {
104-
this.fetched = false;
105103
this.client = Objects.requireNonNull(client);
106104
this.gson = Objects.requireNonNull(gson);
107105
this.id = Objects.requireNonNull(id);
@@ -277,10 +275,7 @@ public ExarotonClient getClient() {
277275
*/
278276
public CompletableFuture<Server> get() throws IOException {
279277
return client.request(new GetServerRequest(this.client, this.gson, this.id))
280-
.thenApply(data -> {
281-
this.fetched = true;
282-
return this.setFromObject(data);
283-
});
278+
.thenApply(this::setFromObject);
284279
}
285280

286281
/**
@@ -422,6 +417,7 @@ public PlayerList getPlayerList(String name) {
422417
*/
423418
@ApiStatus.Internal
424419
public Server setFromObject(Server server) {
420+
this.fetched = true;
425421
this.id = server.getId();
426422
this.name = server.getName();
427423
this.address = server.getAddress();
@@ -457,9 +453,7 @@ public void subscribe() {
457453
throw new IllegalStateException("Websocket connection already active.");
458454
}
459455

460-
String protocol = this.client.getProtocol().equals("https") ? "wss" : "ws";
461-
String uri = protocol + "://" + this.client.getHost() + this.client.getBasePath() + "servers/" + this.id + "/websocket";
462-
this.webSocket = new WebSocketManager(gson, uri, this.client.getApiToken(), this);
456+
this.webSocket = client.connectToWebSocket(this, "servers/" + this.id + "/websocket");
463457
}
464458

465459
/**
@@ -551,7 +545,7 @@ public void addTickSubscriber(TickSubscriber subscriber) {
551545
/**
552546
* @return web socket manager
553547
*/
554-
public WebSocketManager getWebSocket() {
548+
public WebSocketConnection getWebSocket() {
555549
return webSocket;
556550
}
557551
}

src/main/java/com/exaroton/api/ws/WebSocketClient.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)