On Websocket, Ajax polling and long polling (long p0ll)
Websocket introduced recently seen some articles, I feel quite useless, so here the record of their own understanding of what the three.
1. What is Websocket
Websocket new HTML5 proposed agreement , note that this is the protocol can communicate the client and server, the server implementation push function.
2.Websocket HTTP protocol and what is the relationship
In simple terms, Websocket and HTTP has a relationship, but little to do, their relationship is similar to mathematics intersection, as shown below (borrowed Ovear Figure). Websocket borrowed part of the HTTP protocol to complete the handshake process.
3.Websocket handshake
When a client wants to establish a connection Websocket, send it to the server:
GET /chat HTTP/1.1
Host: xxx.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://xxx.com
Which, Upgrade: websocket and Connection: Upgrade tells the server, I want to establish websocket connection; also return to the browser after Sec-WebSocket-Key part of the server encryption, to ensure the establishment of a websocket connection; Sec-WebSocket-Version: 13 is websocket version number.
When the server receives the packet, it will return Content:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
It tells the client that I've switched to websocket agreement, the content of Sec-WebSocket-Accept Sec-WebSocket-Key is encrypted, so that a websocket connection is established.
4.Websocket how it works
Client: I want to establish a connection websocket
Server: good, has been switched to websocket protocol, connection is established websocket
Client: What is the message we must promptly tell (
push
) I
Server-side: good
Server: xxxxxx
Server-side: yyyyyyy
。。。。。
This has the advantage that, as long to establish a connection, can be obtained continuously push message server, saving bandwidth and server-side pressure.
5.Ajax polling how to achieve
In fact, the majority of small partners know, ajax long polling connection is simulated for each period of time (0.5s) initiates ajax request to the server, the server queries whether there is data update
Client: Is there any news
Server-side: no. .
Client: Is there any news
Server-side: there, xxxxx
Client: Is there any news
Server-side: no. .
Client: Is there any news
Server-side: no. .
。。。。。。
The disadvantage is obvious, every time an HTTP connection, even if the data needs to be transmitted is very small, so this is a waste of bandwidth; at the same time, this process is passive, that is not the server actively push.
6. Long polling (long poll)
Client: Is there any new information (Request)
server: No,
(The passage of time has been ..., waiting for the time when there is a message)
Server-side: you xxxx (Response)
Client: There are no new messages (Request)
。。。。。
Its shortcomings are obvious, like ajax poll, but also every time an HTTP connection, are also passive. And this method requires a relatively large parallel server, because when there is no message, the connection still maintained, but then additional information is required and also establish a new connection (connection holding it).
7. References:
. a Baidu
b.http: //www.zhihu.com/question/20215561/answer/40316953
On Websocket, Ajax polling and long polling (long p0ll)
Websocket introduced recently seen some articles, I feel quite useless, so here the record of their own understanding of what the three.
1. What is Websocket
Websocket new HTML5 proposed agreement , note that this is the protocol can communicate the client and server, the server implementation push function.
2.Websocket HTTP protocol and what is the relationship
In simple terms, Websocket and HTTP has a relationship, but little to do, their relationship is similar to mathematics intersection, as shown below (borrowed Ovear Figure). Websocket borrowed part of the HTTP protocol to complete the handshake process.
3.Websocket handshake
When a client wants to establish a connection Websocket, send it to the server:
GET /chat HTTP/1.1
Host: xxx.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://xxx.com
Which, Upgrade: websocket and Connection: Upgrade tells the server, I want to establish websocket connection; also return to the browser after Sec-WebSocket-Key part of the server encryption, to ensure the establishment of a websocket connection; Sec-WebSocket-Version: 13 is websocket version number.
When the server receives the packet, it will return Content:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
It tells the client that I've switched to websocket agreement, the content of Sec-WebSocket-Accept Sec-WebSocket-Key is encrypted, so that a websocket connection is established.
4.Websocket how it works
Client: I want to establish a connection websocket
Server: good, has been switched to websocket protocol, connection is established websocket
Client: What is the message we must promptly tell (
push
) I
Server-side: good
Server: xxxxxx
Server-side: yyyyyyy
。。。。。
This has the advantage that, as long to establish a connection, can be obtained continuously push message server, saving bandwidth and server-side pressure.
5.Ajax polling how to achieve
In fact, the majority of small partners know, ajax long polling connection is simulated for each period of time (0.5s) initiates ajax request to the server, the server queries whether there is data update
Client: Is there any news
Server-side: no. .
Client: Is there any news
Server-side: there, xxxxx
Client: Is there any news
Server-side: no. .
Client: Is there any news
Server-side: no. .
。。。。。。
The disadvantage is obvious, every time an HTTP connection, even if the data needs to be transmitted is very small, so this is a waste of bandwidth; at the same time, this process is passive, that is not the server actively push.
6. Long polling (long poll)
Client: Is there any new information (Request)
server: No,
(The passage of time has been ..., waiting for the time when there is a message)
Server-side: you xxxx (Response)
Client: There are no new messages (Request)
。。。。。
Its shortcomings are obvious, like ajax poll, but also every time an HTTP connection, are also passive. And this method requires a relatively large parallel server, because when there is no message, the connection still maintained, but then additional information is required and also establish a new connection (connection holding it).
7. References:
. a Baidu
b.http: //www.zhihu.com/question/20215561/answer/40316953