This 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
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. 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
protected void onOpen(WsOutbound outbound);
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
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);
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.