WebSocket in Tomcat 7

Source: Internet
Author: User

Transferred from: http://www.open-open.com/news/view/1b01d43

    In the Tomcat7.0.27 version, Apache gives the implementation of WebSocket, a feature that many Tomcat users have been expecting, and now this feature is available. Now let's take a quick look at what the features and limitations of websockets,websockets are, and how TOMCAT7 is implemented. What is Websockets?    websocket is the next generation of web communication protocol, he has the following features:      full page reload       using AJAX to handle reloading components      Comet communication      is similar to Ajax, but does not hold threads on the server for long periods of time.      TCP bidirectional communication     each of the features listed above has advantages and disadvantages, and TOMCAT6 uses the comet processing mechanism to achieve two-way communication via HTTP. This implementation allows asynchronous event-driven and two-way communication, but this implementation has some limitations:      because HTTP is request-based/appropriate, not a two-way protocol, proxies and other mediation mechanisms may not function properly, and at any given point in time , only one-way transmission.      The introduction of multithreaded development models can become more difficult for server-side developers.      because it is not a standard API, it causes no unified interface.     SERVLET3.0 introduces a new function called asynchronous Servlet,servlet after receiving a request, it may be necessary to first take some preprocessing of the data carried by the request, and then the Servlet The thread forwards the request to an asynchronous thread to perform business processing, and the thread itself returns to the container, where the Servlet has not generated the response data, and the asynchronous thread can generate the response data directly after the business is processed (async threads have ServletRequest and Servletresponse object, or the request continues to be forwarded to the other Servlet. As a result, the Servlet thread is no longer stuck in a blocking state waiting for the processing of the business logic, but instead starts the asynchronous thread and can return immediately.       This feature is more powerful than Tomcat's comet processing mechanism. While this is a standard servlet API, it is very easy to build a common development framework based on it. The      asynchronous servlet solves a part of the network communication needs, but because of its lack of two-way communication support, so that its scope of application is still limited.     websocket is another specification protocol that attempts to support asynchronous, event-driven, and bidirectional communication over HTTP. One form of standardization is the JavaScript API. The lack of a standard server-side api,servlet3.1 is now actively studying some grassroots support for this, which is why I am writing this article. Whether     websocket will evolve into a complete WebSocket API remains to be seen. During this time, the main servlet containers were only supported for nonstandard api,tomcat and no exception. Back to the top websocket brought us what?      now let's take a look at what a websocket implementation brings to us.      bidirectional communication through the Upgrading/switching protocol implemented via HTTP ports. The      communicates in a message or frame.      can work in a slightly adjusted agent or other intermediary mechanism. What does WebSocket look like back at the top?      from a protocol level, WebSocket uses "Upgrade:websocket" on the HTTP protocol to upgrade the HTTP protocol to the WebSocket protocol. This allows the agent to be well supported by some adjustments. A websocket request looks like this:
1 GET /chat HTTP/1.1
2 Host: server.example.com
3 Upgrade: websocket
4 Connection: Upgrade
5 Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
6 Origin: http://example.com
7 Sec-WebSocket-Protocol: chat, superchat
8 Sec-WebSocket-Version: 13

To understand this "Connection:upgrade" request header, look at the server-side returned response containing the following information:
1 HTTP/1.1 101
2 Switching Protocols
3 Upgrade: websocket
4 Connection: Upgrade
5 Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Now that the protocol transitions are licensed, after this handshake is complete, the server and client are out of request/response mode and begin to communicate with each other independently of the socket.    Back to top websocket have you solved all the limitations of the previous comet implementation? A simple answer: No. First, the proxy and mediation mechanisms may still not work without making adjustments, so there is a problem with the wide area network (WAN) deployment. Second, there are still no standard JAVAAPI to support it.    However, WebSocket will gradually solve these problems under the pressure of public opinion around them. It is also important to note that each HTTP/TCP connection requires an initial handshake and introduces a two-way message round-trip, even before you send or accept the WebSocket protocol data. If you neglect this, these very short effects can also make your websocket overhead very large.    In general, however, WebSocket is still the ideal solution for long connections between the client and the server. The good news is that websocket more attention than any other solution that tried to harden web communications, especially client side and cross-language support, which makes websocket a lot more profitable and growing fast. WebSocket's other competitor for two-way communication is spdy, and Tomcat's support for Spdy is under development. Back to the top summary websocket two-way communication is implemented directly via HTTP. WebSocket API is part of the HTML5 specification, so far, because the specification is just a draft, different server implementations differ, and support will vary. Developing websocket can be risky because Java's API is not yet standardized. In addition, WebSocket may not be able to work well under agency and intermediary mechanisms. However, it is important that WebSocket use the Protocol upgrade of HTTP to complete the request/response to TCP connection conversion. This gives the future proxy mechanism a choice that can be chosen on the WAN to support websocket or not. Tomcat7.0.27 is the first version of Tomcat released that supports WebSocket.

WebSocket in Tomcat 7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.