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
HOSTEROID is a British hosting company founded in...
NVIDIA today announced a 6G research platform tha...
In 2023, technology will further develop, and new...
Evan Kirstel, Chief Digital Evangelist and Co-Fou...
[[184286]] The software development cycle require...
1. Is it my fault that the signal is weak? Whethe...
DediPath has released this month's promotion ...
[51CTO.com Quick Translation] The new year has ar...
OlinkCloud has released a new discount plan for V...
Your boss asks you to handle tens of millions of ...
SASE (Secure Access Service Edge) is a networking...
Looking back at the year 2020, there are many eve...
[[377345]] Some time ago, China Telecom launched ...
HostDare has updated new discount codes, offering...