The relationship between WebSocked, SSE, http1.0, http1.1 and http2.0

1. What is WebSocked?

First of all, WebSocked is a new protocol developed by html5, so it has nothing to do with http. To say that it has to be connected, it borrows the http protocol to complete part of the handshake, but it is a connection protocol like http. , Built on top of the tcp protocol, used in the application layer.

So why do we need WebSocked if we already have the http protocol?

Because WebSocked has one of the biggest features, that is, the server can actively push information to the client. The http protocol can only push information from the client to the server.

Many websites use Ajax polling in order to implement push technology. Polling is at a specific time interval (such as every 1 second), the browser sends an HTTP request to the server, and then the server returns the latest data to the client's browser.

Then it is easy to cause the following problems:
Client: "Call me if you have a message"
Server: "OK"
//1s after the time
Client: "Are there any news?"
Server: "No"
//Time
Client after 1s : "Are there any news?"
Server: "No"
//1s after
client: "Is there a news?"
Server: "No"
// 1s after
client: "Yes Is there any news?"
Server: "..."

Such polling is inefficient and wastes resources (because the connection must be kept on, or the HTTP connection is always open).

It will be much better when using websocked
Client: "Call me if you have news"
Server: "OK"
Server: "Incoming news"
Server: "Incoming news"

2. The difference between WebSocked, SSE, http1.0, http1.1 and http2.0

SSE: Server-Sent Events, which is also a protocol introduced by html5. It is only simplex communication. It is a kind of HTTP-based communication in popular interpretation. The server continuously sends data to the client in the form of a stream. technology. The advantage is that the simplicity is almost indistinguishable from the traditional http protocol.

Websocked is a persistent connection protocol, and http does not support persistent connections.

Wecsocked is duplex communication. SSE only supports simplex communication. After a connection is established, it can only be sent from the server to the client, and one connection is occupied. If the client communicates with the server, an additional connection needs to be opened.

HTTP1.0/1.1 can only send data from the client to the server, while websocked can send data from the server to the client.

The life cycle of http1.0 is a request and a response, which is over.

http1.1 has been improved and has a keep-alive, that is, in an HTTP connection, multiple Requests can be sent and multiple Responses can be received. But please remember that Request = Response, which is always the case in HTTP, which means that a request can only have one response. And this response is also passive and cannot be initiated actively.

http/2.0 improves access speed and allows multiplexing: allows multiple request-response messages to be sent through a single connection at the same time, binary framing: divides the transmitted information into smaller messages, and performs binary encoding and header compression. Server push.

3. Implementation of WebSocked

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

Upgrade: websocket
Connection: Upgrade
tells the server that it initiated the websocked protocol

Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
is a series of verifications

Reference: https://www.cnblogs.com/fuqiang88/p/5956363.html
http://www.ruanyifeng.com/blog/2017/05/websocket.html

Guess you like

Origin blog.csdn.net/d1063270962/article/details/109600566