WebSocket Implementation in Tomcat 7 and Jaggery
Join the DZone community and get the full member experience.
Join For FreeThis article will cover:
- Evolution of web communication
- Web socket
- Web socket implemented in Apache Tomcat 7
- How we Develop Web socket feature for jaggery
Tomcat 7 introduced a WebSocket implementation. introduced a WebSocket implementation. Firstly I will give web socket's benefits and limitations plus briefly web socket implemented in Apache Tomcat 7.
WebSocket is considered an evolution of web communication as shown below:
Think you know that Tomcat 6 implements bi-directional communication over HTTP using its Comet Processor. Here is limitation that it came across
- Given that HTTP was intended to be request/response protocol rather than a bi directional protocol
- Proxies and other intermediaries may not work properly
- Only forward packets in one direction at any given time
- Non-standard APIs
- Multi threading for the servlet developers not easy
The Servlet 3.0[1] release introduced a new feature called Asynchronous Servlets. We can compare this feature to the server version of client AJAX calls. It lets a request hang until a response is ready to be delivered without using worker thread on the container. WebSockets is another attempt to standardize (JavaScript AP) an idea of having asynchronous, event driven and bi-directional communication over HTTP.
What web socket will give us
- Bi-directional communication over a HTTP port by upgrading/switching protocols is major difference(Initiated by a HTTP request)
- Message/Frame based communication
- Intended to work with proxies and intermediaries
- Still not the proxies and intermediaries working
Some to to know for the best
- WebSocket are ideal for longer running conversations between client and server.
- WebSocket is a straightforward implementation of bi-directional communication over HTTP.
- Since WebSocket is a protocol sent over TCP after an initial HTTP handshake. You really can only do two things:
- Send messages
- Receive messages
Here WS look like.
- “Connection:upgrade” headers.
- The response contains the critical status code of 101.
- Indicating that a protocol switch has been confirmed and is now expected.
After this handshake is complete the client and server move away from request/response communication and can send messages independently of each other. Here is my frames
How Apache Tomcat Implemented WebSocket
1. To get started with WebSocket you will have to extend Tomcat’s WebSocket class[2].
2. Writing you know class extend the WebSocketServlet class (Since it’s a servlet, you must map it to a URL)
3. Implement a message listener since extending the WebSocketServlet requires us to implement createWebSocketInbound()
It can be implemented to be notified of events. The two required methods are
- protected void onBinaryData(InputStream inStream);
- protected void onTextData(Reader reader);
4. If you wish to be notified when a WebSocket connection is opened or closed, simply override the onOpen and onClose methods
- @Override
protected void onOpen(WsOutbound outbound); - @Override
protected void onClose(int status);
5. Writing data to the client
5.1. We have StreamInbound implementation will have a reference to the sender component, WsOutbound. You simply retrieve it by calling
- myStreamInbound.getWsOutbound()
at that point you can send either binary
- public void writeBinaryData(int b);
- public void writeBinaryMessage(ByteBuffer msgBb);
or textual data to the client
- public void writeTextData(char c);
- public void writeTextMessage(CharBuffer msgBb);
[NOTE]
These methods are mutually exclusive. Don’t call both of these methods and expect both binary and textual data to be sent.
Closing the connection to the client
There are two ways a channel can close in both a “clean” and a “not clean” manner. Clean implies that a handshake has been completed over TCP. Not clean closure means that the TCP connection was disconnected or aborted prior to the close handshake taking place.
- public synchronized void close(int status, ByteBuffer data)
To be notified of when a channel has been closed by the client or uncleanly, you override with.
- protected void onClose(int status);
Jaggery Implementation for the web socket
Here is sequence diagram of jaggery Web socket implementation
Next Post I will give how to used jaggery web socket in Chat application.
Still Above implementation can not see in jaggery as I have not commit it, But in tomorrow (3/25/2014)it will be there.
[1] http://jcp.org/en/jsr/detail?id=315
[2] http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/websocket/WebSocketServlet.html
Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Structured Logging
-
Effective Java Collection Framework: Best Practices and Tips
-
Understanding Dependencies...Visually!
-
How To Approach Java, Databases, and SQL [Video]
Comments