To better understand WebSocket, we need to start with HTTP, because many people like to compare WebSocket with HTTP. The life cycle of HTTP is defined by Request. With one Request and one Response, the HTTP request ends. In HTTP1.1, sending multiple Requests and receiving multiple Responses are allowed, but no matter what, a Request is always equal to a Response. In order to achieve real-time information transmission, many websites use polling technology. There are two types of polling: ajax polling and long polling. The principle of Ajax polling is very simple. It allows the browser to send a request to the server every few seconds to ask the server whether there is new information.
Long polling is similar to ajax polling, but the difference is that when the client initiates a request, if there is no message, no Response will be returned until there is a new message.
The above two methods are constantly establishing HTTP connections. The server can only respond when the client initiates a request. The server cannot actively initiate a request. Moreover, HTTP is a stateless protocol. The server does not retain any state during transactions with the client. The advantage of this is that it greatly reduces the memory burden of the server and maintains a faster response speed. However, this also creates some problems. The client has to continuously send HTTP requests, each with a long header, which may result in very little valid data, wasting a lot of bandwidth resources and adding pressure to the server. Well, our protagonist WebSocket is finally ready to appear. Compared with HTTP, WebSocket is a persistent protocol. It will complete part of the handshake based on the HTTP protocol, and then it will break away from HTTP and completely adopt WebSocket. The process of establishing a WebSocket connection is:
After completing the above two steps, the HTTP handshake part is completed and the protocol is upgraded to WebSocket. At this time, the server no longer needs the client to initiate requests and respond to requests, and can actively push information to the client.
With just one HTTP request, the server can continuously push information to the client! The client actively asks and the server actively pushes the information, which solves the problem of excessive resource consumption on the server. The above is the principle of WebSocket and the process of achieving persistent connection. |
<<: Common network problem location tools that programmers should master
>>: The so-called ICMP is nothing more than a general and soldiers
On January 8, 2023, China Academy of Information ...
There are always various opportunities for indust...
According to IDC's Worldwide Quarterly Ethern...
On August 11, according to foreign media reports,...
During the COVID-19 pandemic, industries across t...
1. Layering of computer networks In the computer ...
In the continuous promotion of network speed-up a...
In the rapidly developing digital age, the scope ...
If you've ever bought a Wi-Fi router, you pro...
Ethernet IP has revolutionized the world of netwo...
[[276056]] 1. What is TCP and what does it do? TC...
A friend talked about cluster servers. I recently...
V5 Server (V5.NET) is carrying out a spring busin...
[[355718]] While people may think of 5G as a cool...