O'Reilly logo

Java EE 7 Developer Handbook by Peter A. Pilgrim

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Client-side Java WebSockets

The Java WebSocket API also allows developers to write client endpoints. They can be easily defined with annotation and of course with more difficulty by programming against the configuration API.

The annotation @javax.websock.ClientEndpoint denotes a client specific WebSocket endpoint.

Here is the code for a sample client WebSocket:

package je7hb.websocket.basic;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnMessage;
import javax.websocket.Session;

@ClientEndpoint
public class ClientEchoEndpoint {

  @OnMessage
  public void messageReceived( Session session, String text ) {
    System.out.printf("Message server text: %s\n", text);
  }
}

The client ClientEchoEndpoint is defined as an annotated WebSocket endpoint. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required