WebSocket is a TCP-based protocol like HTTP which is part of HTML 5 specification. Unlike HTTP which is a request response synchronous protocol, WebSocket provides a two way (full-duplex) communications channel over a single TCP connection and messages (textual and binary) can be sent between client and server asynchronously without the overhead of HTTP request/response life cycle. Note that when using WebSockets, both parties can send and receive messages and hence this is closer to a peer-to-peer architecture than a client server architecture like HTTP. An endpoint can be considered as the address and entry point to a websocket. Both parties will have a send/receive endpoint as both parties can send/receive messages asynchronously. HTML5-based web applications can now transport JSON data using WebSockets, in addition to Ajax and other transport technologies. Though WebSocket was designed to be implemented in web browsers and web servers, it can be used by any client or server application.
The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers. HTTP/1.1 introduced support for the upgrade header field that allows clients to request to change protocol to a newer http protocol version or switch to a different protocol. WebSocket uses this mechanism to set up a connection with a HTTP server. The WebSocket Protocol has two parts: a handshake to establish the upgraded connection, then the actual data transfer. First, a client requests a websocket connection by using the "Upgrade: websocket" and "Connection: Upgrade" headers, along with a few protocol-specific headers to establish the version being used and set-up a handshake. The server, if it supports the protocol, replies with the same "Upgrade: websocket" and "Connection: Upgrade" headers and completes the handshake. Once the handshake is completed successfully, data transfer begins.
Example HTTP request and response from the Wikipedia page:
GET /chat HTTP/1.1
Sec-WebSocket-Protocol: chat, superchat
HTTP/1.1 101 Switching Protocols
The Java EE platform’s Java API for WebSocket enables you to create, configure, and deploy WebSocket endpoints in Java EE web applications. To accept messages from clients, a server-side class must be configured as a WebSocket endpoint. The WebSocket client API enables you to access remote WebSocket endpoints from any Java application. The javax.websocket.server package is specific to server endpoints whereas javax.websocket package is common to client and server endpoints.
With Java API for WebSocket, you can create two kinds of endpoints: programmatic endpoints and annotated endpoints. To create a programmatic endpoint, you extend the Endpoint class and override its lifecycle methods. To create an annotated endpoint, you decorate a Java class and some of its methods with the annotations. Java EE 7 provides @ServerEndpoint annotation for marking a class as endpoint and also provides @OnMessage annotation to annotate a message that will be invoked to process the message that is sent to the WebSocket endpoint.
You can find many examples on WebSocket Java EE Example that work with Glassfish, but since our direction is on eclipse+tomcat, we are working to make those examples work in an eclipse+tomcat environment. By the time you read this we might have already found out a solution. Please go to Java EE notes book here or use a search, and see if any examples are added. If you have got it working yourself, please share it with us and help us as well as our readers.
Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!