Rego takes you understand the principles webSocket

Occasionally seen on an almost Replies know instantly feel so much information before and this one did not see the replies made me  websocket know there is a deep wood. So go to my blog, to share. I prefer to see this blog, very easy to read, not boring, no fuss preacher purely to share. So much nonsense, and finally praise a ~

A, websocket and http

HTML5 WebSocket is a thing (agreement), which means that the HTTP protocol has not changed, or that it does not matter, but does not support persistent HTTP connections (long connection, not a loop connection)

First, there is HTTP  1.1 and  1.0 said, so-called  keep-alive , multiple HTTP requests into one, but  Websocket is actually a new agreement with the HTTP protocol basically does not matter, just to shake hands specification compatible with existing browsers only, that is to say it is a supplement to the HTTP protocol this can be understood by the one in FIG.

Intersect, but not all.

In addition Html5 refers to a series of new API, or new norms, new technology. Http protocol itself is only 1.0 and 1.1, but also with Html itself is not directly related. . Popular, you can transfer data with non-Html HTTP protocol, it is so =. =

In short then, the hierarchy is not the same.

Two, Websocket what kind of agreement, what specific advantages

First, Websocket protocol is a persistent, non-persistent with respect to the HTTP protocol for this. Simple To give an example, with currently used widely in the life cycle of PHP to explain.

HTTP life cycle through  Request to define, it is a  Request one  Response , then  HTTP1.0 in the HTTP request is over.

HTTP1.1 the improvement in such a keep-alive, that is, in a HTTP connection may be sent a plurality of Request, receiving a plurality Response. But remember  Request = Response, in HTTP will always be so, that a request can be only one response. And the response is passive, can not initiate.

Coach, BB you so much, have anything to do with Websocket it? _ (: З "∠) _ Well, I was going to say Websocket it. .

First Websocket is based on the HTTP protocol, or HTTP protocols borrowed to complete part of the handshake.

First we look at a typical  Websocket handshake (borrowed from Wikipedia's ..)

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 Origin: http://example.com

HTTP familiar with the children's shoes may have discovered, this is similar to a handshake request HTTP protocol, a few more things. I will explain the role of the way.

Upgrade: websocket
Connection: Upgrade

This is the core of Websocket tell  Apache ,  Nginx and other server: pay attention to you, I launched a Websocket agreement, hurry to help me find the corresponding processing assistant - not the old-fashioned HTTP.

Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13

First of all,  Sec-WebSocket-Key it is a  Base64 encode value, this is a browser randomly generated, tells the server: peat, do not flicker nest, I want to verify that Nigeria is not really Websocket assistant.

Then,  Sec_WebSocket-Protocol a user-defined character string, the URL is used to distinguish the same, different services protocol needs. Simple to understand: Tonight I want to service A, make no mistake friends ~

Finally,  Sec-WebSocket-Version it is to tell the server that is used  Websocket Draft (protocol version), for the first time, Websocket agreement still  Draft stage, all kinds of weird protocol has, but there are a lot of different things weird, what Firefox and Chrome , the original agreement with the not too Websocket a version of the class, but a big problem. . But fortunately now, it has been decided - something we all use - Dehydration:  Waiter, I want a 13-year-old Oh → _ →

The server then returns the following things, said that it has received the request, successfully established Websocket it!

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: chat

Finally, here is the area responsible for HTTP, tell the customer, I have successfully switched protocols friends ~

Upgrade: websocket
Connection: Upgrade

Remains fixed, tell the client is to be upgraded  Websocket protocol, rather than mozillasocket, lurnarsocket or shitsocket.

Then,  Sec-WebSocket-Accept this is confirmed through the server, and after the encryption  Sec-WebSocket-Key . Server: OK, OK, I know you, you look at my ID CARD to prove okay. .

The latter,  Sec-WebSocket-Protocol it is a protocol end-use.

So far, HTTP has completed all of its work, the next is completely in accordance with Websocket agreement. In this particular agreement is not elaborated.

------ ------ complete technical analysis section

You TMD and BBB for so long that in the end what is a ghost Websocket use,  http long poll or ajax轮询 not can achieve real-time information delivered it.

Ok, young people, then we'll talk about Websocket what purpose. To give you eat Hu (Su) dill (Dan) BU (red)

Third, the role of Websocket

Speaking before the Websocket, I would say conveniently slide under  long poll and  ajax轮询 principles.

ajax poll

Ajax polling principle is very simple, every few seconds to let the browser sends a request, ask the server for new messages.

Scene reproduction:

Client: la la la, there is no new information (Request)

Server: no (Response)

Client: la la la, there is no new information (Request)

Server: no. . (Response)

Client: la la la, there is no new information (Request)

Server: hello Fana, no ah. . (Response)

Client: la la la, there are no new messages (Request)

Server: OK, OK, there are friends to you. (Response)

Client: la la la, there are no new messages (Request)

Server:. . . . . No. . . . No. . . No (Response) - loop

long poll

long poll In fact, the principle with  ajax轮询 almost all polling way, but adopted a blocking model (has been called, did not receive not hang up), that is to say, the client initiates a connection, if not the message, it will not return Response to the client. Not return until a message is returned after completing the client to establish a connection again, again and again.

Scene reproduction:

Client: la la la, there is no new information, no word on the other had only returned to me (Request)

Server: the amount. . Time to wait for news. . To give you (Response)

Client: la la la, there is no new information, no word on the other had only returned to me (Request) -loop

In fact, this can be seen from the above two methods are constantly establishes an HTTP connection, and then wait for the server process, may reflect another feature, passive HTTP protocol.

What is the passivity of it, in fact, the server can not take the initiative to contact the client, only the client initiates.

It simply is, the server is a lazy fridge (this is a Terrier) (No, can not initiate the connection), but the boss is ordered, if there are customers, no matter how tired have a good reception.

Having this, let's talk about the above defects (forgive me so much nonsense it OAQ)

From the above it is easy to see, anyway, above both of which are very resource-intensive.

ajax polling require server has fast processing speed and resources. (Speed) long poll needs to have a high concurrency, that is to say the ability to simultaneously receive clients. (Size of the venue)

So  ajax轮询 and  long poll have this situation may occur.

Client: La La La La, there is new information it?

Server: Monthly line is busy, please try again later (503 Server Unavailable)

Client:. . . . Well, la la la, there is new information it?

Server: Monthly line is busy, please try again later (503 Server Unavailable)

Client: The server then busy dying at the side: the refrigerator, I want more of a refrigerator! More. . More. . (I was wrong .. This is another terrier ..)

Closer to home, for us it Websocket

By the above example, we can see that these two approaches are not the best way, it requires a lot of resources.

For a faster rate of need, a necessity more 'phone'. Both can lead to 'phone' demand for higher and higher.

Oh yeah, I forgot to say a state or HTTP protocol.

Popular to say that, because the server must receive too many customers every day, is a ghost forgetful, as you hang up, he took the whole thing forgotten you, and you lost the whole thing. The second time you'll have to tell the server again.

So in this case occurred, Websocket appeared. He solved the HTTP of these problems. First of all, passive, when the server is completed (HTTP-> Websocket) protocol upgrade, the server can proactively push information to the client it. So the above scenarios can be modified as follows.

Client: la la la, I want to build Websocket protocols, services required: chat, Websocket Protocol Version: 17 (HTTP Request)

Server: ok, confirm, has been upgraded to Websocket protocol (HTTP Protocols Switched)

Client: Could you give me information when pushed Oh. .

Server: ok, sometimes I will tell you.

Server: balabalabalabala

Server: balabalabalabala

Server: ah ha ha ha ha ha ha ha ha ha ha

Server: killing me ha ha ha ha ha ha ha

It becomes so, only need to go through an HTTP request, you can do a steady stream of information transmitted. (In programming, this design is called a callback, namely: Do you have information, come and inform me, not silly every time I came to ask you)

This agreement resolves the above synchronization delay, but also very resource consuming this case. So why he would solve the problem of consumption of resources on the server?

In fact, we used to go through two procedures is a proxy, HTTP protocol, etc. In parsing Nginx server, and then sent the appropriate Handler (PHP, etc.) to deal with. Simply put, we have a very fast  接线员(Nginx) , he is responsible for the issue forwarded to the appropriate  客服(Handler) .

Basically speed operator itself is adequate, but every card in the customer service (Handler), the old and the customer service process too slow. , Resulting in customer service is not enough. Websocket to solve such a problem, the establishment of a lasting connection can be established directly with the operator, when customer information to find ways to inform the operator, then transferred to the operator in a unified client.

This problem can be solved customer service too slow a process.

Meanwhile, in the traditional way, we must continue to establish close HTTP protocol, due to the state of non-HTTP, each time re-transmission  identity info (identification information) to tell the server who you are.

Although the operator is very fast, but every time to listen to such a pile, efficiency will decline, but will have to continue to put this information to the customer service, not only a waste of time to deal with customer service, but also consumed in the transmission network excessive flow / time.

But Websocket HTTP handshake only once, so that the entire communication process is based on a connection / state, thus avoiding a state of non-HTTP, the server will always know your information until you shut down the request, this would resolve the the operator repeatedly parsing HTTP protocol, but also view information of identity info.

At the same time take the initiative to ask the customer, converted to a server (push) when the information is sent (of course, the client or wait for the initiative to send information over ..), when no information is given to the operator (Nginx), itself does not take up speed slow customer service (Handler) a

——————–

As for how to use Websocket on unsupported Websocket clients. . The answer is:  not

However, by the above said  long poll , and  ajax 轮询 to simulate the similar effects

Guess you like

Origin www.cnblogs.com/leigepython/p/11058378.html