Chapter 7. WebSocket
The Java API for WebSocket is defined as JSR 356, and the complete specification can be downloaded.
WebSocket provides a full-duplex and bidirectional communication protocol over a single TCP connection. Full-duplex means a client and server can send messages independent of each other. Bidirectional means a client can send a message to the server and vice versa. WebSocket is a combination of the IETF RFC 6455 Protocol and the W3C JavaScript API. The protocol defines an opening handshake and basic message framing, layered over TCP. The API enables web pages to use the WebSocket protocol for two-way communication with the remote host.
Unlike HTTP, there is no need to create a new TCP connection and send a message chock-full of headers for every exchange between client and server. Once the initial handshake happens via HTTP Upgrade (defined in RFC 2616, section 14.42), the client and server can send messages to each other, independent of the other. There are no predefined message exchange patterns of request/response or one-way between client and server. These need to be explicitly defined over the basic protocol.
The communication between client and server is pretty symmetric, but there are two differences:
A client initiates a connection to a server that is listening for a WebSocket request.
A client connects to one server using a URI. A server may listen to requests from multiple clients on the same URI.
Other than these two differences, the client and server behave ...
Get Java EE 7 Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.