Java Reference
In-Depth Information
4 . Register the endpoint.
The class that you must extend, javax.websocket.Endpoint , is an abstract class.
Its definition is as follows:
public abstract class Endpoint {
public abstract void onOpen(Session session, EndpointConfig config);
public void onClose(Session session, CloseReason closeReason) {
// empty
}
public void onError(Session session, Throwable thr) {
// empty
}
}
An implementation must thus provide an implementation of the onOpen method. Within
this method, registration of the message handler should be performed.
To create a programmatic WebSocket, extend javax.websocket.Endpoint and im-
plement the onOpen method. To configure the endpoint so that it can receive connections,
you need to annotate it with the @ServerEndpoint annotation, which we'll discuss in
the next session when we cover annotated endpoints. Within the onMessage method,
message handlers are registered via the addMessageHandler on the WebSocket ses-
sion object. Only one message handler for each type of message can be registered. These
message types are text, binary, and pong messages we discussed earlier. Once registered,
you're ready to accept messages from the client.
14.4.2. Using annotated endpoints
Annotated endpoints are much easier but less flexible than programmatic endpoints. With
an annotated endpoint, all configuration is performed via annotations. The annotations
mark the class that's to be used as the endpoint, lifecycle callback methods, and methods
that are to process messages. All of the functionality for the endpoint, lifecycle, and mes-
sage processing is combined into a single Java object.
An annotated endpoint is instantiated by the WebSocket implementation. You don't have
control over the creation of this class. An instance is created when a new connection is es-
tablished and it's destroyed when the connection is closed. There are very few requirements
Search WWH ::




Custom Search