Heartbeat keep-alive method, system, device and equipment
By dynamically configuring the heartbeat keep-alive interval parameters between the client and the server, the bandwidth waste and connection abnormal deletion problems caused by improper setting of the heartbeat message interval is solved, and the stability of TCP long connections and the improvement of device battery life is achieved.
Patent Information
- Application Number
- CN202310712487.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-15
- Publication Date
- 2024-12-27
AI Technical Summary
In the TCP long connection between the client and the server, the interval time of the heartbeat keep-alive packet is set too small, resulting in bandwidth waste and device power consumption increases. If the setting is too large, it may cause the TCP long connection to be abnormally deleted and unable to communicate normally.
The client sends a heartbeat keep-alive interval parameter request message to the server. The server determines the most matching candidate interval parameters based on the mapping table of latitude and longitude and heartbeat keep-alive interval parameters. The client dynamically configures the interval duration of the heartbeat keep-alive messages to maintain TCP Stability of long connections.
It avoids the problem of over-frequency sending of heartbeat messages, reduces the number of wake-up times of battery-type devices, increases the battery life of the device, and prevents TCP long connections from being deleted abnormally, ensuring normal communication between the server and the client.
Smart Images

Figure CN119211331A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technology, and in particular to a heartbeat keep-alive method, system, device and equipment. Background Art
[0002] In the communication model between the client and the server, after the client and the server establish a TCP (Transmission Control Protocol) long connection, multiple data packets can be continuously sent on the TCP long connection. During the TCP long connection, if no data packet is sent, a heartbeat keep-alive message is needed to maintain the TCP long connection between the client and the server. For example, the interval duration of the heartbeat keep-alive message can be configured based on experience, and the heartbeat keep-alive message is periodically sent based on the interval duration.
[0003] If the interval is set to a small value, a large number of heartbeat keepalive messages will be sent, wasting a lot of bandwidth resources and increasing device power consumption. If the interval is set to a large value, the TCP long connection will be abnormally deleted, and data packets cannot be transmitted between the client and the server, resulting in communication abnormalities. For example, when a client and a server establish a TCP long connection, the TCP long connection will pass through multiple network devices, such as base stations and routers. In order to achieve maximum resource utilization, when the interval of the heartbeat keepalive message is longer than the time for the network device to remove the data-free communication link, the network device will delete the TCP long connection. Summary of the invention
[0004] The present application provides a heartbeat keep-alive method, wherein a TCP long connection is established between a client and a server, and the method is applied to the client, and the method comprises:
[0005] Sending a heartbeat keep-alive interval parameter request message to the server, wherein the heartbeat keep-alive interval parameter request message includes a reference longitude and latitude corresponding to the current location of the client;
[0006] Receive a heartbeat keep-alive interval parameter response message returned by the server, wherein the heartbeat keep-alive interval parameter response message includes a candidate heartbeat keep-alive interval parameter, and the candidate heartbeat keep-alive interval parameter is a heartbeat keep-alive interval parameter corresponding to a target longitude and latitude that satisfies a distance constraint condition with the reference longitude and latitude;
[0007] A heartbeat keep-alive message is sent to the server based on the candidate heartbeat keep-alive interval parameter, where the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
[0008] The present application provides a heartbeat keep-alive system, the system comprising a client and a server, a TCP long connection is established between the client and the server, wherein:
[0009] The client is used to send a heartbeat keep-alive interval parameter request message to the server, wherein the heartbeat keep-alive interval parameter request message includes a reference longitude and latitude corresponding to the current location of the client;
[0010] The server is used to determine a minimum distance based on the distance between the reference longitude and latitude and each longitude and latitude in a mapping table, wherein the mapping table includes a correspondence between longitude and latitude and a heartbeat keep-alive interval parameter; if the minimum distance is less than a preset threshold, the longitude and latitude corresponding to the minimum distance is determined as a target longitude and latitude that satisfies a distance constraint condition with the reference longitude and latitude, and a candidate heartbeat keep-alive interval parameter corresponding to the target longitude and latitude is queried from the mapping table, and a heartbeat keep-alive interval parameter response message is sent to the client, wherein the heartbeat keep-alive interval parameter response message includes the candidate heartbeat keep-alive interval parameter;
[0011] The client is used to send a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter after receiving the heartbeat keep-alive interval parameter response message; wherein the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
[0012] The present application provides a heartbeat keep-alive device, wherein a TCP long connection is established between a client and a server, and the device is applied to the client, and the device comprises:
[0013] A sending module, used for sending a heartbeat keep-alive interval parameter request message to the server, wherein the heartbeat keep-alive interval parameter request message includes a reference longitude and latitude corresponding to the current location of the client;
[0014] A receiving module, configured to receive a heartbeat keep-alive interval parameter response message returned by the server, wherein the heartbeat keep-alive interval parameter response message includes a candidate heartbeat keep-alive interval parameter, wherein the candidate heartbeat keep-alive interval parameter is a heartbeat keep-alive interval parameter corresponding to a target longitude and latitude that satisfies a distance constraint condition with a reference longitude and latitude;
[0015] The sending module is further used to send a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter, and the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
[0016] The present application provides a client device, including: a processor and a machine-readable storage medium, wherein the machine-readable storage medium stores machine-executable instructions that can be executed by the processor; the processor is used to execute the machine-executable instructions to implement the above-mentioned example heartbeat keep-alive method.
[0017] As can be seen from the above technical solutions, in the embodiment of the present application, the corresponding relationship between longitude and latitude and heartbeat keep-alive interval parameters (such as interval duration) can be maintained, and the target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude can be found from all longitudes and latitudes based on the reference longitude and latitude corresponding to the current location of the client, and then the heartbeat keep-alive interval parameter corresponding to the target longitude and latitude is used as the candidate heartbeat keep-alive interval parameter of the client, and the client sends a heartbeat keep-alive message to the server based on this candidate heartbeat keep-alive interval parameter. The candidate heartbeat keep-alive interval parameter is the interval duration that best matches the current location of the client, so as to dynamically configure the matching interval duration for the client. It can avoid the problem of sending a large number of heartbeat keep-alive messages due to too short interval duration, reduce the number of times that battery-type devices (i.e., clients) are awakened to communicate, and increase the battery life of the device. In addition, it can avoid the problem of abnormal deletion of TCP long connections due to too long interval durations, and enable the server and the client to communicate normally with each other, ensuring the stability of TCP long connections. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Figure 1 It is a flowchart of a heartbeat keep-alive method in one embodiment of the present application;
[0019] Figure 2 It is a flowchart of a heartbeat keep-alive method in one embodiment of the present application;
[0020] Figure 3 It is a flowchart of a heartbeat keep-alive method in one embodiment of the present application;
[0021] Figure 4 This is a schematic diagram of the processing flow when a TCP long connection network is abnormal in one implementation of the present application;
[0022] Figure 5 This is a schematic diagram of the structure of a heartbeat keep-alive device in one embodiment of the present application;
[0023] Figure 6 It is a hardware structure diagram of a client device in one implementation of the present application. DETAILED DESCRIPTION
[0024] The terms used in the embodiments of the present application are only for the purpose of describing specific embodiments, rather than limiting the present application. The singular forms of "a", "said" and "the" used in the present application and claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used herein refers to any or all possible combinations of one or more associated listed items.
[0025] It should be understood that, although the terms first, second, third, etc. may be used to describe various information in the embodiments of the present application, these information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of the present application, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, in addition, the word "if" used may be interpreted as "at..." or "when..." or "in response to determination".
[0026] In the embodiment of the present application, a heartbeat keep-alive method is proposed, which can be applied to the client, and a TCP long connection is established between the client and the server, see Figure 1 FIG. 4 is a flow chart of the heartbeat keep-alive method, which may include:
[0027] Step 101: Send a heartbeat keep-alive interval parameter request message to a server. The heartbeat keep-alive interval parameter request message may include a reference longitude and latitude corresponding to the current location of the client.
[0028] Step 102: Receive a heartbeat keep-alive interval parameter response message returned by the server, where the heartbeat keep-alive interval parameter response message may include a candidate heartbeat keep-alive interval parameter, and the candidate heartbeat keep-alive interval parameter is a heartbeat keep-alive interval parameter corresponding to a target longitude and latitude that satisfies a distance constraint condition with the reference longitude and latitude.
[0029] In a possible implementation, the server maintains a mapping table, which may include a correspondence between longitude and latitude and heartbeat keep-alive interval parameters. Based on the distance between the reference longitude and latitude and each longitude and latitude in the mapping table, if the minimum distance is less than a preset threshold (which can be configured based on experience), the longitude and latitude corresponding to the minimum distance may be the target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude.
[0030] Step 103: Send a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter; wherein the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
[0031] Exemplarily, after sending a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter, if a heartbeat keep-alive response returned by the server is received, the heartbeat keep-alive is determined to be successful, and the candidate heartbeat keep-alive interval parameter is determined as the target heartbeat keep-alive interval parameter ultimately adopted by the client; or, if the heartbeat keep-alive response returned by the server is not received, the heartbeat keep-alive is determined to have failed, and the detection heartbeat keep-alive interval parameter between the client and the server is detected, and the detection heartbeat keep-alive interval parameter is determined as the target heartbeat keep-alive interval parameter ultimately adopted by the client. Also, the reference longitude and latitude and the target heartbeat keep-alive interval parameter can be sent to the server, and the server stores the corresponding relationship between the reference longitude and latitude and the target heartbeat keep-alive interval parameter. Also, a heartbeat keep-alive message is sent to the server based on the target heartbeat keep-alive interval parameter.
[0032] Exemplarily, after sending a heartbeat keep-alive interval parameter request message to the server, if a heartbeat keep-alive interval parameter failure response is received from the server, and the heartbeat keep-alive interval parameter failure response is sent when there is no target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude, then the detection heartbeat keep-alive interval parameter between the client and the server is detected, and the detection heartbeat keep-alive interval parameter is determined as the target heartbeat keep-alive interval parameter finally adopted by the client. Also, the reference longitude and latitude and the target heartbeat keep-alive interval parameter are sent to the server, and the server stores the corresponding relationship between the reference longitude and latitude and the target heartbeat keep-alive interval parameter. Also, a heartbeat keep-alive message is sent to the server based on the target heartbeat keep-alive interval parameter.
[0033] Exemplarily, the detection of the heartbeat keep-alive interval parameter between the detection client and the server may include, but is not limited to: sending a heartbeat keep-alive message to the server based on the configured initial value of the heartbeat keep-alive interval parameter. After sending the heartbeat keep-alive message to the server based on the initial value of the heartbeat keep-alive interval parameter, if a heartbeat keep-alive response is received, the initial value of the heartbeat keep-alive interval parameter is increased to obtain the intermediate value of the heartbeat keep-alive interval parameter, and a heartbeat keep-alive message is sent to the server based on the intermediate value of the heartbeat keep-alive interval parameter; if no heartbeat keep-alive response is received, the heartbeat keep-alive interval parameter used last time is determined as the detection heartbeat keep-alive interval parameter; if a heartbeat keep-alive response is received, the intermediate value of the heartbeat keep-alive interval parameter is continued to be increased to obtain the intermediate value of the heartbeat keep-alive interval parameter, and the operation of sending a heartbeat keep-alive message to the server based on the intermediate value of the heartbeat keep-alive interval parameter is returned. Alternatively, after sending a heartbeat keep-alive message to the server based on the initial value of the heartbeat keep-alive interval parameter, if no heartbeat keep-alive response is received, the initial value of the heartbeat keep-alive interval parameter is reduced to obtain an intermediate value of the heartbeat keep-alive interval parameter, and a heartbeat keep-alive message is sent to the server based on the intermediate value of the heartbeat keep-alive interval parameter; if a heartbeat keep-alive response is received, the currently used heartbeat keep-alive interval parameter is determined as the detection heartbeat keep-alive interval parameter; if no heartbeat keep-alive response is received, the intermediate value of the heartbeat keep-alive interval parameter continues to be reduced to obtain the intermediate value of the heartbeat keep-alive interval parameter, and the operation of sending a heartbeat keep-alive message to the server based on the intermediate value of the heartbeat keep-alive interval parameter is returned.
[0034] Exemplarily, after sending a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameter, if the current location of the client changes, and the distance between the changed longitude and latitude corresponding to the changed location and the reference longitude and latitude is greater than a distance threshold, a heartbeat keep-alive interval parameter request message is re-sent to the server, the heartbeat keep-alive interval parameter request message including the changed longitude and latitude; a heartbeat keep-alive interval parameter response message returned by the server is received, the heartbeat keep-alive interval parameter response message including candidate heartbeat keep-alive interval parameters, the candidate heartbeat keep-alive interval parameters being the heartbeat keep-alive interval parameters corresponding to the target longitude and latitude whose changed longitude and latitude satisfy the distance constraint; and a heartbeat keep-alive message is sent to the server based on the candidate heartbeat keep-alive interval parameters.
[0035] Exemplarily, after sending a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameters, if an SMS (Short Messaging Service) message sent by the server is received, and the SMS message is used to indicate that the TCP long connection between the client and the server has been disconnected, a TCP long connection establishment request message is sent to the server to establish a TCP long connection through the TCP long connection establishment request message.
[0036] As can be seen from the above technical solutions, in the embodiment of the present application, the corresponding relationship between longitude and latitude and heartbeat keep-alive interval parameters (such as interval duration) can be maintained, and the target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude can be found from all longitudes and latitudes based on the reference longitude and latitude corresponding to the current location of the client, and then the heartbeat keep-alive interval parameter corresponding to the target longitude and latitude is used as the candidate heartbeat keep-alive interval parameter of the client, and the client sends a heartbeat keep-alive message to the server based on this candidate heartbeat keep-alive interval parameter. The candidate heartbeat keep-alive interval parameter is the interval duration that best matches the current location of the client, so as to dynamically configure the matching interval duration for the client. It can avoid the problem of sending a large number of heartbeat keep-alive messages due to too short interval duration, reduce the number of times that battery-type devices (i.e., clients) are awakened to communicate, and increase the battery life of the device. In addition, it can avoid the problem of abnormal deletion of TCP long connections due to too long interval durations, and enable the server and the client to communicate normally with each other, ensuring the stability of TCP long connections.
[0037] The above technical solutions of the embodiments of the present application are described below in combination with specific application scenarios.
[0038] In the client-server communication model, after the client and the server establish a TCP persistent connection, a heartbeat keepalive message is needed to maintain the TCP persistent connection between the client and the server during the TCP persistent connection. For example, the interval duration of the heartbeat keepalive message is configured based on experience. The interval duration indicates the interval duration between two adjacent heartbeat keepalive messages, and the heartbeat keepalive message is periodically sent based on the interval duration.
[0039] If the interval is set to a relatively short time, a large number of heartbeat keepalive messages will be sent, wasting a lot of bandwidth resources and increasing device power consumption. For battery devices, each time a heartbeat keepalive message is sent, the client needs to be awakened, a heartbeat keepalive message is sent through the client, and then the client is dormant. When the heartbeat keepalive message is sent next time, the client needs to be awakened again. Obviously, if the interval is set to a relatively short time, the client needs to be awakened repeatedly in a short period of time, which increases the power consumption of battery devices.
[0040] If the interval is set to a large value, the TCP long connection will be abnormally deleted, and data packets cannot be transmitted between the client and the server, resulting in communication abnormality. For example, when the client and the server establish a TCP long connection, the TCP long connection will pass through multiple network devices, such as base stations, routers, etc. The network device will configure the no-data communication link removal time, that is, when the data communication link does not transmit data within the removal time, the network device will delete the data communication link, thereby improving resource utilization. Based on this, when the interval of the heartbeat keep-alive message is greater than the no-data communication link removal time, that is, the client does not transmit the heartbeat keep-alive message within the removal time, the network device will delete the TCP long connection, resulting in the TCP long connection being abnormally deleted, and then causing communication abnormality. Since the no-data communication link removal time of different network devices may be different, such as 5 minutes, 10 minutes, 30 minutes, etc., it is also impossible to set the interval of the heartbeat keep-alive message based on the no-data communication link removal time.
[0041] In response to the above findings, a heartbeat keep-alive method is proposed in an embodiment of the present application. Based on the reference longitude and latitude corresponding to the current location of the client, a heartbeat keep-alive interval parameter (i.e., the interval duration of the heartbeat keep-alive message) is set for the client, thereby dynamically configuring a matching interval duration for the client. This can avoid the problem of sending a large number of heartbeat keep-alive messages due to an interval duration that is too short, reduce the number of times battery-type devices (i.e., the client) are awakened to communicate, and avoid the problem of abnormal deletion of TCP long connections due to an interval duration that is too long, so that the server and the client can communicate with each other normally, thereby ensuring the stability of the TCP long connection.
[0042] See also Figure 2 FIG. 4 is a flow chart of a heartbeat keep-alive method, which includes:
[0043] Step 201: The client sends a heartbeat keep-alive interval parameter request message to the server. The heartbeat keep-alive interval parameter request message may include a reference longitude and latitude corresponding to the current location of the client.
[0044] Exemplarily, after a TCP long connection is established between the client and the server, there is no restriction on the establishment method of the TCP long connection, and the client can obtain the longitude and latitude of the current location of the client, which is used to indicate the current location of the client. For the convenience of distinction, the longitude and latitude are referred to as reference longitude and latitude. Then, the client can send a heartbeat keep-alive interval parameter request message to the server, and the heartbeat keep-alive interval parameter request message can include the reference longitude and latitude corresponding to the current location of the client.
[0045] Step 202: After receiving the heartbeat keep-alive interval parameter request message, the server determines whether there is a target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude. In this embodiment, the existence of a target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude is taken as an example, and step 203 is executed.
[0046] Exemplarily, after receiving the heartbeat keep-alive interval parameter request message, the server learns that the heartbeat keep-alive interval parameter request message is used to request the heartbeat keep-alive interval parameter, and therefore parses the reference longitude and latitude from the heartbeat keep-alive interval parameter request message, and determines whether there is a target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude. The distance constraint condition may include: the distance between the target longitude and latitude and the reference longitude and latitude is less than a preset threshold. Of course, the above is only an example of a distance constraint condition, and there is no limitation to this.
[0047] Among them, the preset threshold can be configured based on experience, or it can be determined based on the coverage range of the network device (i.e., the network device through which the TCP long connection passes) (when the TCP long connection passes through multiple network devices, this coverage range can be the minimum coverage range of all network devices). For example, the preset threshold can be equal to the coverage range, or it can be less than the coverage range. For example, when the coverage range of the network device is 500 meters, the preset threshold can be 500 meters or less than 500 meters, and there is no restriction on this preset threshold.
[0048] Exemplarily, the server may maintain a mapping table, such as the server may include a database, and the mapping table is stored in the database. The mapping table may include the correspondence between longitude and latitude and heartbeat keep-alive interval parameters. See Table 1, which is an example of the mapping table. Of course, in addition to longitude and latitude and heartbeat keep-alive interval parameters, the mapping table may also include other content, which is not limited to this. Table 1 is used as an example for explanation below.
[0049] Table 1
[0050] Latitude and longitude Heartbeat keepalive interval parameter Latitude and longitude A1 Heartbeat keep-alive interval parameter B1 Latitude and longitude A2 Heartbeat keep-alive interval parameter B2 Latitude and longitude A3 Heartbeat keep-alive interval parameter B3 ... ...
[0051] After the server parses the reference longitude and latitude from the heartbeat keepalive interval parameter request message, it calculates the distance between the reference longitude and latitude and each longitude and latitude in the mapping table, such as the distance between the reference longitude and latitude and longitude A1, the distance between the reference longitude and latitude and longitude A2, and the distance between the reference longitude and latitude and longitude A3.
[0052] In a possible implementation, based on the distance between the reference longitude and latitude and each longitude and latitude in the mapping table, the minimum distance is selected from all distances. If the minimum distance is less than a preset threshold, the longitude and latitude corresponding to the minimum distance is used as the target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude. For example, when the distance between the reference longitude and latitude and longitude and longitude A3 is the minimum, the target longitude and latitude is longitude and latitude A3. If the minimum distance is not less than the preset threshold, there is no target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude.
[0053] In another possible implementation, based on the distance between the reference longitude and latitude and each longitude and latitude in the mapping table, if there is a distance less than a preset threshold among all the distances, a distance can be randomly selected from the distances less than the preset threshold, and the longitude and latitude corresponding to the randomly selected distance is used as the target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude. Alternatively, if there is no distance less than the preset threshold among all the distances, there is no target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude.
[0054] In summary, the server can determine the target longitude and latitude that meet the distance constraint condition with the reference longitude and latitude, and the distance between the target longitude and latitude and the reference longitude and latitude is less than a preset threshold.
[0055] Step 203: The server queries the candidate heartbeat keep-alive interval parameters corresponding to the target longitude and latitude from the mapping table, and sends a heartbeat keep-alive interval parameter response message to the client. The client receives the heartbeat keep-alive interval parameter response message returned by the server, and the heartbeat keep-alive interval parameter response message includes the candidate heartbeat keep-alive interval parameters.
[0056] Exemplarily, after obtaining the target longitude and latitude, the server can query the mapping table shown in Table 1 through the target longitude and latitude to obtain the heartbeat keep-alive interval parameters corresponding to the target longitude and latitude. The heartbeat keep-alive interval parameters corresponding to the target longitude and latitude can be recorded as candidate heartbeat keep-alive interval parameters. Taking the target longitude and latitude as longitude and latitude A3 as an example, the candidate heartbeat keep-alive interval parameter can be the heartbeat keep-alive interval parameter B3.
[0057] The server may send a heartbeat keep-alive interval parameter response message to the client in response to the heartbeat keep-alive interval parameter request message, and the client may receive the heartbeat keep-alive interval parameter response message, and the heartbeat keep-alive interval parameter response message may include candidate heartbeat keep-alive interval parameters corresponding to the target longitude and latitude.
[0058] Exemplarily, the heartbeat keep-alive interval parameter request message can be transmitted via a TCP long connection or in other ways, without limitation. The heartbeat keep-alive interval parameter response message can be transmitted via a TCP long connection or in other ways, without limitation.
[0059] Step 204: The client sends a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter; wherein the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
[0060] Exemplarily, the candidate heartbeat keep-alive interval parameter can be the interval duration of the heartbeat keep-alive message, which is used to indicate the interval duration between two adjacent heartbeat keep-alive messages. Based on the duration corresponding to the candidate heartbeat keep-alive interval parameter, the client sends a heartbeat keep-alive message to the server once every this duration, that is, sends the heartbeat keep-alive message through the TCP long connection, thereby avoiding the network device through which the TCP long connection passes from deleting the TCP long connection, that is, after the network device receives the heartbeat keep-alive message, it will maintain the TCP long connection.
[0061] Step 205: After the client sends the heartbeat keep-alive message to the server, it is determined whether a heartbeat keep-alive response returned by the server is received, that is, whether a heartbeat keep-alive response returned by the server is received within a preset time period.
[0062] Exemplarily, the client starts timing after sending a heartbeat keep-alive message to the server, and determines whether a heartbeat keep-alive response returned by the server is received within a preset time period, and the preset time period can be configured based on experience.
[0063] Exemplarily, if the client receives a heartbeat keepalive response returned by the server to the heartbeat keepalive message, it is determined that the heartbeat keepalive is successful and step 206 can be executed. If the client does not receive a heartbeat keepalive response returned by the server to the heartbeat keepalive message, it is determined that the heartbeat keepalive fails and step 208 can be executed.
[0064] Exemplarily, the client determining that the heartbeat keep-alive is successful may include: if the client sends K heartbeat keep-alive messages continuously based on the candidate heartbeat keep-alive interval parameter, K is a positive integer, such as 1, 3, 5, etc., and receives heartbeat keep-alive responses respectively returned by the server for the K heartbeat keep-alive messages, that is, the client receives K heartbeat keep-alive responses, and then determines that the heartbeat keep-alive is successful. The client determining that the heartbeat keep-alive fails may include: if the client sends K heartbeat keep-alive messages continuously based on the candidate heartbeat keep-alive interval parameter, and does not receive a heartbeat keep-alive response returned by the server for each heartbeat keep-alive message, then determines that the heartbeat keep-alive fails.
[0065] Step 206: The client determines the candidate heartbeat keep-alive interval parameter as the target heartbeat keep-alive interval parameter to be finally adopted, and sends a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameter.
[0066] Exemplarily, if the heartbeat keep-alive is determined to be successful based on the candidate heartbeat keep-alive interval parameter, the candidate heartbeat keep-alive interval parameter can be determined as the target heartbeat keep-alive interval parameter finally adopted. The target heartbeat keep-alive interval parameter can be the interval duration of the heartbeat keep-alive message, which is used to indicate the interval duration between two adjacent heartbeat keep-alive messages. Based on the duration corresponding to the target heartbeat keep-alive interval parameter, the client sends a heartbeat keep-alive message to the server once every this duration, and there is no restriction on the transmission of this heartbeat keep-alive message.
[0067] Step 207: The client sends the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and the server stores the corresponding relationship between the reference longitude and latitude and the target heartbeat keep-alive interval parameter.
[0068] Exemplarily, after obtaining the target heartbeat keep-alive interval parameter (i.e., the candidate heartbeat keep-alive interval parameter, such as heartbeat keep-alive interval parameter B3), the client can send the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and the server stores the correspondence between the reference longitude and latitude and the target heartbeat keep-alive interval parameter in a mapping table, as shown in Table 2, which is an example of a mapping table.
[0069] Table 2
[0070] Latitude and longitude Heartbeat keepalive interval parameter Latitude and longitude A1 Heartbeat keep-alive interval parameter B1 Latitude and longitude A2 Heartbeat keep-alive interval parameter B2 Latitude and longitude A3 Heartbeat keep-alive interval parameter B3 Latitude and longitude A4 Heartbeat keep-alive interval parameter B3 ... ...
[0071] Step 208: The client detects the heartbeat keep-alive interval parameter between the client and the server.
[0072] Exemplarily, since the heartbeat keep-alive of the candidate heartbeat keep-alive interval parameter fails, that is, the candidate heartbeat keep-alive interval parameter cannot be applied to the client, it is necessary to detect the detection heartbeat keep-alive interval parameter between the client and the server, and the detection heartbeat keep-alive interval parameter is a heartbeat keep-alive interval parameter that can make the heartbeat keep-alive successful, and there is no restriction on the detection heartbeat keep-alive interval parameter. For example, the detection heartbeat keep-alive interval parameter can be the maximum heartbeat keep-alive interval parameter that can make the heartbeat keep-alive successful.
[0073] For example, the client obtains multiple heartbeat keepalive interval parameters that can make the heartbeat keepalive successful, that is, when the client adopts these heartbeat keepalive interval parameters, it is determined that the heartbeat keepalive is successful. Then, the client can select the maximum heartbeat keepalive interval parameter from these heartbeat keepalive interval parameters, and use the maximum heartbeat keepalive interval parameter as the detection heartbeat keepalive interval parameter between the client and the server.
[0074] Step 209: The client determines the detection heartbeat keep-alive interval parameter as the target heartbeat keep-alive interval parameter to be finally adopted, and sends a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameter.
[0075] Exemplarily, based on the detection of the heartbeat keep-alive interval parameter, it can be determined that the heartbeat keep-alive is successful. The detection of the heartbeat keep-alive interval parameter can be determined as the target heartbeat keep-alive interval parameter finally adopted. Based on the duration corresponding to the target heartbeat keep-alive interval parameter, the client sends a heartbeat keep-alive message to the server every this duration.
[0076] Step 210: The client sends the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and the server stores the corresponding relationship between the reference longitude and latitude and the target heartbeat keep-alive interval parameter.
[0077] Exemplarily, after obtaining the target heartbeat keep-alive interval parameter (i.e., the detection heartbeat keep-alive interval parameter, such as heartbeat keep-alive interval parameter B4), the client can send the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and the server stores the correspondence between the reference longitude and latitude and the target heartbeat keep-alive interval parameter in a mapping table, as shown in Table 3, which is an example of a mapping table.
[0078] Table 3
[0079] Latitude and longitude Heartbeat keepalive interval parameter Latitude and longitude A1 Heartbeat keep-alive interval parameter B1 Latitude and longitude A2 Heartbeat keep-alive interval parameter B2 Latitude and longitude A3 Heartbeat keep-alive interval parameter B3 Latitude and longitude A4 Heartbeat keep-alive interval parameter B4 ... ...
[0080] At this point, based on steps 201 to 210, the heartbeat keep-alive method can be completed.
[0081] See also Figure 3 FIG. 4 is a flow chart of a heartbeat keep-alive method, which includes:
[0082] Step 301: The client sends a heartbeat keep-alive interval parameter request message to the server. The heartbeat keep-alive interval parameter request message may include a reference longitude and latitude corresponding to the client's current location.
[0083] Step 302: After receiving the heartbeat keep-alive interval parameter request message, the server determines whether there is a target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude. In this embodiment, it is taken that there is no target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude as an example, and step 303 is executed.
[0084] Exemplarily, the server can parse the reference longitude and latitude from the heartbeat keep-alive interval parameter request message and determine whether there is a target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude. The distance constraint condition may include: the distance between the target longitude and latitude and the reference longitude and latitude is less than a preset threshold.
[0085] In a possible implementation, based on the distance between the reference longitude and latitude and each longitude and latitude in the mapping table, the minimum distance is selected from all distances, and if the minimum distance is not less than a preset threshold, the server determines that there is no target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude. Alternatively, based on the distance between the reference longitude and latitude and each longitude and latitude in the mapping table, if there is no distance less than a preset threshold among all distances, the server determines that there is no target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude.
[0086] Step 303: The server sends a heartbeat keep-alive interval parameter failure response to the client, and the client receives the heartbeat keep-alive interval parameter failure response returned by the server in response to the heartbeat keep-alive interval parameter request message.
[0087] Exemplarily, if there is no target longitude and latitude that meets the distance constraint with the reference longitude and latitude, the server may send a heartbeat keepalive interval parameter failure response to the client, where the heartbeat keepalive interval parameter failure response is used to indicate that there is no candidate heartbeat keepalive interval parameter that matches the client.
[0088] Exemplarily, the heartbeat keep-alive interval parameter request message can be transmitted via a TCP long connection or in other ways, without limitation. The heartbeat keep-alive interval parameter failure response can be transmitted via a TCP long connection or in other ways, without limitation.
[0089] Step 304: The client detects the heartbeat keep-alive interval parameter between the client and the server.
[0090] Exemplarily, since the heartbeat keep-alive interval parameter failure response is used to indicate that there is no candidate heartbeat keep-alive interval parameter matching the client, the client needs to detect the detection heartbeat keep-alive interval parameter between the client and the server, and the detection heartbeat keep-alive interval parameter is a heartbeat keep-alive interval parameter that can make the heartbeat keep-alive successful, and there is no restriction on the detection heartbeat keep-alive interval parameter. For example, the detection heartbeat keep-alive interval parameter can be the maximum heartbeat keep-alive interval parameter that can make the heartbeat keep-alive successful.
[0091] For example, the client obtains multiple heartbeat keepalive interval parameters that can make the heartbeat keepalive successful. The client can select the maximum heartbeat keepalive interval parameter from these heartbeat keepalive interval parameters and use the maximum heartbeat keepalive interval parameter as the detection heartbeat keepalive interval parameter between the client and the server.
[0092] Step 305: The client determines the detection heartbeat keep-alive interval parameter as the target heartbeat keep-alive interval parameter to be finally adopted, and sends a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameter.
[0093] Exemplarily, based on the detection of the heartbeat keep-alive interval parameter, it can be determined that the heartbeat keep-alive is successful. The detection of the heartbeat keep-alive interval parameter can be determined as the target heartbeat keep-alive interval parameter finally adopted. Based on the duration corresponding to the target heartbeat keep-alive interval parameter, the client sends a heartbeat keep-alive message to the server every this duration.
[0094] Step 306: The client sends the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and the server stores the corresponding relationship between the reference longitude and latitude and the target heartbeat keep-alive interval parameter.
[0095] Exemplarily, after obtaining the target heartbeat keep-alive interval parameter (i.e., the detection heartbeat keep-alive interval parameter), the client can send the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and the server stores the correspondence between the reference longitude and latitude and the target heartbeat keep-alive interval parameter in a mapping table.
[0096] At this point, based on step 301 to step 306, the heartbeat keep-alive method is completed.
[0097] In one possible implementation, after sending a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameter, that is, after the target heartbeat keep-alive interval parameter has been determined, if the current location of the client changes, and the distance between the changed longitude and latitude corresponding to the changed location and the reference longitude and latitude (that is, the longitude and latitude corresponding to the location before the change) is greater than the distance threshold, the client re-sends a heartbeat keep-alive interval parameter request message to the server, and the heartbeat keep-alive interval parameter request message includes the changed longitude and latitude.
[0098] Exemplarily, the distance threshold can be configured based on experience, or it can be determined based on the coverage range of the network device (i.e., the network device through which the TCP long connection passes). For example, the distance threshold can be equal to the coverage range, or it can be smaller than the coverage range, and there is no restriction on this distance threshold.
[0099] Exemplarily, after receiving the heartbeat keep-alive interval parameter request message, the server determines whether there is a target longitude and latitude that meets the distance constraint condition with the changed longitude and latitude. If there is a target longitude and latitude that meets the distance constraint condition with the changed longitude and latitude, the server obtains the candidate heartbeat keep-alive interval parameter corresponding to the target longitude and latitude, and sends a heartbeat keep-alive interval parameter response message to the client. The client receives the heartbeat keep-alive interval parameter response message returned by the server, and the heartbeat keep-alive interval parameter response message includes the candidate heartbeat keep-alive interval parameter. Then, the client sends a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter. This process can be seen in Figure 2 The process shown will not be repeated here.
[0100] If there is no target longitude and latitude that meets the distance constraint condition with the changed longitude and latitude, the server sends a heartbeat keep-alive interval parameter failure response to the client. The client receives the heartbeat keep-alive interval parameter failure response returned by the server in response to the heartbeat keep-alive interval parameter request message, and detects the detection heartbeat keep-alive interval parameter between the client and the server, and determines the detection heartbeat keep-alive interval parameter as the target heartbeat keep-alive interval parameter finally adopted. This process can be seen in Figure 3 The process shown will not be repeated here.
[0101] With respect to step 208 and step 304, the client may detect a detection heartbeat keep-alive interval parameter between the client and the server, the detection heartbeat keep-alive interval parameter being a heartbeat keep-alive interval parameter that enables the heartbeat keep-alive to succeed, such as a maximum heartbeat keep-alive interval parameter that enables the heartbeat keep-alive to succeed. In order to detect the detection heartbeat keep-alive interval parameter between the client and the server, the following steps may be used:
[0102] Step S11: The client obtains the configured initial value of the heartbeat keep-alive interval parameter.
[0103] Exemplarily, the initial value of the heartbeat keep-alive interval parameter can be configured based on experience, and there is no limitation on this. For example, the initial value of the heartbeat keep-alive interval parameter is X minutes. For example, the duration of the removal of the no-data communication link of the network device can be 5 minutes, 10 minutes, 30 minutes, etc., and the duration of the removal of the no-data communication link is 10 minutes as an example, then the initial value of the heartbeat keep-alive interval parameter can be less than 10 minutes, such as 5 minutes.
[0104] Step S12: The client sends a heartbeat keep-alive message to the server based on the initial value of the heartbeat keep-alive interval parameter.
[0105] Exemplarily, the initial value of the heartbeat keepalive interval parameter is used to indicate the interval duration between two adjacent heartbeat keepalive messages. Based on the duration corresponding to the initial value of the heartbeat keepalive interval parameter, the client sends a heartbeat keepalive message to the server after the duration, i.e., sends the heartbeat keepalive message through a TCP long connection.
[0106] Step S13: After the client sends the heartbeat keep-alive message to the server, it is determined whether a heartbeat keep-alive response returned by the server is received, that is, whether a heartbeat keep-alive response returned by the server is received within a preset time period.
[0107] Exemplarily, the client starts timing after sending a heartbeat keep-alive message to the server, and determines whether a heartbeat keep-alive response returned by the server is received within a preset time period, and the preset time period is configured based on experience. If the client receives a heartbeat keep-alive response returned by the server to the heartbeat keep-alive message, step S21 is executed, and if the client does not receive a heartbeat keep-alive response returned by the server to the heartbeat keep-alive message, step S31 is executed.
[0108] Step S21: If a heartbeat keep-alive response is received, the client increases the initial value of the heartbeat keep-alive interval parameter and uses the increased heartbeat keep-alive interval parameter as the intermediate value of the heartbeat keep-alive interval parameter.
[0109] Exemplarily, if a heartbeat keep-alive response is received, that is, the heartbeat keep-alive communication is successful, the initial value of the heartbeat keep-alive interval parameter is added with N seconds, where N is a custom time value, such as 5 seconds, 10 seconds, 15 seconds, etc., to obtain the middle value of the heartbeat keep-alive interval parameter, such as the middle value of the heartbeat keep-alive interval parameter is X minutes + N seconds.
[0110] Step S22: The client sends a heartbeat keep-alive message to the server based on the middle value of the heartbeat keep-alive interval parameter.
[0111] Exemplarily, based on the duration corresponding to the middle value of the heartbeat keep-alive interval parameter, the client sends a heartbeat keep-alive message to the server after the duration, that is, sends the heartbeat keep-alive message through a TCP long connection.
[0112] Step S23: After the client sends the heartbeat keep-alive message to the server, it is determined whether a heartbeat keep-alive response returned by the server is received, that is, whether a heartbeat keep-alive response returned by the server is received within a preset time period.
[0113] Exemplarily, if the client receives a heartbeat keep-alive response returned by the server, step S24 may be executed; if the client does not receive a heartbeat keep-alive response returned by the server, step S25 may be executed.
[0114] Step S24: If a heartbeat keep-alive response is received, the client continues to increase the middle value of the heartbeat keep-alive interval parameter, and uses the increased heartbeat keep-alive interval parameter as the middle value of the heartbeat keep-alive interval parameter, and returns to step S22, that is, sending a heartbeat keep-alive message to the server based on the middle value of the heartbeat keep-alive interval parameter.
[0115] Step S25: If no heartbeat keep-alive response is received, the client determines the heartbeat keep-alive interval parameter used last time as the detection heartbeat keep-alive interval parameter, wherein the heartbeat keep-alive interval parameter used last time may be the heartbeat keep-alive interval parameter initial value or the heartbeat keep-alive interval parameter intermediate value.
[0116] Exemplarily, after a heartbeat keep-alive message is sent to the server based on the intermediate value of the heartbeat keep-alive interval parameter, if no heartbeat keep-alive response is received, it means that the intermediate value of the heartbeat keep-alive interval parameter cannot guarantee the success of the heartbeat keep-alive, and since the heartbeat keep-alive response corresponding to the heartbeat keep-alive interval parameter used last time has been received, that is, the heartbeat keep-alive interval parameter used last time can guarantee the success of the heartbeat keep-alive, therefore, the heartbeat keep-alive interval parameter used last time is the maximum heartbeat keep-alive interval parameter that can make the heartbeat keep-alive successful, and the heartbeat keep-alive interval parameter used last time can be determined as the detection heartbeat keep-alive interval parameter.
[0117] For example, the client sends 4 heartbeat keepalive messages in succession. The middle value of the heartbeat keepalive interval parameter between the first heartbeat keepalive message and the previous heartbeat keepalive message is X minutes + N seconds, and the heartbeat keepalive response corresponding to the first heartbeat keepalive message is received. The middle value of the heartbeat keepalive interval parameter between the second heartbeat keepalive message and the first heartbeat keepalive message is X minutes + 2N seconds, and the heartbeat keepalive response corresponding to the second heartbeat keepalive message is received. The middle value of the heartbeat keepalive interval parameter between the third heartbeat keepalive message and the second heartbeat keepalive message is X minutes + 3N seconds, and the heartbeat keepalive response corresponding to the third heartbeat keepalive message is received. The middle value of the heartbeat keepalive interval parameter between the fourth heartbeat keepalive message and the third heartbeat keepalive message is X minutes + 4N seconds, but the heartbeat keepalive response corresponding to the fourth heartbeat keepalive message is not received. Based on this, the heartbeat keepalive interval parameter (X minutes + 3N seconds) used last time is used as the detection heartbeat keepalive interval parameter.
[0118] Exemplarily, before determining the heartbeat keep-alive interval parameter (referred to as the heartbeat keep-alive interval parameter A) used last time as the detection heartbeat keep-alive interval parameter, the client can also determine whether the heartbeat keep-alive interval parameter A can guarantee the success of the heartbeat keep-alive. If so, the heartbeat keep-alive interval parameter A can be used as the detection heartbeat keep-alive interval parameter. If not, it can be determined whether the last heartbeat keep-alive interval parameter of the heartbeat keep-alive interval parameter A can guarantee the success of the heartbeat keep-alive. If so, the last heartbeat keep-alive interval parameter of the heartbeat keep-alive interval parameter A can be used as the detection heartbeat keep-alive interval parameter, and so on.
[0119] If the client sends K heartbeat keepalive messages continuously based on the heartbeat keepalive interval parameter A, where K is a positive integer such as 1, 3, 5, 10, etc., and receives heartbeat keepalive responses returned by the server for the K heartbeat keepalive messages, that is, receives K heartbeat keepalive responses, it is determined that the heartbeat keepalive interval parameter A can ensure the success of the heartbeat keepalive, and the heartbeat keepalive interval parameter A is used as the detection heartbeat keepalive interval parameter.
[0120] Step S31: If no heartbeat keep-alive response is received, the client reduces the initial value of the heartbeat keep-alive interval parameter and uses the reduced heartbeat keep-alive interval parameter as the intermediate value of the heartbeat keep-alive interval parameter.
[0121] Exemplarily, if no heartbeat keep-alive response is received, that is, the heartbeat keep-alive communication fails, the initial value of the heartbeat keep-alive interval parameter is reduced by M seconds, where M is a custom time value, such as 5 seconds, 10 seconds, 15 seconds, etc., to obtain the middle value of the heartbeat keep-alive interval parameter, such as the middle value of the heartbeat keep-alive interval parameter is X minutes - M seconds.
[0122] Step S32: The client sends a heartbeat keep-alive message to the server based on the middle value of the heartbeat keep-alive interval parameter.
[0123] Step S33: After the client sends the heartbeat keep-alive message to the server, it is determined whether a heartbeat keep-alive response returned by the server is received, that is, whether a heartbeat keep-alive response returned by the server is received within a preset time period.
[0124] Exemplarily, if the client does not receive a heartbeat keep-alive response returned by the server, step S34 may be executed; if the client receives a heartbeat keep-alive response returned by the server, step S35 may be executed.
[0125] Step S34: If no heartbeat keep-alive response is received, the client continues to reduce the intermediate value of the heartbeat keep-alive interval parameter, and uses the reduced heartbeat keep-alive interval parameter as the intermediate value of the heartbeat keep-alive interval parameter, and returns to step S32, that is, sending a heartbeat keep-alive message to the server based on the intermediate value of the heartbeat keep-alive interval parameter.
[0126] Step S35, if a heartbeat keep-alive response is received, the client determines the currently used heartbeat keep-alive interval parameter intermediate value as the detection heartbeat keep-alive interval parameter. For example, after sending a heartbeat keep-alive message to the server based on the heartbeat keep-alive interval parameter intermediate value, if a heartbeat keep-alive response is received, it means that the heartbeat keep-alive interval parameter intermediate value can ensure the success of the heartbeat keep-alive, and because the heartbeat keep-alive interval parameter used last time cannot ensure the success of the heartbeat keep-alive (i.e., no heartbeat keep-alive response is received), therefore, the currently used heartbeat keep-alive interval parameter intermediate value is the maximum heartbeat keep-alive interval parameter that can make the heartbeat keep-alive successful, and the currently used heartbeat keep-alive interval parameter intermediate value can be determined as the detection heartbeat keep-alive interval parameter.
[0127] Exemplarily, before determining the currently used heartbeat keep-alive interval parameter (recorded as heartbeat keep-alive interval parameter B) as the detection heartbeat keep-alive interval parameter, the client can also determine whether the heartbeat keep-alive interval parameter B can ensure the success of the heartbeat keep-alive. If so, the heartbeat keep-alive interval parameter B can be used as the detection heartbeat keep-alive interval parameter. If not, it is determined whether the next heartbeat keep-alive interval parameter after the heartbeat keep-alive interval parameter B (i.e., the heartbeat keep-alive interval parameter B minus M seconds) can ensure the success of the heartbeat keep-alive, and so on.
[0128] If the client sends K heartbeat keepalive messages continuously based on the heartbeat keepalive interval parameter B, and receives heartbeat keepalive responses returned by the server for the K heartbeat keepalive messages, that is, receives K heartbeat keepalive responses, then the client determines that the heartbeat keepalive interval parameter B can ensure the success of the heartbeat keepalive.
[0129] At this point, the client has successfully detected the heartbeat keep-alive interval parameter between the client and the server.
[0130] In one possible implementation, see Figure 4 The following is the processing flow when the TCP persistent connection network is abnormal. When TCP communication is normal, the client and the server establish a TCP persistent connection, and the client and the server communicate normally based on the TCP persistent connection. There is no restriction on the communication process of this TCP persistent connection.
[0131] If the client finds an abnormal TCP communication state, such as when the client actively initiates data transmission with the server and finds that the TCP persistent connection has been interrupted, it is determined that the TCP communication is abnormal, and the client sends a TCP persistent connection establishment request message to the server to establish a TCP persistent connection through the TCP persistent connection establishment request message. There is no restriction on this TCP persistent connection establishment process. If the client re-initiates a TCP persistent connection, a new TCP persistent connection is established between the client and the server, and the client and the server communicate normally based on the new TCP persistent connection.
[0132] If the server finds an abnormal TCP communication state, such as when the server actively initiates data transmission with the client and finds that the TCP persistent connection has been interrupted, it determines that the TCP communication is abnormal, and the server sends an SMS message to the client, and the SMS message is used to indicate that the TCP persistent connection between the client and the server has been disconnected.
[0133] After the client receives the SMS message sent by the server, since the SMS message is used to indicate that the TCP persistent connection between the client and the server has been disconnected, it can be determined that the TCP persistent connection between the client and the server has been disconnected. Based on this, the client can resend a TCP persistent connection establishment request message to the server to establish a TCP persistent connection through the TCP persistent connection establishment request message. There is no restriction on this TCP persistent connection establishment process. For example, the client re-initiates a TCP persistent connection and establishes a new TCP persistent connection between the client and the server. The client and the server communicate normally based on the new TCP persistent connection.
[0134] Obviously, if the server finds that the TCP communication is abnormal, that is, the server and the client cannot communicate, the server can notify the client through SMS to quickly reconnect, thereby maintaining the stability of the communication link. When there is no operation, the battery-based client wakes up regularly based on the target heartbeat keep-alive interval parameter (such as 10 minutes), and after waking up, it communicates with the server by heartbeat (that is, sends heartbeat keep-alive messages and receives heartbeat keep-alive responses). If a communication abnormality occurs within the heartbeat keep-alive interval (such as 3 minutes), the server cannot communicate with the client through the TCP long connection, and the client can be quickly reconnected through SMS to achieve the purpose of communication.
[0135] As can be seen from the above technical solutions, in the embodiment of the present application, the heartbeat keep-alive interval is maintained by the server, and the heartbeat keep-alive interval is dynamically configured during the movement of the client, which can avoid the problem of sending a large number of heartbeat keep-alive messages due to the interval being too short, reduce the number of times the battery-type device (i.e., the client) is awakened to communicate, and increase the battery life of the device. The problem of abnormal deletion of TCP long connections due to excessive intervals can be avoided, and the server and the client can communicate normally with each other, ensuring the stability of the TCP long connection. When the network is congested or the switching of base stations causes abnormal communication, the server can quickly reconnect the network link through SMS, so that the server and the client can communicate normally with each other, ensuring the stability of the network connection.
[0136] Based on the same application concept as the above method, a heartbeat keep-alive system is proposed in an embodiment of the present application, including a client and a server, and a TCP long connection is established between the client and the server.
[0137] The client is used to send a heartbeat keep-alive interval parameter request message to the server, wherein the heartbeat keep-alive interval parameter request message includes a reference longitude and latitude corresponding to the current location of the client;
[0138] The server is used to determine a minimum distance based on the distance between the reference longitude and latitude and each longitude and latitude in a mapping table, wherein the mapping table includes a correspondence between longitude and latitude and a heartbeat keep-alive interval parameter; if the minimum distance is less than a preset threshold, the longitude and latitude corresponding to the minimum distance is determined as a target longitude and latitude that satisfies a distance constraint condition with the reference longitude and latitude, and a candidate heartbeat keep-alive interval parameter corresponding to the target longitude and latitude is queried from the mapping table, and a heartbeat keep-alive interval parameter response message is sent to the client, wherein the heartbeat keep-alive interval parameter response message includes the candidate heartbeat keep-alive interval parameter;
[0139] The client is used to send a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter after receiving the heartbeat keep-alive interval parameter response message; wherein the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
[0140] Exemplarily, the client is also used to, after sending a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter, determine that the heartbeat keep-alive is successful if a heartbeat keep-alive response returned by the server is received, and determine the candidate heartbeat keep-alive interval parameter as the target heartbeat keep-alive interval parameter ultimately adopted by the client; or, if no heartbeat keep-alive response returned by the server is received, determine that the heartbeat keep-alive has failed, and detect the detection heartbeat keep-alive interval parameter between the client and the server, and determine the detection heartbeat keep-alive interval parameter as the target heartbeat keep-alive interval parameter ultimately adopted by the client; send the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and the server stores the corresponding relationship between the reference longitude and latitude and the target heartbeat keep-alive interval parameter; and send a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameter.
[0141] Exemplarily, the client is also used to detect the detection heartbeat keep-alive interval parameters between the client and the server, and determine the detection heartbeat keep-alive interval parameters as the target heartbeat keep-alive interval parameters finally adopted by the client; send the reference longitude and latitude and the target heartbeat keep-alive interval parameters to the server, and the server stores the correspondence between the reference longitude and latitude and the target heartbeat keep-alive interval parameters; and send a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameters.
[0142] Based on the same application concept as the above method, a heartbeat keep-alive device is proposed in the embodiment of the present application. A TCP long connection is established between the client and the server. The device is applied to the client. Figure 5 FIG. 1 is a schematic diagram of the structure of the device, and the device may include:
[0143] A sending module 51 is used to send a heartbeat keep-alive interval parameter request message to the server, where the heartbeat keep-alive interval parameter request message includes a reference longitude and latitude corresponding to the current location of the client;
[0144] A receiving module 52 is used to receive a heartbeat keep-alive interval parameter response message returned by the server, wherein the heartbeat keep-alive interval parameter response message includes a candidate heartbeat keep-alive interval parameter, and the candidate heartbeat keep-alive interval parameter is a heartbeat keep-alive interval parameter corresponding to a target longitude and latitude that satisfies a distance constraint condition with a reference longitude and latitude;
[0145] The sending module 51 is further used to send a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter, and the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
[0146] Exemplarily, the server maintains a mapping table, which may include a correspondence between longitude and latitude and heartbeat keep-alive interval parameters; on this basis, based on the distance between the reference longitude and latitude and each longitude and latitude in the mapping table, if the minimum distance is less than a preset threshold, the longitude and latitude corresponding to the minimum distance may be the target longitude and latitude that meets the distance constraint with the reference longitude and latitude.
[0147] Exemplarily, the device also includes: a processing module, which is used to determine that the heartbeat keep-alive is successful if a heartbeat keep-alive response returned by the server is received after sending a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter, and determine the candidate heartbeat keep-alive interval parameter as the target heartbeat keep-alive interval parameter ultimately adopted by the client; or, if the heartbeat keep-alive response returned by the server is not received, determine that the heartbeat keep-alive fails, and detect the detection heartbeat keep-alive interval parameter between the client and the server, and determine the detection heartbeat keep-alive interval parameter as the target heartbeat keep-alive interval parameter ultimately adopted by the client; the sending module 51 is also used to send the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and the server stores the corresponding relationship between the reference longitude and latitude and the target heartbeat keep-alive interval parameter; the sending module 51 is also used to send a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameter.
[0148] Exemplarily, the processing module is used to detect the detection heartbeat keep-alive interval parameters between the client and the server, and determine the detection heartbeat keep-alive interval parameters as the target heartbeat keep-alive interval parameters finally adopted by the client, if a heartbeat keep-alive interval parameter failure response is received from the server after sending a heartbeat keep-alive interval parameter request message to the server, and the heartbeat keep-alive interval parameter failure response is sent when there is no target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude. The sending module 51 is also used to send the reference longitude and latitude and the target heartbeat keep-alive interval parameters to the server, and the server stores the correspondence between the reference longitude and latitude and the target heartbeat keep-alive interval parameters; and send a heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameters.
[0149] Exemplarily, when the processing module detects the detection heartbeat keep-alive interval parameter between the client and the server, it is specifically used to: send a heartbeat keep-alive message to the server based on the configured heartbeat keep-alive interval parameter initial value; if a heartbeat keep-alive response is received, the initial value of the heartbeat keep-alive interval parameter is increased to obtain an intermediate value of the heartbeat keep-alive interval parameter, and a heartbeat keep-alive message is sent to the server based on the intermediate value of the heartbeat keep-alive interval parameter; if no heartbeat keep-alive response is received, the heartbeat keep-alive interval parameter used last time is determined as the detection heartbeat keep-alive interval parameter; if a heartbeat keep-alive response is received, the intermediate value of the heartbeat keep-alive interval parameter is continued to be increased to obtain an intermediate value of the heartbeat keep-alive interval parameter value, returns to execute the operation of sending a heartbeat keep-alive message to the server based on the intermediate value of the heartbeat keep-alive interval parameter; if no heartbeat keep-alive response is received, the initial value of the heartbeat keep-alive interval parameter is reduced to obtain the intermediate value of the heartbeat keep-alive interval parameter, and a heartbeat keep-alive message is sent to the server based on the intermediate value of the heartbeat keep-alive interval parameter; if a heartbeat keep-alive response is received, the currently used heartbeat keep-alive interval parameter is determined as the detection heartbeat keep-alive interval parameter; if no heartbeat keep-alive response is received, the intermediate value of the heartbeat keep-alive interval parameter is continued to be reduced to obtain the intermediate value of the heartbeat keep-alive interval parameter, and returns to execute the operation of sending a heartbeat keep-alive message to the server based on the intermediate value of the heartbeat keep-alive interval parameter.
[0150] Exemplarily, the sending module 51 is also used to re-send a heartbeat keep-alive interval parameter request message to the server if the current location of the client changes and the distance between the changed longitude and latitude corresponding to the changed location and the reference longitude and latitude is greater than a distance threshold, and the heartbeat keep-alive interval parameter request message includes the changed longitude and latitude; the receiving module 52 is also used to receive a heartbeat keep-alive interval parameter response message returned by the server, and the heartbeat keep-alive interval parameter response message includes a candidate heartbeat keep-alive interval parameter, and the candidate heartbeat keep-alive interval parameter is the heartbeat keep-alive interval parameter corresponding to the target longitude and latitude that meets the distance constraint condition with the changed longitude and latitude; the sending module 51 is also used to send a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter.
[0151] Exemplarily, the receiving module 52 is also used to receive an SMS message sent by the server; the sending module 51 is also used to send a TCP long connection establishment request message to the server if the SMS message is received and the SMS message is used to indicate that the TCP long connection between the client and the server has been disconnected, so as to establish a TCP long connection through the TCP long connection establishment request message.
[0152] Based on the same application concept as the above method, a client device is proposed in the embodiment of the present application, see Figure 6 As shown, the client device includes: a processor 61 and a machine-readable storage medium 62, the machine-readable storage medium 62 stores machine-executable instructions that can be executed by the processor 61; the processor 61 is used to execute the machine-executable instructions to implement the heartbeat keep-alive method disclosed in the above example of this application.
[0153] Based on the same application concept as the above method, an embodiment of the present application also provides a machine-readable storage medium, on which a number of computer instructions are stored. When the computer instructions are executed by a processor, the heartbeat keep-alive method disclosed in the above example of the present application can be implemented.
[0154] The above-mentioned machine-readable storage medium may be any electronic, magnetic, optical or other physical storage device, which may contain or store information, such as executable instructions, data, etc. For example, the machine-readable storage medium may be: RAM (Radom Access Memory), volatile memory, non-volatile memory, flash memory, storage drive (such as hard disk drive), solid state drive, any type of storage disk (such as CD, DVD, etc.), or similar storage medium, or a combination thereof.
[0155] The systems, devices, modules or units described in the above embodiments may be implemented by computers or entities, or by products with certain functions. A typical implementation device is a computer, which may be in the form of a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email transceiver, a game console, a tablet computer, a wearable device or a combination of any of these devices.
[0156] For the convenience of description, the above device is described in various units according to their functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0157] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the embodiments of the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0158] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0159] Moreover, these computer program instructions can also be stored in a computer-readable memory that can guide a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory produce a product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.
[0160] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0161] The above is only an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.
Claims
1. A heartbeat keep-alive method, characterized in that: A TCP long connection is established between a client and a server, and the method is applied to the client, and the method includes: Sending a heartbeat keep-alive interval parameter request message to the server, wherein the heartbeat keep-alive interval parameter request message includes a reference longitude and latitude corresponding to the current location of the client; Receive a heartbeat keep-alive interval parameter response message returned by the server, wherein the heartbeat keep-alive interval parameter response message includes a candidate heartbeat keep-alive interval parameter, and the candidate heartbeat keep-alive interval parameter is a heartbeat keep-alive interval parameter corresponding to a target longitude and latitude that satisfies a distance constraint condition with the reference longitude and latitude; A heartbeat keep-alive message is sent to the server based on the candidate heartbeat keep-alive interval parameter, where the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
2. The method according to claim 1, characterized in that The server maintains a mapping table, which includes the correspondence between longitude and latitude and heartbeat keep-alive interval parameters; based on the distance between the reference longitude and latitude and each longitude and latitude in the mapping table, if the minimum distance is less than a preset threshold, the longitude and latitude corresponding to the minimum distance is the target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude.
3. The method according to claim 1, characterized in that After sending the heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter, the method further includes: If a heartbeat keep-alive response returned by the server is received, it is determined that the heartbeat keep-alive is successful, and the candidate heartbeat keep-alive interval parameter is determined as the target heartbeat keep-alive interval parameter finally adopted by the client; or, if the heartbeat keep-alive response returned by the server is not received, it is determined that the heartbeat keep-alive fails, and the detection heartbeat keep-alive interval parameter between the client and the server is detected, and the detection heartbeat keep-alive interval parameter is determined as the target heartbeat keep-alive interval parameter finally adopted by the client; Sending the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and storing the corresponding relationship between the reference longitude and latitude and the target heartbeat keep-alive interval parameter by the server; A heartbeat keep-alive message is sent to the server based on the target heartbeat keep-alive interval parameter.
4. The method according to claim 1, characterized in that: After sending the heartbeat keep-alive interval parameter request message to the server, the method further includes: If a heartbeat keep-alive interval parameter failure response returned by the server is received, and the heartbeat keep-alive interval parameter failure response is sent when there is no target longitude and latitude that meets the distance constraint condition with the reference longitude and latitude, then the detection heartbeat keep-alive interval parameter between the client and the server is detected, and the detection heartbeat keep-alive interval parameter is determined as the target heartbeat keep-alive interval parameter finally adopted by the client; Sending the reference longitude and latitude and the target heartbeat keep-alive interval parameter to the server, and storing the corresponding relationship between the reference longitude and latitude and the target heartbeat keep-alive interval parameter by the server; A heartbeat keep-alive message is sent to the server based on the target heartbeat keep-alive interval parameter.
5. The method according to claim 3 or 4, characterized in that: The detecting of the heartbeat keep-alive interval parameter between the client and the server includes: Sending a heartbeat keep-alive message to the server based on the configured initial value of the heartbeat keep-alive interval parameter; If a heartbeat keep-alive response is received, the initial value of the heartbeat keep-alive interval parameter is increased to obtain the intermediate value of the heartbeat keep-alive interval parameter, and a heartbeat keep-alive message is sent to the server based on the intermediate value of the heartbeat keep-alive interval parameter; if no heartbeat keep-alive response is received, the heartbeat keep-alive interval parameter used last time is determined as the detection heartbeat keep-alive interval parameter; if a heartbeat keep-alive response is received, the intermediate value of the heartbeat keep-alive interval parameter is continued to be increased to obtain the intermediate value of the heartbeat keep-alive interval parameter, and the operation of sending a heartbeat keep-alive message to the server based on the intermediate value of the heartbeat keep-alive interval parameter is returned; If no heartbeat keep-alive response is received, the initial value of the heartbeat keep-alive interval parameter is reduced to obtain the intermediate value of the heartbeat keep-alive interval parameter, and a heartbeat keep-alive message is sent to the server based on the intermediate value of the heartbeat keep-alive interval parameter; if a heartbeat keep-alive response is received, the currently used heartbeat keep-alive interval parameter is determined as the detection heartbeat keep-alive interval parameter; if no heartbeat keep-alive response is received, the intermediate value of the heartbeat keep-alive interval parameter is continued to be reduced to obtain the intermediate value of the heartbeat keep-alive interval parameter, and the operation of sending a heartbeat keep-alive message to the server based on the intermediate value of the heartbeat keep-alive interval parameter is returned.
6. The method according to claim 3 or 4, characterized in that: After sending the heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameter, the method further includes: If the current location of the client changes, and the distance between the changed longitude and latitude corresponding to the changed location and the reference longitude and latitude is greater than the distance threshold, a heartbeat keep-alive interval parameter request message is resent to the server, where the heartbeat keep-alive interval parameter request message includes the changed longitude and latitude; Receive a heartbeat keep-alive interval parameter response message returned by the server, wherein the heartbeat keep-alive interval parameter response message includes a candidate heartbeat keep-alive interval parameter, and the candidate heartbeat keep-alive interval parameter is a heartbeat keep-alive interval parameter corresponding to the target longitude and latitude that satisfy the distance constraint condition after the change; Send a heartbeat keepalive message to the server based on the candidate heartbeat keepalive interval parameter.
7. The method according to claim 3 or 4, characterized in that: After sending the heartbeat keep-alive message to the server based on the target heartbeat keep-alive interval parameter, the method further includes: If an SMS message sent by the server is received, and the SMS message is used to indicate that the TCP persistent connection between the client and the server has been disconnected, a TCP persistent connection establishment request message is sent to the server to establish a TCP persistent connection through the TCP persistent connection establishment request message.
8. A heartbeat keep-alive system, characterized in that: The system includes a client and a server, and a TCP long connection is established between the client and the server, wherein: The client is used to send a heartbeat keep-alive interval parameter request message to the server, wherein the heartbeat keep-alive interval parameter request message includes a reference longitude and latitude corresponding to the current location of the client; The server is used to determine a minimum distance based on the distance between the reference longitude and latitude and each longitude and latitude in a mapping table, wherein the mapping table includes a correspondence between longitude and latitude and a heartbeat keep-alive interval parameter; if the minimum distance is less than a preset threshold, the longitude and latitude corresponding to the minimum distance is determined as a target longitude and latitude that satisfies a distance constraint condition with the reference longitude and latitude, and a candidate heartbeat keep-alive interval parameter corresponding to the target longitude and latitude is queried from the mapping table, and a heartbeat keep-alive interval parameter response message is sent to the client, wherein the heartbeat keep-alive interval parameter response message includes the candidate heartbeat keep-alive interval parameter; The client is used to send a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter after receiving the heartbeat keep-alive interval parameter response message; wherein the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
9. A heartbeat keep-alive device, characterized in that: A TCP long connection is established between a client and a server, and the device is applied to the client, and the device includes: A sending module, used for sending a heartbeat keep-alive interval parameter request message to the server, wherein the heartbeat keep-alive interval parameter request message includes a reference longitude and latitude corresponding to the current location of the client; A receiving module, configured to receive a heartbeat keep-alive interval parameter response message returned by the server, wherein the heartbeat keep-alive interval parameter response message includes a candidate heartbeat keep-alive interval parameter, wherein the candidate heartbeat keep-alive interval parameter is a heartbeat keep-alive interval parameter corresponding to a target longitude and latitude that satisfies a distance constraint condition with a reference longitude and latitude; The sending module is further used to send a heartbeat keep-alive message to the server based on the candidate heartbeat keep-alive interval parameter, and the heartbeat keep-alive message is used to maintain a TCP long connection between the client and the server.
10. A client device, characterized in that: include: a processor and a machine-readable storage medium storing machine-executable instructions executable by the processor; The processor is used to execute machine executable instructions to implement the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Method for obtaining heart beat period, terminal and server
CN103209089A
Heartbeat connection method and heartbeat connection device
CN107197509A
Method and device for achieving heartbeat
CN107204999A
Method and device used for determining heartbeat period
CN108494615A
UDP dynamic heartbeat implementation method and device and storage medium
CN112422646A