When you use a web browser to play small games in your daily life, have you ever thought about why web games can communicate with the server in real time? Friends who are engaged in technology should have heard of websocket. Before talking about websocket, you need to talk about HTTP first.
HTTP stands for Hypertext Transfer Protocol. The most common one is browser browsing web pages (browser and web server communication). http has the following basic characteristics
Short link, the client (browser) initiates a connection request to the server, and the server gives a receipt message after parsing. After the interaction is completed, the client or server actively disconnects
Related Article:
Comparison of TCP/UDP/HTTP/MQTT Protocols
HTTP VS.RPC, which protocol is the best
Application layer protocol, all data transmission processes are initiated by the client, and the server does not actively send data. There are the following processes
Establish connection: Establish a connection between the client and the server. .
Send request: The client sends a request to the server.
Process request: After receiving the request, the server finds the corresponding resource according to the information in the request and performs the corresponding processing operation.
Send response: The server encapsulates the processed result in the response and sends it back to the client.
Close the connection: After completing the request-response cycle, the connection between the client and the server can be closed.
It can be seen that in HTTP, if you want to continuously obtain data from the server, you must continuously initiate requests. I believe the following scenario
The central gateway for data collection in a smart home system is equipped with a web monitoring interface. Remote login can access home appliances at home. Once, after you remotely log in to the interface, you want to turn on the air purifier and continue to observe the PM2.5 index at home. Since the evolution process takes a long time, more than 1 hour, if you want to monitor in real time, if you follow the interaction logic of the normal http protocol, you can use the timed request (polling) method to obtain data. However, if there are other people in your family who want to view it at this time, if you have 100 family members, at this time, the central gateway server will most likely cry for help, because too many "connect-request-reply-disconnect" processes require the server to constantly apply for and release space, and constantly establish sockets, resulting in a large amount of server time being occupied by requests, instantly increasing server pressure. In order to solve this problem, there is a websocket.
The simple understanding of Websocket is that on the basis of HTTP,
* After the connection is connected, it will not be disconnected unless code is used or manually disconnected
* The server can actively send messages without the client requesting again.
The Websocket communication process has the following stages.
1. Establish a connection (handshake), use HTTP request

Contains key headers:
* Connection: Upgrade
* Upgrade: websocket
* Sec-WebSocket-Key: afmbhhBRQuwCLmnWDRWHxw==
Among them, the two fields "Connection: Upgrade" and "Upgrade: websocket" represent the protocol upgrade to wensocket, and "Sec-WebSocket-Key: afmbhhBRQuwCLmnWDRWHxw==" is similar to a login code, used for connection verification between the client and the server.
2. Server handshake response:

The most important part of the handshake response is: "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo="This value is concatenated with Sec-WebSocket-Key and "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" and encoded with SHA1. The result is then encoded with BASE64 to get "Sec-WebSocket-Accept".
3. Communication process
For this process, please refer to "https://developer.mozilla.org/zh-CN/docs/Web/API/WebSockets_API/Writing_WebSocket_servers". I will not go into details here. You only need to pay attention to the websocket data frame format in the figure below.

4. Close the connection
In the websocket, there is a single bit "FIN" to mark the disconnection. Either party can disconnect the connection.