Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a standalone Java websocket client endpoint? [closed]

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?

like image 825
Tyler Durden Avatar asked Feb 17 '14 20:02

Tyler Durden


Video Answer


1 Answers

@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.

like image 200
Leo Avatar answered Oct 07 '22 20:10

Leo