Java Reference
In-Depth Information
architectural point of view because WebSocket message handlers don't usually match gran-
ular business methods. WebSockets are fundamentally different from technologies such as
JAX-WS that have well-defined messages and are usually used to expose business func-
tionality to external systems.
Now that you've done some of the groundwork, we'll look at the Session interface be-
fore diving into the details of programmatic and annotated endpoints.
14.3.2. Session interface
The javax.websocket.Session interface is one of the most important WebSocket
classes. You'll use this interface whether you're using programmatic or annotated end-
points. It represents a connection of a client to the server and is an abstract representation
of the underlying socket connection. With WebSockets you're never able to access the un-
derlying java.net.Socket instance; the Session interface is the closest you'll come
to it.
An instance of the WebSocket interface is created when a client successfully initiates a con-
nection to the server. A Session instance provides a number of useful methods, including
those for sending messages back to the client, storing state, and obtaining more information
about the connection. Depending on whether the endpoint is programmatic or annotated,
you can acquire a reference to the session either in the onOpen handler or in the onMes-
sage handler of an annotated endpoint. Several of the other callbacks can also optionally
provide the Session interface.
Sending a message
The WebSocket interface contains two methods that you use to send messages back to the
client:
getAsyncRemote()— Sends asynchronous messages to the client
getBasicRemote()— Sends synchronous messages to the client
Which method you choose depends on whether you want to send a message asynchron-
ously or synchronously. The asynchronous method returns a RemoteEndpoint.Async
Search WWH ::




Custom Search