Long poll, Ajax polling, and WebSocket

Source: Internet
Author: User
Tags base64 encode

WebSocket has a deep understanding of wood. So go to my blog and share. Prefer to read this kind of blog, reading it is easy, not boring, no preacher of the battle, purely for sharing. So much nonsense, and finally praise a ~

WebSocket is HTML5 Out of the Thing (protocol), that is, the HTTP protocol does not change, or it does not matter, but HTTP is not support for persistent connection (long connection, not counting the loop connection)

1.1 and keep-alive , to merge multiple HTTP requests into one, but Websocket is actually a new protocol, with the HTTP protocol basically does not matter, just to be compatible with the existing browser handshake specification, that is, it is a supplement on the HTTP protocol can be understood by such a picture

Another Html5 refers to a series of new APIs, or new specifications, and new technologies. The HTTP protocol itself is only 1.0 and 1.1, and is not directly related to HTML itself. In layman's terms, you can transfer non-HTML data using the HTTP protocol. =

Second, WebSocket is what kind of agreement, concrete have what merit

PHP life cycle to explain.

Request to define, that is, the one Response , then in HTTP1.0 , the HTTP request is over.

Request = Response, which is always the case in HTTP, meaning that a request can have only one Response. And this response is also passive, can not initiate.

First, the WebSocket is based on the HTTP protocol, or the Protocol that borrowed HTTP to complete a part of the handshake.

Websocket shook hands (borrowed from Wikipedia. )

Familiar with the HTTP children's shoes may have found, this is similar to the HTTP protocol handshake request, a few more things. I'll explain the effect by the way.

This is the core of WebSocket, tell the Nginx server: Notice, I initiated the WebSocket agreement, quickly help me find the corresponding assistant processing ~ Not that old-fashioned HTTP.

First of all, Base64 encode the value of this is randomly generated by the browser, tell the server: Peat, do not confuse the nest, I want to verify that Nikki is not really websocket assistant.

Sec_websocket-protocol is a user-defined string used to distinguish between the protocols required for different services under the same URL. Easy to understand: Tonight I have to service A, do not mistake

sec-websocket-version  is to tell the server to use the  draft   Stage, a variety of bizarre agreements have, and there are many strange different things, what Firefox and Chrome is not a version of the same, the original WebSocket agreement too much is a big problem. But now it is OK, has been set up ~ everyone uses a thing ~ dehydration:   Waiter, I want 13-year-old Oh →_→

http/1.1 101 Switching Protocols upgrade:websocket Connection:upgrade sec-websocket-accept: hsmrc0smlyukagmm5oppg2hagwk= Sec-websocket-protocol:chat

Upgrade:websocket Connection:upgrade

Websocket protocol, not mozillasocket,lurnarsocket or shitsocket.

Sec-websocket-accept This is confirmed by the server, and after the encryption Sec-WebSocket-Key . Server: OK, OK, I'll show you my ID card to prove it.

Sec-websocket-protocol is the protocol that represents the end use.

—————— Technical Analysis section is complete ——————

HTTP long poll, or ajax轮询 not all can implement real-time information transfer.

Before I talk about WebSocket, I'll take a walk. ajax轮询The principle.

The principle of Ajax polling is very simple, allowing the browser to send a request every few seconds, asking the server if there is any new information.

Scene Reproduction:

Client: La LA, no new information (Request)

Service side: No (Response)

Client: La LA, no new information (Request)

Service side: No. (Response)

Client: La LA, no new information (Request)

Service side: Hello annoying ah, no AH. (Response)

Client: La LA, there are no new messages (Request)

Server: All right, all right, here you are. (Response)

Client: La LA, there are no new messages (Request)

Service side: ... Didn't.... Didn't... No (Response)--loop

ajax轮询Almost, is the use of polling, but to take a blocking model (always call, do not receive the phone), that is, after the client initiates the connection, if there is no message, it has not returned response to the client. Until a message is returned, the client establishes a connection again and again after the return is completed.

Scene Reproduction:

Client: La La la, there is no new information, no words to wait for it to return to me (Request)

Service side: Amount. Wait till there's a message. Here you Go (Response)

Client: La La la, there is no new information, no words to wait for the return to Me (Request)-loop

What is passive, in fact, the server can not actively contact the client, can only be initiated by the client.

To finish this, let's talk about the above flaws (forgive me for so much nonsense Oaq)

Ajax polling requires the server to have fast processing speed and resources. (speed) Long poll need to have very high concurrency, that is, the ability to host customers at the same time. (Venue size)

This can happen with Ajax polling and long poll both.

Client: La la la, do you have any new information?

Service side: The monthly line is busy, please try again later (503 Server unavailable)

Client:.... All right, cheer up, do you have any new information?

Service side: The monthly line is busy, please try again later (503 Server unavailable)

Client: Then the service side is busy dying: refrigerator, I want more refrigerators! More.. More.. (I was wrong.) This is the stem again. )

From the above example, we can see that neither of these approaches is the best way and requires a lot of resources.

A need for faster speed, a need for more ' phone '. Both of these will lead to higher demand for ' phones '.

Oh yes, forget to say HTTP is still a stateful protocol.

Popular saying is that the server because every day to receive too many customers, is a forgetful ghost, you hang up the phone, he put your things all forgotten, put all your things lost. You'll have to tell the server again the second time.

So in this case, WebSocket appeared. He solves these problems with HTTP. First, the passivity, when the server completes the Protocol upgrade (Http->websocket), the service side can proactively push the message to the client. So the above scenario can be modified as follows.

Client: La LA, I want to set up the WebSocket protocol, required services: Chat,websocket protocol version: + (HTTP Request)

Server: OK, confirm, upgrade to WebSocket protocol (HTTP protocols switched)

Client: Please push it to me when you have information.

Server: OK, sometimes I will tell you.

Service side: Balabalabalabala

Service side: Balabalabalabala

Service side: ha haha haha ah haha haha

Service side: Laughing at me ha ha haha haha

It becomes so that only one HTTP request, you can do a steady stream of information transmission. (in programming, this design is called a callback, that is: you have the information to notify me again, and not my stupid every time to ask you)

In fact, we use the program is to go through two layers of proxy, that is, HTTP protocol in Nginx and other servers, and then sent to the corresponding handler (PHP, etc.) to deal with. To put it simply, we have a very fast one 客服(Handler) .

This will solve the problem of customer service processing speed too slow.

Identity info (identifying information) to tell the server who you are.

But websocket only need an HTTP handshake, so that the entire communication process is established in a connection/state, also avoids the non-state of HTTP, the server will always know your information until you close the request, so that the operator to resolve the HTTP protocol repeatedly, Also view information for identity info.

At the same time by the customer unsolicited inquiry, the conversion to the server (push) has information when it is sent (of course, the client is still waiting to send the message over the active). ), when no information is given to the operator (Nginx), do not need to occupy their own slow speed of customer service (Handler)

——————–

As for how to use WebSocket on clients that do not support websocket. The answer is: no

But you can ajax 轮询 simulate a similar effect by saying that.

Long poll, Ajax polling, and WebSocket

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.