GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev โ WebSocket clients with java.net.http | java.evolved
I/O
Intermediate
WebSocket clients with java.net.httpUse the standard asynchronous WebSocket client instead of adding a third-party dependency. Code Comparison
โ Third-party library
WebSocketClient client =
new WebSocketClient(serverUri) {
@Override
public void onMessage(String message) {
handle(message);
}
};
client.connect();
โ Java 11+
HttpClient.newHttpClient()
.newWebSocketBuilder()
.buildAsync(serverUri,
new WebSocket.Listener() {
@Override
public CompletionStage<?> onText(
WebSocket socket,
CharSequence data,
boolean last) {
handle(data.toString());
return WebSocket.Listener.super
.onText(socket, data, last);
}
});
See a problem with this code? Let us know.
Why the modern way wins
๐ฆ
No extra dependencyThe WebSocket client ships with the JDK. ๐งต
Asynchronous APIConnection and message handling compose with CompletionStage. ๐
Shared HTTP stackProxy, TLS, and executor configuration use standard JDK facilities. Old Approach
Third-party WebSocket client
Modern Approach
java.net.http.WebSocket
Since JDK
11
Difficulty
Intermediate
JDK Support
WebSocket clients with java.net.http
Available
Widely available since JDK 11 (September 2018) How it works
The java.net.http package includes an asynchronous WebSocket client integrated with the standard HTTP client. Connections are established with CompletableFuture, listener callbacks support non-blocking message processing through CompletionStage, and the client shares the JDK's standard proxy, TLS, and executor configuration. Related Documentation
Proof
Related patterns
I/O
Beginner
Modern HTTP clientJava 8
URL url = new URL("https://api.com/data");
HttpURLConnection con =
(HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
// read lines, close streams...
Java 11+
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.com/data"))
.build();
var response = client.send(
request, BodyHandlers.ofString());
String body = response.body();
JDK 11+
โ
Concurrency
Intermediate
CompletableFuture chainingPre-Java 8
Future<String> future =
executor.submit(this::fetchData);
String data = future.get(); // blocks
String result = transform(data);
Java 8+
CompletableFuture.supplyAsync(
this::fetchData
)
.thenApply(this::transform)
.thenAccept(IO::println);
JDK 8+
โ
Concurrency
Intermediate
Concurrent HTTP with virtual threadsJava 8
ExecutorService pool =
Executors.newFixedThreadPool(10);
List<Future<String>> futures =
urls.stream()
.map(u -> pool.submit(
() -> fetchUrl(u)))
.toList();
// manual shutdown, blocking get()
Java 21+
try (var exec = Executors
.newVirtualThreadPerTaskExecutor()) {
var results = urls.stream()
.map(u -> exec.submit(
() -> client.send(req(u),
ofString()).body()))
.toList().stream()
.map(Future::join).toList();
}
JDK 21+
โ
|
| You are visitor 01402503 โ GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร600. No cookies required. |