package org.argeo.cms.client; import java.net.URI; import java.net.http.HttpClient; import java.net.http.WebSocket; import java.nio.ByteBuffer; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; /** Tests connectivity to the web socket server. */ public class WebSocketEventClient { public static void main(String[] args) throws Exception { if (args.length == 0) { System.err.println("usage: java " + WebSocketEventClient.class.getName() + " "); System.exit(1); return; } URI uri = URI.create(args[0]); WebSocket.Listener listener = new WebSocket.Listener() { public CompletionStage onText(WebSocket webSocket, CharSequence message, boolean last) { System.out.println(message); CompletionStage res = CompletableFuture.completedStage(message.toString()); return res; } @Override public CompletionStage onPong(WebSocket webSocket, ByteBuffer message) { // System.out.println("Pong received."); return null; } }; HttpClient client = HttpClient.newHttpClient(); CompletableFuture ws = client.newWebSocketBuilder().buildAsync(uri, listener); WebSocket webSocket = ws.get(); webSocket.request(Long.MAX_VALUE); Runtime.getRuntime().addShutdownHook(new Thread(() -> webSocket.sendClose(WebSocket.NORMAL_CLOSURE, ""))); while (!webSocket.isInputClosed()) { webSocket.sendPing(ByteBuffer.allocate(0)); Thread.sleep(10000); } } }