I want to create a websocket endpoint client in Java (as pure as possible, no frameworks), but virtually all the examples I have found have only server endpoints in Java, whereas the client is in Javascript. Can anyone point me to a good client example, or provide one?
@ClientEndpoint
annottation is part of the spec, so I'd suggest to use that,
here is a good tutorial , see step 4 and 6 .
Step 6 from Openshift tutorial
@ClientEndpoint
public class WordgameClientEndpoint {
private static CountDownLatch latch;
private Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onOpen(Session session) {
// same as above
}
@OnMessage
public String onMessage(String message, Session session) {
// same as above
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
logger.info(String.format("Session %s close because of %s", session.getId(), closeReason));
latch.countDown();
}
public static void main(String[] args) {
latch = new CountDownLatch(1);
ClientManager client = ClientManager.createClient();
try {
client.connectToServer(WordgameClientEndpoint.class, new URI("ws://localhost:8025/websockets/game"));
latch.await();
} catch (DeploymentException | URISyntaxException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
in this example they are using tyrus, which is the reference implementation for websockets in java, so I think that meets your requirement of a well backed websocket implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With