Long connection establishment control method and device, storage medium and electronic device
By applying the gateway as the redirecting server, the client can directly establish a connection with the long-connected server, solving the problem of waste of service resources caused by limited port number of the reverse proxy server, and achieving efficient server resource utilization and massive connection support.
Patent Information
- Application Number
- CN202311593794.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-24
- Publication Date
- 2025-05-27
AI Technical Summary
In the prior art, the number of service port numbers of the reverse proxy server is limited, resulting in serious waste of service resources when long connections are established and the number of connections cannot be effectively increased.
By sending a connection request to the application gateway, the application gateway returns the address of the long-connected server as the redirecting server. The client directly establishes a connection with the long-connected server, skipping the reverse proxy server, thereby avoiding port number restrictions.
It realizes improving server resource utilization, avoiding the restriction on the number of connections by the reverse proxy server port number, and supports massive connections.
Smart Images

Figure CN120050329A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence, and in particular, to a control method and device for establishing a long connection, a storage medium, and an electronic device. Background Art
[0002] Currently, a terminal device can establish a long connection with a backend long connection server through a reverse proxy server. In this process, the reverse proxy server needs to initiate a connection to the backend long connection server as a client itself. Since the number of port numbers of the reverse proxy server is limited, the number of connections that can be established is restricted by the number of its own service port numbers. If the number of connections needs to be increased, only by continuously expanding the number of reverse proxy servers, there is a serious waste of service resources.
[0003] It can be seen that in the long connection establishment method in the related art, there is a serious waste of service resources caused by the limited number of service port numbers of the reverse proxy server. Summary of the Invention
[0004] Embodiments of this application provide a control method and device for establishing a long connection, a storage medium, and an electronic device, so as to at least solve the problem of serious waste of service resources caused by the limited number of service port numbers of the reverse proxy server in the long connection establishment method in the related art.
[0005] According to one aspect of the embodiments of this application, a control method for establishing a long connection is provided, including: sending a first connection request to an application gateway, where the first connection request is used to request to establish a long connection service; receiving a first request response message sent by the application gateway, where the first request response message is a response message returned by the application gateway as a redirect server to redirect the first connection request, and the first request response message carries the address of a first long connection server; based on the address of the first long connection server extracted from the first request response message, sending a second connection request to the first long connection server to request to establish a long connection with the first long connection server.
[0006] In an exemplary embodiment, before receiving the first request response message sent by the application gateway, the method further includes: receiving a registration request of the first long connection server on the application gateway, where the registration request is used to request to register the first long connection server as a server for long connection services; in response to the received registration request, when the registration request passes the verification, registering the first long connection server as a server for long connection services on the application gateway through the address of the first long connection server.
[0007] In an exemplary embodiment, after sending the first connection request to the application gateway, the method further includes: in response to the received first connection request, in the case where there are multiple registered long connection servers, selecting the first long connection server from the multiple long connection servers on the application gateway, where the first long connection server is determined based on the target location information carried in the first connection request and is the long connection server with the shortest distance to the location indicated by the target location information; or, is a long connection server randomly selected from the multiple long connection servers.
[0008] In an exemplary embodiment, after receiving the first request response message sent by the application gateway, the method further includes: extracting address information from a specified field in the hypertext transfer protocol (HTTP) request header of the first request response message to obtain the address of the first long connection server.
[0009] In an exemplary embodiment, the receiving the first request response message sent by the application gateway includes: receiving the first request response message with an HTTP status code of 302 returned by the application gateway as a redirect server.
[0010] In an exemplary embodiment, after sending a second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message, the method further includes: in the case where it is determined that the establishment of a long connection with the first long connection server fails, sending a third connection request to the application gateway, where the third connection request is used to request the establishment of a long connection service; receiving a second request response message sent by the application gateway, where the second request response message is a response message returned by the application gateway as a redirect server for redirecting the third connection request, and the second request response message carries the address of a second long connection server; based on the address of the second long connection server extracted from the second request response message, sending a fourth connection request to the second long connection server to request the establishment of a long connection with the second long connection server.
[0011] In an exemplary embodiment, after sending a second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message, the method further includes: in the case of determining that the establishment of a long connection with the first long connection server fails, based on the address of the third long connection server extracted from the first request response message, sending a fifth connection request to the third long connection server to request the establishment of a long connection with the third long connection server, where the first request response message carries the address of the first long connection server and the addresses of multiple candidate long connection servers, and the multiple candidate long connection servers include the third long connection server.
[0012] According to another aspect of the embodiments of the present application, there is also provided a control device for long connection establishment, including: a first sending unit configured to send a first connection request to an application gateway, where the first connection request is used to request the establishment of a long connection service; a first receiving unit configured to receive a first request response message sent by the application gateway, where the first request response message is a response message returned by the application gateway as a redirect server for redirecting the first connection request, and the first request response message carries the address of the first long connection server; a second sending unit configured to send a second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message to request the establishment of a long connection with the first long connection server.
[0013] In an exemplary embodiment, the device further includes: a second receiving unit configured to receive a registration request of the first long connection server on the application gateway before receiving the first request response message sent by the application gateway, where the registration request is used to request registering the first long connection server as a server for long connection service; a registration unit configured to, in response to the received registration request, in the case of passing the verification of the registration request, register the first long connection server as a server for long connection service on the application gateway through the address of the first long connection server.
[0014] In an exemplary embodiment, the device further includes: a selection unit configured to, after sending the first connection request to the application gateway, in response to the received first connection request, in the case of multiple registered long connection servers, select the first long connection server from the multiple long connection servers on the application gateway, where the first long connection server is determined based on the target location information carried in the first connection request and is the long connection server with the shortest distance to the location indicated by the target location information; or is a long connection server randomly selected from the multiple long connection servers.
[0015] In an exemplary embodiment, the apparatus further includes: an extraction unit, configured to extract address information from a specified field in a hypertext transfer protocol (HTTP) request header of the first request response message after receiving the first request response message sent by the application gateway, so as to obtain the address of the first long connection server.
[0016] In an exemplary embodiment, the first receiving unit includes: a receiving module, configured to receive the first request response message with an HTTP status code of 302 returned by the application gateway as a redirect server.
[0017] In an exemplary embodiment, the apparatus further includes: a third sending unit, configured to send a third connection request to the application gateway when it is determined that a long connection to the first long connection server fails after sending a second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message, where the third connection request is used to request the establishment of a long connection service; a third receiving unit, configured to receive a second request response message sent by the application gateway, where the second request response message is a response message returned by the application gateway as a redirect server for redirecting the third connection request, and the second request response message carries the address of a second long connection server; a fourth sending unit, configured to send a fourth connection request to the second long connection server based on the address of the second long connection server extracted from the second request response message, so as to request to establish a long connection with the second long connection server.
[0018] In an exemplary embodiment, the apparatus further includes: a fifth sending unit, configured to send a fifth connection request to the third long connection server based on the address of the third long connection server extracted from the first request response message when it is determined that a long connection to the first long connection server fails after sending a second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message, so as to request to establish a long connection with the third long connection server, where the first request response message carries the address of the first long connection server and the addresses of multiple candidate long connection servers, and the multiple candidate long connection servers include the third long connection server.
[0019] According to another aspect of the embodiments of the present application, there is also provided a computer-readable storage medium, in which a computer program is stored, where the computer program is configured to execute the above-mentioned control method for long connection establishment when running.
[0020] According to another aspect of the embodiments of the present application, an electronic device is further provided, including a memory, a processor, and a computer program stored on the memory and executable on the processor. Wherein, the above-mentioned processor executes the above-mentioned control method for establishing a long connection through the computer program.
[0021] In the embodiments of the present application, the method of removing the reverse proxy service gateway, using the redirect service to return the real long connection service address, and the client directly establishing a connection with the long connection service is adopted to send a first connection request to the application gateway, where the first connection request is used to request the establishment of a long connection service; receive a first request response message sent by the application gateway, where the first request response message is a response message returned by the application gateway as a redirect server for redirecting the first connection request, and the first request response message carries the address of the first long connection server; based on the address of the first long connection server extracted from the first request response message, send a second connection request to the first long connection server to request to establish a long connection with the first long connection server. Since when establishing a long connection, the application gateway only returns the address of the backend long connection server to the client as a redirect server, and the client directly establishes a connection with the backend long connection server, without the application gateway acting as a reverse proxy server to connect to the backend long connection server. Since the reverse proxy server is skipped, the purpose of avoiding the limitation of the number of service port numbers of the reverse proxy server on the number of long connections that can be established by a single reverse proxy server can be achieved, and the technical effect of improving the utilization rate of server resources can be achieved, thereby solving the problem of serious waste of service resources caused by the limited number of service port numbers of the reverse proxy server in the long connection establishment method in the related art. Description of the Drawings
[0022] The drawings here are incorporated into the specification and constitute a part of this specification, showing embodiments consistent with the present application, and are used together with the specification to explain the principles of the present application.
[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, for those of ordinary skill in the art, other drawings can be obtained according to these drawings without creative efforts.
[0024] Figure 1 It is a schematic diagram of the hardware environment of an optional control method for establishing a long connection according to the embodiments of the present application;
[0025] Figure 2 It is a schematic flowchart of an optional control method for establishing a long connection according to the embodiments of the present application;
[0026] Figure 3 It is a schematic diagram of an optional control method for establishing a long connection according to an embodiment of the present application;
[0027] Figure 4 It is a flowchart of another optional control method for establishing a long connection according to an embodiment of the present application;
[0028] Figure 5 It is a flowchart of yet another optional control method for establishing a long connection according to an embodiment of the present application;
[0029] Figure 6 It is a schematic diagram of another optional control method for establishing a long connection according to an embodiment of the present application;
[0030] Figure 7 It is a block diagram of the structure of an optional control device for establishing a long connection according to an embodiment of the present application;
[0031] Figure 8 It is a block diagram of the structure of an optional electronic device according to an embodiment of the present application. Detailed implementation manners
[0032] In order to enable those skilled in the art to better understand the solutions of the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present application.
[0033] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and do not necessarily need to describe a specific order or sequence. It should be understood that such data can be interchanged under appropriate circumstances so that the embodiments of the present application described herein can be implemented in an order different from those illustrated or described herein. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device that includes a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0034] According to one aspect of an embodiment of the present application, a control method for establishing a long connection is provided. The control method for establishing a long connection is widely used in smart home (Smart Home), smart home, smart home device ecology, smart residential (Intelligence House) ecology and other whole-house intelligent digital control application scenarios. Optionally, in this embodiment, the control method for establishing a long connection can be applied to Figure 1 In the hardware environment composed of the terminal device 102 and the server 104 shown in FIG. Figure 1 As shown, the server 104 is connected to the terminal device 102 via a network, and can be used to provide services (such as application services, etc.) for the terminal device or a client installed on the terminal device. A database can be set on the server or independently of the server to provide data storage services for the server 104. Cloud computing and / or edge computing services can be configured on the server or independently of the server to provide data computing services for the server 104.
[0035] The network may include but is not limited to at least one of the following: wired network, wireless network. The wired network may include but is not limited to at least one of the following: wide area network, metropolitan area network, local area network, and the wireless network may include but is not limited to at least one of the following: WIFI (Wireless Fidelity), Bluetooth. The terminal device 102 may be but is not limited to a PC, a mobile phone, a tablet computer, a smart air conditioner, a smart range hood, a smart refrigerator, a smart oven, a smart stove, a smart washing machine, a smart water heater, a smart washing device, a smart dishwasher, a smart projection device, a smart TV, a smart clothes drying rack, a smart curtain, a smart audio and video, a smart socket, a smart speaker, a smart speaker, a smart fresh air device, a smart kitchen and bathroom device, a smart bathroom device, a smart sweeping robot, a smart window cleaning robot, a smart mopping robot, a smart air purification device, a smart steamer, a smart microwave oven, a smart kitchen treasure, a smart purifier, a smart water dispenser, a smart door lock, and the like.
[0036] The control method for establishing a persistent connection in the embodiment of the present application may be executed by the server 104, or by the terminal device 102, or by both the server 104 and the terminal device 102. The control method for establishing a persistent connection in the embodiment of the present application may be executed by the terminal device 102 or by a client installed thereon.
[0037] Taking the control method for establishing a long connection in this embodiment executed by the client as an example, Figure 2 is a flow chart of an optional control method for establishing a long connection according to an embodiment of the present application, such as Figure 2 As shown, the process of the method may include the following steps:
[0038] Step S202: Send a first connection request to the application gateway, where the first connection request is used to request the establishment of a long connection service.
[0039] The long connection establishment control method in this embodiment can be applied to scenarios where a long connection relationship needs to be established with a long connection server. Here, the long connection server is a network server that can establish and maintain a long-term connection with the client. Compared with the traditional short connection server, the long connection server does not immediately disconnect the connection after each communication ends, but keeps the connection open for multiple communications when needed. By keeping the connection open, the overhead of connection establishment and disconnection can be reduced, and the communication efficiency and response speed can be improved. The long connection server can use different communication protocols, such as TCP / IP (Transmission Control Protocol / Internet Protocol), WebSocket, a full-duplex communication protocol, etc.
[0040] In the related art, the terminal device can establish a long connection with the backend long connection server through a reverse proxy server. In this process, the reverse proxy server needs to initiate a connection to the backend long connection server as a client itself. Since the number of port numbers of the reverse proxy server is limited, the number of connections that can be established is restricted by the number of its own service port numbers. If the number of connections needs to be increased, only the number of reverse proxy servers can be continuously expanded, resulting in a serious waste of service resources.
[0041] For example, using an application gateway as a reverse proxy service for load balancing to request the backend long connection server, this solution has a connection number bottleneck due to the limited number of ports of the application gateway. That is, when the reverse proxy server (such as nginx, gateway, etc.) initiates a connection to the backend long connection server as a client itself, there is a limit on the number of port numbers (the maximum number of ports for linux services is 65535). Therefore, the maximum number of connections for a single reverse proxy server cannot exceed 65535.
[0042] To solve at least part of the above problems, in this embodiment, when establishing a long connection, the application gateway, as a redirect server, only returns the address of the backend long connection server to the client, and the client directly establishes a connection with the backend long connection server, without the application gateway acting as a reverse proxy server to connect to the backend long connection server. Since the reverse proxy server is skipped, the purpose of avoiding the limitation of the number of service port numbers of the reverse proxy server on the number of long connections that can be established by a single reverse proxy server can be achieved, and the technical effect of improving the utilization rate of server resources can be achieved, thereby solving the problem of serious waste of service resources caused by the limited number of service port numbers of the reverse proxy server in the long connection establishment method in the related art.
[0043] In this embodiment, when establishing a long connection, the client can send a first connection request to the application gateway to request the establishment of a long connection service. At this time, the application gateway can act as a redirect server and, in response to the first connection request sent by the client, send a request response message to the client. Through redirection, the server can guide the user from one page to another to implement specific business logic or provide a better user experience.
[0044] Step S204: Receive the first request response message sent by the application gateway, where the first request response message is the response message returned by the application gateway as a redirect server for redirecting the first connection request, and the address of the first long connection server is carried in the first request response message.
[0045] In this embodiment, the client can receive the first request response message sent by the application gateway. The first request response message can be the response message returned by the application gateway as a redirect server for redirecting the first connection request, and the address of the first long connection server is carried in the first request response message, that is, the real long connection service address of the backend. Through the real long connection address, the client can directly initiate a connection request to the long connection service.
[0046] Here, after receiving the client request, the redirect server can, according to some preset rules and conditions, redirect the requested URL (Uniform Resource Locator) to another URL. This process usually involves HTTP status codes, such as 301 permanent redirect or 302 temporary redirect.
[0047] Step S206: Based on the address of the first long connection server extracted from the first request response message, send a second connection request to the first long connection server to request the establishment of a long connection with the first long connection server.
[0048] After extracting the address of the first long connection server from the first request response message, the client can directly send a second connection request to the first long connection server to request the establishment of a long connection with the first long connection server.
[0049] It should be noted that the interaction between the server and the terminal device can be based on WebSocket. WebSocket is a protocol for full-duplex communication over a single TCP (Transmission Control Protocol) connection, which makes the data exchange between the client and the server simpler and allows the server to actively push data to the client (for example, the target application). In the WebSocket API (Application Programming Interface), the client and the server only need to complete a handshake once, and then a persistent connection can be directly established between them for two-way data transmission.
[0050] Through the above steps S202 to S206, a first connection request is sent to the application gateway, where the first connection request is used to request the establishment of a long connection service; a first request response message sent by the application gateway is received, where the first request response message is a response message returned by the application gateway as a redirect server for redirecting the first connection request, and the first request response message carries the address of the first long connection server; based on the address of the first long connection server extracted from the first request response message, a second connection request is sent to the first long connection server to request to establish a long connection with the first long connection server, which solves the problem of serious waste of service resources caused by the limited number of service port numbers of the reverse proxy server in the long connection establishment method in the related art.
[0051] In an exemplary embodiment, before receiving the first request response message sent by the application gateway, the above method further includes:
[0052] S11, receiving a registration request of the first long connection server on the application gateway, where the registration request is used to request to register the first long connection server as a server for long connection services;
[0053] S12, in response to the received registration request, when the registration request passes the verification, registering the first long connection server as a server for long connection services on the application gateway through the address of the first long connection server.
[0054] Before establishing a persistent connection, the application gateway may verify and determine the server of the persistent connection service. In this embodiment, before the client receives the first request response message sent by the application gateway as a redirect server, the application gateway may receive a registration request of the first persistent connection server to request that the first persistent connection server be registered as a server of the persistent connection service. After receiving the registration request, the application gateway may respond to the received registration request and verify the registration request. If the registration request is verified, the application gateway may register the first persistent connection server as a server of the persistent connection service through the address of the first persistent connection server. If the registration request verification fails, the application gateway may return registration failure indication information to the first persistent connection server through the address of the first persistent connection server.
[0055] Here, the registration request can be used to verify the identity of the persistent connection server. By sending a registration request to the redirect server, the persistent connection server can prove that it is a legitimate server and obtain the corresponding authentication information to ensure that subsequent communications are safe and reliable. The registration request can also be used to add the persistent connection server to the load balancing pool of the redirect server. The redirect server can distribute the client's request to different persistent connection servers based on the load of the persistent connection server and other conditions to achieve load balancing and improve the performance and scalability of the entire system.
[0056] Optionally, the registration request can also be used for fault recovery. If a persistent connection server fails or goes down, the redirect server can detect the change in the server's status through the registration request and reallocate the client's request to other available persistent connection servers to ensure service continuity and reliability. The registration request can also be used to update configuration information to the redirect server. The persistent connection server can send a registration request to the redirect server periodically or when necessary to obtain the latest configuration information, such as server address, port number, load balancing policy, etc., to keep in sync with the redirect server.
[0057] For example, Figure 3 As shown, the persistent connection server initiates a registration request to the redirect server. When the registration request is verified, the redirect server (application gateway) registers the persistent connection server as a server for persistent connection service through the address of the persistent connection server.
[0058] Through this embodiment, by applying the gateway as a redirect server to check and determine the server of the persistent connection service before sending a response message carrying the persistent connection server address to the client, the security of the persistent connection can be improved and load balancing can be achieved.
[0059] In an exemplary embodiment, after sending a first connection request to the application gateway, the method further includes:
[0060] S21, in response to the received first connection request, in the case where there are multiple registered long - connection servers, select a first long - connection server from the multiple long - connection servers on the application gateway, where the first long - connection server is determined based on the target location information carried in the first connection request and is the long - connection server closest to the location indicated by the target location information; or, it is a long - connection server randomly selected from the multiple long - connection servers.
[0061] After the application gateway receives the first connection request sent by the client, as a redirect server, the application gateway can return a first request response message carrying the address of the first long - connection server to the client. In the case where there are multiple registered long - connection servers, the application gateway can select a first long - connection server from the multiple long - connection servers. Optionally, the first long - connection server is determined based on the target location information carried in the first connection request and is the long - connection server closest to the location indicated by the target location information. The target location information can be the location information of the terminal device corresponding to the client; or, the first long - connection server can be a long - connection server randomly selected from the multiple long - connection servers. This embodiment does not make a limitation in this regard.
[0062] Through this embodiment, by selecting the long - connection server closest to the terminal device corresponding to the client or randomly selecting a registered long - connection server as the target long - connection server from the multiple registered long - connection servers, the rationality of selecting the target long - connection server can be improved.
[0063] In an exemplary embodiment, after receiving the first request response message sent by the application gateway, the method further includes:
[0064] S31, extract the address information from a specified field in the hypertext transfer protocol (HTTP) request header of the first request response message to obtain the address of the first long - connection server.
[0065] After the client receives the first request response message sent by the application gateway as a redirect server, it can extract the address information from a specified field in the hypertext transfer protocol (HTTP) request header of the first request response message to obtain the address of the first long - connection server. Here, the HTTP response status code indicates whether a specific HTTP request has been successfully completed. The HTTP status code can be 301 permanent redirect or 302 temporary redirect.
[0066] For example, in this embodiment, when the client initiates a request and the server returns an HTTP status code, the client can extract the address information from the location field in the response header to obtain the actual address of the backend long connection server.
[0067] Through this embodiment, by extracting the address information from the specified field of the HTTP request header of the response message sent by the redirect server, the address of the target backend long connection server can be obtained, so that the client can directly initiate a connection request to the long connection service, making the number of connections no longer limited by the proxy server, and theoretically supporting a large number of connections.
[0068] In an exemplary embodiment, receiving the first request response message sent by the application gateway includes:
[0069] S41, receiving the first request response message with an HTTP status code of 302 returned by the application gateway as a redirect server.
[0070] In this embodiment, the application gateway can return a response with an HTTP status code of 302 as a redirect server, and carry the actual long connection service address of the backend in the HTTP request header to the client. After receiving the 302 redirect, the client can take out the actual long connection address from the HTTP request header and directly initiate a connection request to the long connection service.
[0071] Through this embodiment, the client receives the first request response message with an HTTP status code of 302 returned by the application gateway as a redirect server, can obtain the actual long connection address, and directly initiate a connection request to the long connection service, improving the resource utilization rate of the server resources.
[0072] In an exemplary embodiment, after sending the second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message, the above method further includes:
[0073] S51, in the case of determining that the long connection with the first long connection server fails, sending a third connection request to the application gateway, where the third connection request is used to request to establish a long connection service;
[0074] S52, receiving the second request response message sent by the application gateway, where the second request response message is the response message returned by the application gateway as a redirect server for redirecting the third connection request, and the second request response message carries the address of the second long connection server;
[0075] S53. Based on the address of the second long - connection server extracted from the second request response message, send a fourth connection request to the second long - connection server to request to establish a long connection with the second long - connection server.
[0076] Combined with Figure 4 As shown, through steps S402 - S406, when the client fails to establish a long connection with the first long - connection server, the client can resend a connection request to the application gateway acting as a redirect server to obtain the address of the second long - connection server, and directly initiate a connection request to the second long - connection server based on the obtained address of the second long - connection server. Here, the second long - connection server can be a registered server different from the first long - connection server.
[0077] Optionally, before sending the third connection request to the application gateway, the client can send a long - connection establishment request to the first long - connection server a preset number of times based on the address of the first long - connection server. When the number of times of failure to establish a long connection between the client and the first long - connection server exceeds the preset value, it is determined that the establishment of a long connection with the first long - connection server fails, and a third connection request is sent to the application gateway to request to establish a long - connection service.
[0078] The third connection request can be similar to the foregoing first connection request, the second request response message can be similar to the foregoing first request response message, and the fourth connection request can be similar to the foregoing second connection request, which will not be elaborated here.
[0079] Through this embodiment, when the client fails to establish a long connection with the first long - connection server, it can obtain the address of a long - connection server through the application gateway acting as a redirect server and establish a long connection again, avoiding the interruption of long - connection establishment.
[0080] In an exemplary embodiment, after sending the second connection request to the first long - connection server based on the address of the first long - connection server extracted from the first request response message, the above - mentioned method further includes:
[0081] S61. When it is determined that the establishment of a long connection with the first long - connection server fails, based on the address of the third long - connection server extracted from the first request response message, send a fifth connection request to the third long - connection server to request to establish a long connection with the third long - connection server, where the first request response message carries the address of the first long - connection server and the addresses of multiple candidate long - connection servers, and the multiple candidate long - connection servers include the third long - connection server.
[0082] In order to improve the success rate of establishing a persistent connection, in this embodiment, the first request response message sent by the application gateway acting as a redirect server to the client may carry the addresses of multiple registered candidate persistent connection servers. After sending a second connection request to the first persistent connection server based on the address of the first persistent connection server extracted from the first request response message, if it is determined that the persistent connection with the first persistent connection server has failed, the client may send a fifth connection request to the third persistent connection server based on the address of the third persistent connection server extracted from the first request response message, so as to request to establish a persistent connection with the third persistent connection server.
[0083] Here, the candidate long connection server may include a third long connection server. The candidate long connection server may be a fixed preset number of back-end long connection servers, or may include all registered long connection servers, or may include all registered long connection servers whose distance to the terminal device corresponding to the client is within a preset threshold. This is not limited in this embodiment.
[0084] Through this embodiment, the addresses of multiple candidate persistent connection servers are carried in the redirection response message, which can improve the success rate of persistent connection establishment.
[0085] The following is an explanation of the control method for establishing a persistent connection in the embodiment of the present application in conjunction with an optional example. This optional example provides a solution that saves a lot of resources of the reverse proxy server. By removing the reverse proxy service gateway and using the redirection service to return the real persistent connection service address, the device can directly establish a connection with the persistent connection service. Figure 5 As shown, the process of the control method for establishing a long connection in this optional example may include the following steps:
[0086] Step S502: The client initiates a ws request to the redirect server (gateway).
[0087] The ws request may refer to a request for interaction between a server and a terminal device based on WebSocket. In this embodiment, the ws request may be a request for requesting to establish a persistent connection service.
[0088] Step S504: the persistent connection server initiates a registration request to the redirect server. If the registration request is verified to be successful, the redirect server registers the persistent connection server as a server of the persistent connection service through the address of the persistent connection server.
[0089] Step S506: The redirect server returns a response with an HTTP status code of 302 and carries the real long connection service address of the backend in the HTTP request header to the client.
[0090] Step S508: After the client receives the 302 redirect, it obtains the address of the real long connection from the HTTP request header and directly initiates a connection request to the long connection service.
[0091] Through this optional example, the application gateway no longer acts as a reverse proxy server but as a redirect server. Combining Figure 6 , the client first sends a request to the redirect server, and then the redirect server returns a response with an HTTP status code of 302 and carries the address of the real long connection service at the back end in the HTTP request header to the client. After the client receives the 302 redirect, it obtains the address of the real long connection from the HTTP request header and directly initiates a connection request to the long connection service. In this way, the reverse proxy server is skipped, that is, there is no limit on the number of connections.
[0092] It should be noted that for the foregoing method embodiments, for the sake of simple description, they are all expressed as a series of action combinations. However, those skilled in the art should know that this application is not limited by the described action sequence, because according to this application, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily essential to this application.
[0093] Through the description of the above embodiments, those skilled in the art can clearly understand that the method according to the above embodiments can be implemented by means of software plus a necessary general hardware platform. Of course, it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of this application, in essence, or the part that contributes to the prior art can be embodied in the form of a software product. The computer software product is stored in a storage medium (such as ROM (Read-Only Memory), RAM (Random Access Memory), magnetic disk, optical disk), and includes several instructions to enable a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods of the various embodiments of this application.
[0094] According to another aspect of the embodiments of this application, a control device for establishing a long connection for implementing the above long connection establishment control method is also provided. Figure 7 It is a structural block diagram of an optional control device for establishing a long connection according to the embodiments of this application. As Figure 7 shown, the device may include:
[0095] A first sending unit 702, configured to send a first connection request to the application gateway, where the first connection request is used to request to establish a long connection service;
[0096] The first receiving unit 704, connected to the first sending unit 702, is configured to receive a first request response message sent by an application gateway, where the first request response message is a response message returned by the application gateway as a redirect server for redirecting a first connection request, and the address of a first long connection server is carried in the first request response message;
[0097] The second sending unit 706, connected to the first receiving unit 704, is configured to send a second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message, so as to request to establish a long connection with the first long connection server.
[0098] It should be noted that the first sending unit 702 in this embodiment may be configured to execute the above step S202, the first receiving unit 704 in this embodiment may be configured to execute the above step S204, and the second sending unit 706 in this embodiment may be configured to execute the above step S206.
[0099] Through the above modules, a first connection request is sent to the application gateway, where the first connection request is used to request to establish a long connection service; a first request response message sent by the application gateway is received, where the first request response message is a response message returned by the application gateway as a redirect server for redirecting the first connection request, and the address of a first long connection server is carried in the first request response message; a second connection request is sent to the first long connection server based on the address of the first long connection server extracted from the first request response message, so as to request to establish a long connection with the first long connection server, which solves the problem of serious waste of service resources caused by the limited number of service port numbers of the reverse proxy server in the long connection establishment method in the related art, and saves server resources.
[0100] In an exemplary embodiment, the above device further includes:
[0101] A second receiving unit, configured to receive a registration request of a first long connection server on the application gateway before receiving the first request response message sent by the application gateway, where the registration request is used to request to register the first long connection server as a server for long connection services;
[0102] A registration unit, configured to, in response to the received registration request, register the first long connection server as a server for long connection services on the application gateway through the address of the first long connection server when the verification of the registration request passes.
[0103] In an exemplary embodiment, the above device further includes:
[0104] A selection unit, configured to, after sending a first connection request to an application gateway, in response to the received first connection request, when there are multiple registered long - connection servers, select a first long - connection server from the multiple long - connection servers on the application gateway, where the first long - connection server is determined based on the target location information carried in the first connection request and is the long - connection server with the shortest distance to the location indicated by the target location information; or, is a long - connection server randomly selected from the multiple long - connection servers.
[0105] In an exemplary embodiment, the above - mentioned device further includes:
[0106] An extraction unit, configured to, after receiving a first request response message sent by the application gateway, extract address information from a specified field in the hypertext transfer protocol (HTTP) request header of the first request response message to obtain the address of the first long - connection server.
[0107] In an exemplary embodiment, the first receiving unit includes:
[0108] A receiving module, configured to receive a first request response message with an HTTP status code of 302 returned by the application gateway as a redirect server.
[0109] In an exemplary embodiment, the above - mentioned device further includes:
[0110] A third sending unit, configured to, after sending a second connection request to the first long - connection server based on the address of the first long - connection server extracted from the first request response message, when it is determined that the establishment of a long - connection with the first long - connection server fails, send a third connection request to the application gateway, where the third connection request is used to request the establishment of a long - connection service;
[0111] A third receiving unit, configured to receive a second request response message sent by the application gateway, where the second request response message is a response message returned by the application gateway as a redirect server for redirecting the third connection request, and the second request response message carries the address of a second long - connection server;
[0112] A fourth sending unit, configured to send a fourth connection request to the second long - connection server based on the address of the second long - connection server extracted from the second request response message to request the establishment of a long - connection with the second long - connection server.
[0113] In an exemplary embodiment, the above - mentioned device further includes:
[0114] A fifth sending unit, configured to, after sending a second connection request to a first long connection server based on the address of the first long connection server extracted from a first request response message, and in the case of determining that the establishment of a long connection with the first long connection server fails, send a fifth connection request to a third long connection server based on the address of the third long connection server extracted from the first request response message, so as to request to establish a long connection with the third long connection server, where the first request response message carries the address of the first long connection server and the addresses of a plurality of candidate long connection servers, and the plurality of candidate long connection servers include the third long connection server.
[0115] It should be noted here that the examples and application scenarios implemented by the above modules and the corresponding steps are the same, but are not limited to the content disclosed in the above embodiments. It should be noted that the above modules, as part of the device, can run in a hardware environment as shown in Figure 1 and can be implemented by software or by hardware, where the hardware environment includes a network environment.
[0116] According to another aspect of the embodiments of the present application, a storage medium is further provided. Optionally, in this embodiment, the above storage medium can be used to execute the program code of any one of the above long connection establishment control methods in the embodiments of the present application.
[0117] Optionally, in this embodiment, the above storage medium can be located on at least one of a plurality of network devices in the network shown in the above embodiments.
[0118] Optionally, in this embodiment, the storage medium is set to store program code for executing the following steps:
[0119] S1. Send a first connection request to an application gateway, where the first connection request is used to request to establish a long connection service;
[0120] S2. Receive a first request response message sent by the application gateway, where the first request response message is a response message returned by the application gateway as a redirect server for redirecting the first connection request, and the first request response message carries the address of the first long connection server;
[0121] S3. Based on the address of the first long connection server extracted from the first request response message, send a second connection request to the first long connection server to request to establish a long connection with the first long connection server.
[0122] Optionally, the specific examples in this embodiment can refer to the examples described in the above embodiments, and details are not described herein again.
[0123] Optionally, in this embodiment, the above storage medium may include, but is not limited to: various media such as USB flash drives, ROMs, RAMs, mobile hard disks, magnetic disks, or optical discs that can store program codes.
[0124] According to another aspect of the embodiments of the present application, an electronic device for implementing the above control method for establishing a long connection is further provided. The electronic device may be a server, a terminal, or a combination thereof.
[0125] Figure 8 is a structural block diagram of an optional electronic device according to the embodiments of the present application, as Figure 8 shown, including a processor 802, a communication interface 804, a memory 806, and a communication bus 808. Among them, the processor 802, the communication interface 804, and the memory 806 communicate with each other through the communication bus 808. Among them,
[0126] The memory 806 is used to store computer programs;
[0127] When the processor 802 is used to execute the computer program stored on the memory 806, the following steps are implemented:
[0128] S1, send a first connection request to the application gateway, where the first connection request is used to request the establishment of a long connection service;
[0129] S2, receive a first request response message sent by the application gateway, where the first request response message is a response message returned by the application gateway as a redirect server for redirecting the first connection request, and the first request response message carries the address of the first long connection server;
[0130] S3, based on the address of the first long connection server extracted from the first request response message, send a second connection request to the first long connection server to request the establishment of a long connection with the first long connection server.
[0131] Optionally, the communication bus may be a PCI (Peripheral Component Interconnect) bus, an EISA (Extended Industry Standard Architecture) bus, etc. The communication bus may be divided into an address bus, a data bus, a control bus, etc. For the sake of convenience of representation, Figure 8 only a thick line is shown in, but it does not mean that there is only one bus or one type of bus. The communication interface is used for communication between the above electronic device and other devices.
[0132] The memory may include RAM and may also include non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0133] As an example, the memory 806 may include, but is not limited to, the first sending unit 702, the first receiving unit 704, and the second sending unit 706 in the control device for establishing the long connection described above. In addition, it may also include, but is not limited to, other module units in the control device for establishing the long connection, which will not be elaborated in this example.
[0134] The aforementioned processor may be a general-purpose processor, which may include, but is not limited to: CPU (Central Processing Unit), NP (Network Processor), etc.; it may also be a DSP (Digital Signal Processing), ASIC (Application Specific Integrated Circuit), FPGA (Field-Programmable Gate Array), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0135] Optionally, the specific examples in this embodiment may refer to the examples described in the above embodiment, and will not be elaborated herein.
[0136] Those of ordinary skill in the art can understand that Figure 8 The structure shown is only schematic. The device for implementing the control method for establishing the long connection may be a terminal device, which may be a smart phone (such as an Android phone, an iOS phone, etc.), a tablet computer, a handheld computer, and a Mobile Internet Device (MID), a PAD, and other terminal devices. Figure 8 It does not limit the structure of the aforementioned electronic device. For example, the electronic device may further include more or fewer components (such as a network interface, a display device, etc.) than those shown Figure 8 in the figure, or have a different configuration from that shown Figure 8 in the figure.
[0137] Those of ordinary skill in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing the relevant hardware of the terminal device through a program, and the program can be stored in a computer-readable storage medium. The storage medium may include: a flash drive, a ROM, a RAM, a magnetic disk, or an optical disc, etc.
[0138] The serial numbers of the embodiments of the present application above are only for description and do not represent the superiority or inferiority of the embodiments.
[0139] If the integrated units in the above embodiments are implemented in the form of software function units and sold or used as independent products, they can be stored in the above computer-readable storage media. Based on such an understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing one or more computer devices (which may be personal computers, servers, or network devices, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present application.
[0140] In the above embodiments of the present application, the descriptions of the various embodiments each have their own focuses. For the parts not detailed in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0141] In the several embodiments provided by the present application, it should be understood that the disclosed client can be implemented in other ways. Among them, the device embodiments described above are only illustrative. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection between each other can be through some interfaces. The indirect coupling or communication connection of units or modules can be in an electrical or other form.
[0142] The units described as separate components may or may not be physically separated. The components displayed as units may or may not be physical units, that is, they can be located in one place or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution provided in this embodiment.
[0143] In addition, the functional units in the various embodiments of the present application can be integrated in a processing unit, or each unit can exist physically alone, or at least two units can be integrated in one unit. The above integrated units can be implemented in the form of hardware or in the form of software function units.
[0144] The above is only the preferred embodiment of the present application. It should be noted that for those of ordinary skill in the art, without departing from the principle of the present application, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present application.
Claims
1. A control method for establishing a long connection, characterized in that, it includes: Sending a first connection request to the application gateway, wherein the first connection request is used to request the establishment of a long connection service; Receiving a first request response message sent by the application gateway, wherein the first request response message is a response message returned by the application gateway as a redirect server for redirecting the first connection request, and the first request response message carries the address of the first long connection server; Based on the address of the first long connection server extracted from the first request response message, sending a second connection request to the first long connection server to request to establish a long connection with the first long connection server.
2. The method according to claim 1, characterized in that, Before receiving the first request response message sent by the application gateway, the method further includes: Receiving a registration request of the first long connection server on the application gateway, wherein the registration request is used to request registering the first long connection server as a server for long connection services; In response to the received registration request, when the registration request passes the verification, registering the first long connection server as a server for long connection services on the application gateway through the address of the first long connection server.
3. The method according to claim 2, characterized in that, After sending the first connection request to the application gateway, the method further includes: In response to the received first connection request, when there are multiple registered long connection servers, selecting the first long connection server on the application gateway from the multiple long connection servers, wherein the first long connection server is determined based on the target location information carried in the first connection request and is the long connection server with the shortest distance to the location indicated by the target location information; or, is a long connection server randomly selected from the multiple long connection servers.
4. The method according to claim 1, characterized in that, After receiving the first request response message sent by the application gateway, the method further includes: Extracting address information from a specified field in the hypertext transfer protocol (HTTP) request header of the first request response message to obtain the address of the first long connection server.
5. The method according to claim 4, characterized in that, Receiving the first request response message sent by the application gateway includes: Receiving the first request response message with an HTTP status code of 302 returned by the application gateway as a redirect server.
6. The method according to any one of claims 1 to 5, characterized in that, After sending the second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message, the method further includes: When it is determined that the establishment of a long connection with the first long connection server fails, sending a third connection request to the application gateway, wherein the third connection request is used to request the establishment of a long connection service; Receive a second request response message sent by the application gateway, where the second request response message is a response message returned by the application gateway as a redirect server for redirecting the third connection request, and the address of a second long connection server is carried in the second request response message; Based on the address of the second long connection server extracted from the second request response message, send a fourth connection request to the second long connection server to request to establish a long connection with the second long connection server.
7. The method according to any one of claims 1 to 5, characterized in that, after sending the second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message, the method further includes: in the case where it is determined that the establishment of the long connection with the first long connection server fails, based on the address of the third long connection server extracted from the first request response message, send a fifth connection request to the third long connection server to request to establish a long connection with the third long connection server, where the first request response message carries the address of the first long connection server and the addresses of multiple candidate long connection servers, and the multiple candidate long connection servers include the third long connection server.
8. A control device for establishing a long connection, characterized in that, it includes: a first sending unit, configured to send a first connection request to an application gateway, where the first connection request is used to request to establish a long connection service; a first receiving unit, configured to receive a first request response message sent by the application gateway, where the first request response message is a response message returned by the application gateway as a redirect server for redirecting the first connection request, and the address of a first long connection server is carried in the first request response message; a second sending unit, configured to send a second connection request to the first long connection server based on the address of the first long connection server extracted from the first request response message to request to establish a long connection with the first long connection server.
9. A computer-readable storage medium, characterized in that, the computer-readable storage medium includes a stored program, where the program, when running, executes the method according to any one of claims 1 to 7.
10. An electronic device, including a memory and a processor, characterized in that, a computer program is stored in the memory, and the processor is configured to execute the method according to any one of claims 1 to 7 through the computer program.