Java Reference
In-Depth Information
The use of two different scenarios is shown here to demonstrate the two different ap-
proaches to implementing WebSocket endpoints with Java EE. The first, chat support,
demonstrates the use of a programmatic endpoint where you have greater control. The
second, the dashboard/bulletin, demonstrates the use of an annotated endpoint. There's also
one more distinction between these two scenarios. In the first scenario, the WebSocket is
being used to connect two users. In the second scenario, events are published out to all con-
nected CSRs. Looking at the source code for these two examples will give you a template
for implementing your own endpoints. Both examples will work in a load-balanced envir-
onment where there are multiple servers.
Figure 14.1
provides an overview of the chat-support implementation. A user, such as a
bidder or seller, will navigate to the chat page. The JavaScript code on the page will auto-
matically initiate a WebSocket connection with the
ClientChatEndpoint
(
/chat
).
The
ClientChatEndpoint
will then call the
ChatServer
, which is a singleton bean.
On the other end, when a CSR logs into the support page, the JavaScript code on that page
will automatically initiate a WebSocket invocation with the
SupportChatEndpoint
(
/admin/support
). The
SupportChatEndpoint
then passes along the request to
the
ChatServer
singleton bean. The
ChatServer
is thus the nexus and is respons-
ible for routing the incoming messages. It caches the
javax.websocket.Session
for
both the client and CSR endpoints. The code for the
ChatServer
bean is shown in the
next listing.