In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to understand Long Polling, in view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Introduction
As we all know, there are two modes of data interaction: Push (push mode) and Pull (pull mode).
Push mode refers to the establishment of a long-term network connection between the client and the server, and the server has relevant data, which is directly pushed to the client through the persistent connection channel. Its advantage is timely, once there is a data change, the client can perceive it immediately; in addition, the logic for the client is simple, and there is no need to care about the logical processing of data. The disadvantage is that we do not know the data consumption capacity of the client, which may lead to the backlog of data on the client and there is no time to deal with it.
Pull mode means that the client initiatively sends a request to the server to pull the relevant data. The advantage is that this process is initiated by the client, so there is no problem of data backlog in push mode. The disadvantage is that it may not be timely enough, for the client, it is necessary to consider the relevant logic of data pull, when to pull, how to control the frequency of pull, and so on.
Detailed explanation
When it comes to Long Polling (long polling), it is inevitable to mention Polling (polling), which are two ways to pull mode.
Polling means that no matter whether the server data is updated or not, the client requests to pull data every long time, and the updated data may be returned or there may be nothing at all.
The principle of Long Polling is also very simple. Compared with Polling, the client initiates Long Polling. If the server has no relevant data, it will hold the request until the server has the relevant data, or wait for a certain time to time out before returning. Upon return, the client will immediately initiate the next Long Polling. This method is also an optimization of pull mode, which solves the problem that the data notification of pull mode is not timely, and reduces a large number of invalid polling times. (the so-called hold request means that the server temporarily does not reply to the result, saves the relevant request, does not close the request connection, and writes to the client when the relevant data is ready. )
As mentioned earlier, if the Long Polling does not have the relevant data needed by the server at that time, the request will hold until the relevant data is ready or wait for a certain period of time until the request times out. Do you have any questions here? why not wait until the data on the server is ready before returning, so that you do not need to launch the next Long Polling again to save resources?
The main reason is that the network transport layer mainly adopts tcp protocol, and tcp protocol is a reliable connection-oriented protocol, which establishes the connection through three-way handshake. However, the established connection is virtual, and there may be a certain period of network failure, or the server program is abnormally shut down, or the server machine is abnormally shut down. in the face of these situations, the client simply does not know that the server can no longer communicate with each other at this time. Still foolishly waiting for the server to send data, which usually takes a long time. Of course, the tcp protocol stack is guaranteed by a survival timer, but it takes a long time to find that the connection has been disconnected. If there is no special configuration of the relevant tcp parameters, it usually takes 2 hours, and these parameters are at the machine operating system level, so it is not reliable to keep alive in this way, so the implementation of Long Polling generally needs to set a timeout.
Realize
The implementation of Long Polling is simple and can be divided into four processes:
Initiate Polling
Initiating Polling is very simple, you only need to initiate a request to the server, and the server has not answered yet, so there is always a connection between the client and the server.
Data push
If the server has relevant data, the server will send the data back to the client through the previously established channel.
Polling termination
There are three types of Polling termination scenarios:
If the server returns relevant data, after receiving the data, the client closes the request connection and ends the Polling process.
If the client waits for the set timeout period and the server still does not return data, the client needs to actively terminate the Polling request.
If the client receives a network failure or exception, the client naturally needs to take the initiative to terminate the Polling request.
Re-Polling
After terminating the last Polling, the client needs to initiate the Polling request again immediately. Only in this way can we ensure the timeliness of pulling data.
The code is also very simple to implement, and Http Call follows the above process to easily implement LongPolling. The following Code simply shows the process and adjusts it according to the specific business logic in a specific scenario.
Client Code
Server Code
Result
Application
WebQQ and Comet both use long polling technology, and other messaging systems consumed by Pull mode will be optimized using Long Polling technology.
The answer to the question on how to understand Long Polling is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.