Java Reference
In-Depth Information
public class AcmeChatEndpointIf extends javax.websocket.Endpoint {
@Override
public void onOpen(Session session, EndpointConfig config) {
// Implementation here
}
public void onClose(Session session){
// Implementation here
}
public void onError(Session session){
// Implementation here
}
}
Creating WebSocket Clients
A POJO class can be made a WebSocket client by annotating it with @ClientEndpoint . Any POJO that is annotated
as such can be used to initiate, but will not listen for incoming requests and therefore contains on path specification.
The following code demonstrates an example of such a client.
@ClientEndpoint
public class AcmeChatClient {
@OnOpen
public void onOpen(Session session) {
System.out.println("Connected to endpoint: " + session.getAsyncRemote());
String name = "Message from the Acme Chat Client";
session.getAsyncRemote().sendText(name);
}
@OnMessage
public void processMessage(String message) {
System.out.println("Received message in client: " + message);
}
}
Utilizing a Simple Client
To test a WebSocket without creating the associated browser client page, make use of the
ContainerProvider.getWebSocketContainer() method. This method returns a WebSocketContainer object,
which can then be used to connect to an existing web socket endpoint. The following class demonstrates how to test
a connection to a WebSocket endpoint using a simple client.
 
Search WWH ::




Custom Search