Websocket, Ajax round robin, long pull, and websocketajax

Source: Internet
Author: User

Websocket, Ajax round robin, long pull, and websocketajax

Recently I have seen some articles about Websocket, and I think it is quite useful. Here I will record my understanding of the three.

1. What is Websocket?

Websocket is new in HTML5ProtocolNote: The protocol is used to implement communication between the client and the server to implement the server'sPushFunction.

2. What is the relationship between Websocket and HTTP?

In short, Websocket has a relationship with HTTP, but it has little relationship. Their relationship is similar to that in mathematics.Intersection,For example, (borrow an Ovear image ). Websocket uses the HTTP protocol to complete a part of the handshake process.

  

3. Websocket handshake process

  When the client wants to establish a Websocket connection, it sends the following message to the server:

GET/chat HTTP/1.1 Host: xxx. comUpgrade: websocketConnection: UpgradeSec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw = Sec-WebSocket-Protocol: chat, superchatSec-WebSocket-Version: 13 Origin: http://xxx.com in which, Upgrade: websocket and Connection: upgrade tells the server that I want to establish a websocket connection; Sec-WebSocket-Key is encrypted on some servers and then returns to the browser to ensure that a websocket connection is established; Sec-WebSocket-Version: 13 is the websocket version number. After the server receives the preceding package, it returns the following content:HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk = Sec-WebSocket-Protocol: chat it tells the client that I have switched to the websocket Protocol, sec-WebSocket-Accept is the content encrypted by Sec-WebSocket-Key. In this way, a websocket connection is established. 4. How does Websocket work on the client: I want to establish a websocket to connect to the server: Okay, I have switched to the websocket protocol, and the websocket connection has been established on the client: Tell me in time ( Push) My server: Good server: xxxxxx server: yyyyy ..... The advantage is that as long as a connection is established, messages pushed by the server can be continuously obtained, saving the bandwidth and pressure on the server. 5. How to Implement Ajax round robin
In fact, most of our friends know that ajax round-robin simulation of persistent connections means that ajax requests are initiated to the server every time (0.5 s) to query whether the server has a Data Update client: whether there is a new message server: No .. Client: whether there is a new message server: Yes, xxxxx client: whether there is a new message server: No .. Client: whether there is a new message server: No .. ...... Its disadvantage is obvious: every time an HTTP connection is established, even if there is very little data to be transmitted, this is a waste of bandwidth. At the same time, this process is passive, that is, it is not actively pushed by the server. 6. long pull)
Client: whether there is new information (Request)
Server: No (time passes ..., when there is a message,) server side: xxxx (Response) Client: whether there is a new message (Request )..... Its disadvantage is also obvious. Like ajax Round Robin, it is also passive to establish HTTP connections every time. In addition, this method requires a large number of parallel servers, because the connection remains unchanged when there is no message, and other information is required to create a new connection (to maintain the connection ). 7. Reference: a. Baidu
B. http://www.zhihu.com/question/20215561/answer/40316953

Related Article

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.