Java Reference
In-Depth Information
ent sends another request and so on. Although it seems good, this technique re-
quires proprietary solutions (comet) and when data are frequently updated, the loop
connection-request-response-disconnection may negatively impact the network.
WebSocket is not an HTTP-based technique, it is a protocol that provides a new and
better way to overcome the shortcomings of the HTTP protocol in real-time commu-
nication between web client and web server/service.
The WebSocket API
The Java API for WebSocket 1.0 defines a standard API to build WebSocket-driven
applications in the Java EE platform.
A WebSocket application consists of two types of components called endpoints: a
client endpoint and a server endpoint. A client endpoint is the component that ini-
tiates a WebSocket connection, while a server endpoint is waiting for connections.
With the Java API for WebSocket 1.0, both component types can be created either
programmatically or declaratively by using annotations. In this chapter we will only
see annotated endpoints in a small student chat room application.
Server endpoint
The following code demonstrates how to create a WebSocket endpoint that is able
to accept client connections and send messages:
@ServerEndpoint("/chatserver")
public class ChatServerEndPoint {
@OnOpen
public void openConnection(Session session)
throws Exception {
//...
}
@OnMessage
public void onMessage(Session session, String
msg)throws Exception {
//...
}
Search WWH ::




Custom Search