In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the advantages of WebSocket". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the advantages of WebSocket"?
Why do you need the WebSocket technology?
As we all know, when HTML pages first appeared, they were static and could not interact. Later, with JavaScript, this problem was solved to some extent, but when JavaScript first appeared, it could not interact with the server until the emergence of Ajax. Ajax effectively solves the problem of interaction between the page and the server, but Ajax has a problem, that is, all requests must be initiated by the client, and the server responds. If the server has the latest message, it is difficult to send it to the client immediately. Before the advent of WebSocket technology, in order to enable the client to obtain server data immediately, the following three solutions are generally adopted:
Polling
This is the simplest solution, that is, the client keeps sending requests to the server at a fixed time interval (usually 1 second) to check whether the server has the latest data. If the server has the latest data, it returns it to the client. If not, the server returns an empty json or xml document. This method is simple to implement, but the disadvantages are also obvious. That is, there will be a large number of invalid requests, and the resources on the server side will be greatly wasted.
Long connection
Persistent connections are a bit similar to polling, except that the server does not respond to the client's request every time, but only when the server has the latest data. This method obviously saves network resources and server resources, but there are also some problems, such as:
1. If the browser has new data to send before the server responds, it can only create a new concurrent request, or try to break the current request before creating a new one.
Both 2.TCP and HTTP specifications have a connection timeout, so the so-called persistent connection cannot last forever. The connection between the server and the client needs to be connected and closed regularly. Of course, there are some technologies that can prolong the time of each connection, which is beside the point.
Applet and Flash
Applet and Flash are a thing of the past, but these two technologies not only made our HTML pages more gorgeous, but also solved the problem of message pushing. At a time when Ajax is in trouble to achieve full-duplex communication, developers try to simulate full-duplex communication with Applet and Flash. Developers can create an ordinary transparent Applet or Flash with only one pixel, and then embed it in the page, and then the code in the Applet or Flash creates a Socket connection, which removes various restrictions in the HTTP protocol. When a message from the server is sent to the client, the developer can call the JavaScript function in Applet or Flash, pass the message from the server to the JavaScript function, and then update the page. When the browser has data to send to the server, it can also be transmitted through Applet or Flash. This approach really implements full-duplex communication, but there are also some problems, as follows:
1. Browsers must be able to run Java or Flash
two。 Both Applet and Flash have security problems.
3. With the widespread support of HTML5 in standards and browsers, the removal of Flash has been put on the agenda
Finally giving up, Adobe announced that it would officially stop supporting Flash/ http://tech.163.com/17/0726/07/CQ8M4HT200097U7T.html) in 2020.
What are the characteristics of WebSocket
Since none of the above technologies are good, who can? Of course it's me, WebSocket!
Upgrade features of HTTP/1.1
To talk about the WebSocket protocol, we have to talk about a request header of the HTTP protocol. In fact, all HTTP clients (browsers, mobile devices, etc.) can include Connection:Upgrade in the request header, which indicates that the client wants to upgrade the request protocol, so what kind of protocol do you want to upgrade to? We need to specify a list of one or more protocols in the upgrade header, which must of course be compatible with the HTTP/1.1 protocol. After the server receives the request, if it accepts the upgrade request, it will return a 101status code indicating the transition request protocol and use a single value in the Upgrade header of the response. This single value is the first protocol supported by the server in the request protocol list (that is, the first protocol supported by the server in the protocol list listed in the Upgrade field of the request header).
The biggest advantage of HTTP upgrade is that it finally enables us to use any protocol. After the upgrade handshake is completed, it no longer uses HTTP connection. We can even establish a Socket connection after the upgrade handshake is completed. In theory, we can use HTTP upgrade to use any protocol designed by ourselves between the two endpoints, thus creating all kinds of TCP communications. Of course, browsers will not let developers do this at will. But to specify certain protocols, WebSocket came into being!
Let's look at a screenshot:
WebSocket protocol upgraded with HTTP/1.1
OK, after learning about the upgrade features of the HTTP/1.1 protocol, let's take a closer look at the whole process.
A WebSocket request first accesses a URL in a specific mode using an abnormal HTTP request. The URL has two modes, ws and wss. Corresponding to http and https in the HTTP protocol, there is a Upgrade:websocket in the request header in addition to Connection:Upgrade. Together, they will tell the server to upgrade the connection to a full-duplex protocol such as WebSocket. In this way, after the handshake is complete, text messages or other binary messages can be sent in both directions at the same time without the need to close and rebuild the connection. At this time, the relationship between the client and the server is actually peer-to-peer, and they can both actively send messages to each other. Then there is one thing to note here: ws and wss patterns do not count as part of the HTTP protocol, because HTTP requests and request headers do not contain URL patterns, HTTP requests only contain URL relative to the server in the first line of the request, and domain names in the Host header, while the unique ws and wss modes in WebSocket are mainly used to inform browsers and API that they want to use SSL/TLS (wss). You still want to connect in an unencrypted way (ws).
Advantages of WebSocket protocol
Having said that, let's take a look at the advantages of the WebSocket protocol:
1. Because WebSocket connections are created on port 80 (ws) or 443 (wss), which is the same port used by HTTP, basically all firewalls do not block WebSocket connections
2.WebSocket uses the HTTP protocol for handshaking, so it can be naturally integrated into web browsers and HTTP servers
3. Heartbeat messages (ping and pong) will be sent repeatedly, thus keeping the WebSocket connection active almost all the time. Generally speaking, one node periodically sends a small packet to another node (ping), while the other node responds with a packet containing the same data (pong), so that both nodes will be connected.
4. Using this protocol, both the server and the client can know when the message starts or arrives.
A special closing message will be sent when the 5.WebSocket connection is closed
6.WebSocket supports cross-domain and avoids the limitations of Ajax
The 7.HTTP specification requires browsers to limit the number of concurrent connections to two connections per hostname, but when we use WebSocket, this limit does not exist when the handshake is complete, because the connection is no longer a HTTP connection
The purpose of the WebSocket protocol
After all that has been said, where on earth can the WebSocket protocol be used? In fact, the use of the WebSocket protocol is almost unlimited, such as:
1. Online chat on web pages
two。 Multiplayer online games
3. Online stock website
4. Online instant news website
5. High definition video stream
6. Communication between application clusters
7. Real-time monitoring of the status and performance of remote systems / software
At this point, I believe you have a deeper understanding of "what are the advantages of WebSocket?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.