Internet of things connection method, gateway server and gateway
The method addresses resource occupation by switching between short and long connections based on user activity, optimizing resource use and ensuring reliable gateway-server connections.
Patent Information
- Application Number
- CN202010655333.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-07-09
- Publication Date
- 2024-12-13
- Estimated Expiration
- 2040-07-09
AI Technical Summary
The increasing number of gateways connected to a gateway server leads to significant occupation of server and network resources, and existing connection methods struggle to balance between resource efficiency and reliability.
Implementing a method that switches between short and long connections based on user activity, where gateways in a short connection state send heartbeat messages and switch to long connections upon user login, and revert to short connections when inactive, using protocols like UDP and TLS for efficient resource management.
This approach reduces resource occupation by the gateway server and network while ensuring reliable and responsive connections, optimizing resource utilization and maintaining connection performance.
Smart Images

Figure CN113992737B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of Internet of Things, and in particular to an Internet of Things connection method, a gateway server and a gateway. Background Art
[0002] The wave of the Internet of Things affects all aspects of society and life. The Internet of Things is the use of information technology by humans to solve existing life problems. For example, there are usually multiple terminals in modern families, such as mobile phones, tablets, TVs, and air conditioners. These terminals have various interfaces and cannot be interconnected with each other, and cannot effectively share data and information.
[0003] With the popularization of smart terminals, terminals usually have networking functions. By connecting different terminals to the public network, information interaction and control between preset different terminals (such as mobile phones and TVs) can be realized through the public network. For example, terminals such as tablets, TVs, and air conditioners are connected to the gateway server through the gateway, and the mobile terminal controls the gateway through the mobile phone APP (Application), adjusts or sets gateway parameters, controls gateway access terminals, etc. In order to ensure that the mobile terminal can control the gateway quickly and effectively, the gateway and the gateway server need to remain connected at all times. As the number of gateways connected to the gateway server continues to increase, if the gateway and the gateway server are always connected, a large amount of gateway server and network resources will be occupied. Summary of the invention
[0004] The present application provides an Internet of Things connection method, a gateway server, and a gateway, so that the gateway and the gateway server are always connected, thereby reducing the occupation of the gateway server and network resources.
[0005] In a first aspect, the Internet of Things connection method provided by the present application is applied to a gateway server, including:
[0006] Receiving a short connection heartbeat keep-alive message sent by a target gateway, wherein the target gateway in a short connection state with the gateway server sends a short connection heartbeat keep-alive message to the gateway server, and the target gateway is the gateway that the target user needs to configure;
[0007] If the target gateway ID sent by the application server is received, a message is replied to the target gateway corresponding to the target gateway ID according to the target gateway ID, wherein the application server obtains the target gateway ID according to the login information of the target user and sends the target gateway ID to the gateway server;
[0008] Receive a persistent connection message sent by the target gateway.
[0009] In a second aspect, the IoT connection method provided by the present application is applied to a gateway, including:
[0010] Send a short connection heartbeat keep-alive message to the gateway server;
[0011] If a reply message from the gateway server is received, when a message needs to be sent to the gateway server, a long connection message is sent to the gateway server.
[0012] In a third aspect, the gateway server provided by the present application is configured as follows:
[0013] Receiving a short connection heartbeat keep-alive message sent by a target gateway, wherein the target gateway in a short connection state with the gateway server sends a short connection heartbeat keep-alive message to the gateway server, and the target gateway is the gateway that the target user needs to configure;
[0014] If the target gateway ID sent by the application server is received, a message is replied to the target gateway corresponding to the target gateway ID according to the target gateway ID, wherein the application server obtains the target gateway ID according to the login information of the target user and sends the target gateway ID to the gateway server;
[0015] Receive a persistent connection message sent by the target gateway.
[0016] In a fourth aspect, the gateway provided by the present application is configured to: send a short connection heartbeat keep-alive message to the gateway server;
[0017] If a reply message from the gateway server is received, when a message needs to be sent to the gateway server, a long connection message is sent to the gateway server.
[0018] The Internet of Things connection method, gateway server and gateway provided by the present application are such that when the target user does not perform the target gateway configuration operation, the target gateway and the gateway server are in a short connection state, and the target gateway sends a short connection heartbeat keep-alive message to the gateway server; when the target user needs to perform the target gateway configuration operation, the target user logs in to the application server, the application server receives the login information of the target user, obtains the target user ID according to the login information of the target user, and obtains the target gateway ID bound to the target user ID according to the target user ID, and sends the target gateway ID of the target user to the gateway server; the gateway server receives the target gateway ID sent by the application server; the gateway server replies to the target gateway according to the target gateway ID; the target gateway receives the reply message from the gateway server, switches the connection state with the gateway server, and sends a long connection message to the gateway server when it needs to send a message to the gateway server. The gateway server notifies the application server that the gateway server has established a long connection with the target gateway bound to the target user; the application server receives the notification and can notify the target user that the target gateway bound to the target user is ready, and then the target user can initiate operation control to the target gateway to perform gateway configuration operation. Therefore, in the Internet of Things connection method, gateway server and gateway provided in the present application, switching between short connection and long connection is realized between the gateway server and the gateway. When the target user logs in to the application server, there is a long connection between the gateway server and the target gateway. When the target user does not log in to the application server, the gateway server releases the long connection established with the target gateway to avoid maintaining the long connection state between the gateway server and the target gateway and occupying the gateway server and network resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solution of the present invention, the drawings required for use in the embodiments are briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0020] Figure 1 A timing diagram of an Internet of Things connection provided in an embodiment of the present application;
[0021] Figure 2 A timing diagram of another Internet of Things connection provided in an embodiment of the present application;
[0022] Figure 3 A schematic diagram of a flow chart of an Internet of Things connection method provided in an embodiment of the present application;
[0023] Figure 4 A timing diagram of another Internet of Things connection provided in an embodiment of the present application. DETAILED DESCRIPTION
[0024] The following will be combined with the drawings in this embodiment to clearly and completely describe the technical solution in this embodiment. Obviously, the described embodiment is only a part of the embodiment of the present invention, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0025] The Internet of Things involved in the embodiments of the present application includes an application server, a mobile terminal, a gateway server and a gateway, wherein the gateway is connected to the gateway server through communication, and the application server is connected to the gateway server through communication, and the downstream devices in the Internet of Things are connected to the gateway through communication. An APP (Application) is set in the mobile terminal, and the user logs in to access the application server through the APP in the mobile terminal, and can initiate operations to the gateway through the application server and the gateway server, such as querying the devices connected to the gateway Internet of Things, controlling the devices connected to the gateway Internet of Things, etc.
[0026] Attached Figure 1 A timing diagram of an Internet of Things connection is provided for an embodiment of the present application. Taking querying the device connected to the gateway as an example, Figure 1 As shown: the user logs in to the APP on the mobile terminal, the mobile terminal connects and logs in to the application server through the HTTP protocol, and sends a control message to the application server, the message format is such as {gateway ID, OPTION, param}; the application server parses the message, determines the validity of the message, and distributes the message to the gateway server according to the request; the gateway server receives the message distributed by the application server, parses the meaning of the message, and sends the operation message to the corresponding gateway, such as querying whether the corresponding gateway is online according to the gateway ID, querying the gateway connection channel according to the login status, etc., and sending the relevant message to the relevant gateway through the connection channel; the gateway receives the message sent by the gateway server, parses the message and executes it, and sends the execution result, return status, etc. to the gateway server; the gateway server receives the execution result, return status, etc. sent by the gateway and returns it to the application server, and the application server returns the received execution result, return status, etc. to the corresponding mobile terminal, and the user views the execution result, return status, etc. after the gateway executes the operation through the APP on the mobile terminal.
[0027] In an embodiment of the present application, the gateway and the gateway server use the CoAP protocol to exchange information. To ensure that the user responds promptly to the gateway operation, the gateway and the gateway server remain connected. Optionally, the gateway and the gateway server maintain a long connection state or a short connection state. The connection mode between the gateway and the gateway server includes UDP, DTLS, TLS and TCP connection, and then the gateway and the gateway server can be connected through UDP, DTLS, TLS or TCP. Among them, UDP is an unreliable connection, DTLS is an encrypted unreliable connection, TCP is a reliable connection, TLS is an encrypted reliable connection, UDP and DTLS are short connections, and TLS and TCP are long connections. The so-called long connection means that multiple data packets can be sent continuously on a connection. During the connection maintenance period, if no data packet is sent, the two ends of the connection need to send a detection packet to maintain the connection. Then when the two ends require a long connection but there is no data communication, it is necessary to send data packets regularly to maintain the connection state. The so-called short connection is to establish a connection when the two communicating parties have data interaction, and disconnect the connection after the data is sent. Short connections are more convenient to manage and do not require additional control.
[0028] As the number of gateways in the Internet of Things continues to increase, the connection between the gateway and the gateway server occupies a large amount of resources of the gateway server and the network. Although short connections are more convenient to manage, the large-scale use of short connections cannot ensure the reliability of the connection between the gateway and the gateway server. For example, it may be difficult to maintain the reply channel, resulting in the failure of the sent instructions to be executed in time. However, if a large number of long connections are used, a large amount of resources of the gateway server and the network will be occupied, reducing the connection performance between the gateway and the gateway server.
[0029] In the embodiment of the present application, the gateway server may include one server or may include several servers, and each server cooperates with each other to realize the connection and operation of the gateway through distributed computing. For example, the gateway server includes two servers (gateway server A and gateway server B), and gateway server A and gateway server B cooperate with each other to realize the connection and operation of the gateway through distributed computing.
[0030] In order to reduce the occupation of gateway servers and network resources and ensure the connection performance between the gateway and the gateway server, the embodiment of the present application provides an Internet of Things connection method. The Internet of Things connection method provided by the embodiment of the present application is described in detail below in combination with the specific form of the gateway server.
[0031] Attached Figure 2 Another timing diagram of the Internet of Things connection is provided for the embodiment of the present application, Figure 3 The present invention provides a flow chart of an Internet of Things connection method. Figure 2 and Figure 3As shown, the gateway server in this embodiment includes a server, and the gateway server completes the information interaction and data processing between the application server and the gateway.
[0032] For ease of description, a user in a logged-in state is referred to as a target user, the user's ID is referred to as a target user ID, the gateway bound to the user is referred to as a target gateway, and the ID of the target gateway is referred to as a target gateway ID.
[0033] The Internet of Things connection method provided in the embodiment of the present application is applied to a gateway server, including:
[0034] S100: receiving a short connection heartbeat keep-alive message sent by a target gateway, wherein the target gateway in a short connection state with the gateway server sends a short connection heartbeat keep-alive message to the gateway server, and the target gateway is the gateway that the target user needs to configure.
[0035] In an embodiment of the present application, when the target gateway is in a short connection state with the gateway server, the target gateway needs to actively send a short connection heartbeat keep-alive message to the gateway server to inform the gateway server that it is still online. The gateway server receives the short connection heartbeat keep-alive message sent by the target gateway. If the gateway server receives the short connection heartbeat keep-alive message from the gateway, it is considered that the corresponding target gateway is still in a short connection state with the gateway server at this moment, that is, the target gateway is online.
[0036] Optionally, to ensure that the gateway server clearly knows whether the target gateway is online, the target gateway periodically sends CoAP heartbeat keepalive messages to the gateway server, where the CoAP heartbeat keepalive messages can use UDP or TCP. Specifically, when the target gateway and the gateway server are in a short connection state, the target gateway periodically sends short connection heartbeat keepalive messages, such as UDP heartbeat keepalive messages; if the gateway and the gateway server are in a long connection state, the target gateway sends a long connection heartbeat keepalive message to the gateway server, such as a TCP heartbeat keepalive message or a TLS heartbeat keepalive message. Further, if the gateway sends a UDP heartbeat keepalive message to the gateway server, the gateway server may reply to the gateway or not. If the gateway server does not reply to the gateway, the server load can be relatively increased. The interval period for the gateway to periodically send CoAP heartbeat keepalive messages to the gateway server can be 10S, etc.
[0037] In addition, in the embodiment of the present application, all gateways that are connected to the gateway server need to send a heartbeat keep-alive message to the gateway server so that the gateway server knows that the corresponding gateway is still online.
[0038] In an embodiment of the present application, the gateway server can monitor the online status of the gateway to which it is connected in real time. To facilitate the gateway server to monitor the online status of the gateway to which it is connected in real time, the gateway server receives the heartbeat keep-alive message of each gateway. If the heartbeat keep-alive message of a certain gateway is received, the online identification of the gateway is set to online in the online table item in the gateway database. The online status of the corresponding gateway can be obtained by searching the online table item, which is convenient for the gateway server to know the online status of the gateway. For example, the gateway server receives the heartbeat keep-alive message sent by the target gateway, and sets the online identification of the target gateway to online in the online table item in the gateway database. Then, when the gateway server receives the target gateway ID, it can be known whether the target gateway corresponding to the target gateway ID is online by querying the online table item according to the target gateway ID, which is convenient for the gateway server to know the online status of the target gateway in time. In addition, the gateway server monitors the message of the gateway marked as online in the online table item according to the online table item.
[0039] S200: If a target gateway ID sent by an application server is received, a reply message is sent to a target gateway corresponding to the target gateway ID according to the target gateway ID, wherein the application server obtains the target gateway ID according to the login information of the target user and sends the target gateway ID to the gateway server.
[0040] When the user opens and logs in to the corresponding APP on the mobile terminal, the mobile terminal sends the user's login information to the application server. The application server receives the user's login information, verifies the user's login information, and determines whether the user is allowed to log in. In this embodiment, the mobile terminal can log in and connect to the application server using the HTTP protocol. If the application server verifies that the user's login information meets the requirements, on the one hand, the user is allowed to successfully log in to the application server through the APP, and on the other hand, the gateway ID bound to the user ID in the login information is obtained. One user ID can be bound to several gateway IDs, that is, the user can be bound to more than one gateway. The user's login information includes the user ID and the ID of the gateway bound by the user, and then the application server can obtain the gateway ID bound to the user according to the user's login information.
[0041] Therefore, the application server receives the login information of the target user, and obtains the target user ID and the target gateway ID bound to the target user ID from the login information of the target user. When the application server obtains the target gateway ID bound to the target user ID, it sends the target gateway ID bound to the target user ID to the gateway server, and the gateway server receives the target gateway ID. In the embodiment of the present application, the application server and the gateway server can transmit data via the HTTP protocol, and then the application server uses the HTTP protocol to send a message carrying the target gateway ID to the gateway server, and the gateway server receives and parses the target gateway ID via the HTTP protocol.
[0042] If the gateway server receives the target gateway ID sent by the application server, the gateway server replies to the target gateway corresponding to the target gateway ID. The gateway server replies to the target gateway so that the subsequent message sent by the target gateway to the gateway server is a long connection message, and the connection between the target gateway and the gateway server is switched from a short connection to a long connection. The long connection in this embodiment can be a TLS connection, which is convenient for ensuring data security, but is not limited to a TLS connection, and can also be a TCP connection.
[0043] Since there may be more than one target gateway corresponding to the target user, and the target gateway may not be online, when the gateway server replies to the target gateway corresponding to the target gateway ID, it is necessary to determine the online status of the target gateway and then reply to the target gateway that is online.
[0044] In an embodiment of the present application, the gateway server replies a message to the target gateway corresponding to the target gateway ID according to the target gateway ID. Specifically, the gateway server replies a heartbeat message to the target gateway corresponding to the target gateway ID according to the target gateway ID. The heartbeat message causes the corresponding target gateway to receive the heartbeat message, and then the message sent by the target gateway to the gateway server is a long connection message. That is, after the target gateway receives the reply heartbeat message from the gateway server, the short connection between the target gateway and the gateway server is switched to a long connection, and the message from the target gateway to the gateway server will be a long connection message.
[0045] In the embodiment of the present application, the reply content in the heartbeat message that the gateway server replies to the target gateway corresponding to the target gateway ID according to the target gateway ID can be empty, which can reduce the bandwidth occupied by the gateway server replying the heartbeat message to the target gateway corresponding to the target gateway ID. However, if the heartbeat keep-alive message sent by the target gateway needs to be replied, then when the gateway server replies to the target gateway with the heartbeat message, the ACK message of the reply content needs to add content.
[0046] In an embodiment of the present application, the target gateway receives a reply message from the application server, sends a request to establish a persistent connection to the gateway server, the gateway server receives the request to establish a persistent connection sent by the target gateway, and the gateway server completes the persistent connection with the target gateway based on the request to establish a persistent connection sent by the target gateway.
[0047] In the implementation of this application, if the gateway server requests the target gateway to establish a persistent connection by replying to a UDP message, when the target gateway receives the reply UDP message from the gateway server, the target gateway requests the gateway server to establish a persistent connection, that is, initiates a persistent connection with the gateway server.
[0048] S300: Receive a long connection message sent by the target gateway.
[0049] After the target gateway receives the reply message sent by the gateway server, the message sent by the target gateway to the gateway server is a persistent connection message, and the gateway server receives the persistent connection message sent by the target gateway. For example, the target gateway sends a persistent connection heartbeat keep-alive message to the gateway server.
[0050] Furthermore, after the target gateway establishes a persistent connection with the gateway server, the gateway server notifies the application server that the target gateway bound to the target user has established a persistent connection with the gateway server, and the application server notifies the mobile terminal that the target gateway bound to the target user has established a persistent connection with the gateway server. The target user's bound target gateway has established a persistent connection with the gateway server and is ready to initiate an operation, and then the user can control the APP to send a control message to the application server.
[0051] If the user controls the APP of the mobile terminal to send a control message to the application server, the application server parses the message, determines the validity of the message, and sends the message to the gateway server according to the request; the gateway server receives the message distributed by the application server, parses the meaning of the message, and sends the operation message to the target gateway; the target gateway receives the message sent by the gateway server, parses the message and executes it, and sends the execution result, return status, etc. to the gateway server; the gateway server receives the execution result, return status, etc. sent by the target gateway and returns it to the application server, and the application server returns the received execution result, return status, etc. to the mobile terminal of the corresponding target user, and the user views the execution result, return status, etc. after the gateway executes the operation through the APP on the mobile terminal.
[0052] In the Internet of Things connection method provided in the embodiment of the present application, switching between short connection and long connection is implemented between the gateway server and the gateway. When the target user logs in to the application server, the gateway server establishes a long connection with the target gateway. Then, when the target user does not log in to the application server, the gateway server releases the long connection with the target gateway, thereby avoiding maintaining the long connection state between the gateway server and the target gateway to occupy the gateway server and network resources, releasing the idle resources of the gateway server, and facilitating the rational use of the gateway server resources.
[0053] The Internet of Things connection method provided in the embodiment of the present application also includes: if a notification that the target user corresponding to the target gateway is offline sent by the application server, releasing the long connection with the target gateway and establishing a short connection with the target gateway.
[0054] If the target user of the mobile terminal logs out, the application server will be notified of the target user's logout at the time of logout. When the application server receives the target user's logout notification, the application server will send the target user offline notification to the gateway server. The gateway server receives the target user offline notification sent by the application server, and releases the long connection with the target gateway corresponding to the target user according to the target user offline notification, and switches the long connection between the target gateway and the gateway server to a short connection. In this example, the gateway server actively switches the long connection between the target gateway and the gateway server to a short connection, avoiding the target gateway and the gateway server from occupying too many network resources in a long connection state, releasing the idle resources of the gateway server, and facilitating the rational use of the gateway server resources.
[0055] Optionally, if the gateway server receives a target user offline notification sent by the application server, it releases the TLS connection with the target gateway corresponding to the target user according to the target user offline notification and switches to a UDP connection. The gateway terminates the TLS connection with the gateway server and establishes a UDP connection with the gateway server. The heartbeat keepalive message used for keeping the user alive is switched from a TLS heartbeat keepalive message to a UDP heartbeat keepalive message.
[0056] The Internet of Things connection method provided by an embodiment of the present application also includes: receiving a disconnection request for a long connection sent by the target gateway, wherein if the target gateway does not receive an operation instruction within a first threshold time, the target gateway sends a disconnection request for a long connection to the gateway server; according to the disconnection request for a long connection, disconnecting the long connection with the target gateway and establishing a short connection with the target gateway.
[0057] If the target gateway does not receive the operation instruction within the first threshold time, it is considered that the target user on the mobile terminal has gone offline or the target user has switched to the background, and the target gateway will send a disconnection request to the gateway server to enable the target gateway to actively disconnect the long connection with the gateway server. The gateway server receives the disconnection request sent by the target gateway, and the gateway server disconnects the long connection with the target gateway according to the disconnection request, establishes a short connection with the target gateway, releases the idle resources of the gateway server, and facilitates the reasonable use of the gateway server resources.
[0058] For example, assuming that the first threshold time is 10 minutes, when the target gateway does not receive an operation instruction within 10 minutes, the target gateway sends a disconnection request to the gateway server, and the gateway server receives the disconnection request sent by the target gateway and disconnects the target gateway's long connection according to the disconnection request, and establishes a short connection with the target gateway. Optionally, a timer is set inside the gateway, starting from the receipt of the last operation instruction of the APP; if the next operation instruction is not received when the first threshold time is reached, it is considered that the operation instruction has not been received within the first threshold time; if the next operation instruction is received when the first threshold time is reached, the timer is restarted.
[0059] In the embodiment of the present application, the first threshold time can be set as needed and is not limited to 10 minutes. Accordingly, in the embodiment of the present application, it can be set that if the user of the mobile terminal operates the APP within the first threshold time, the APP will log out, and if the user continues to operate the APP, the APP needs to log in again.
[0060] The Internet of Things connection method provided by the embodiment of the present application also includes: if the long connection heartbeat keep-alive message sent by the target gateway is not received within the second threshold time, releasing the long connection with the target gateway and establishing a short connection with the target gateway.
[0061] In an embodiment of the present application, when the target gateway and the gateway server are in a long connection state, the target gateway should periodically send a long connection heartbeat keep-alive message to the gateway server. If the gateway server does not receive the long connection heartbeat keep-alive message sent by the target gateway to the gateway server within several consecutive cycles, the gateway may be offline. Therefore, in this implementation, a second threshold time is set. If the gateway server does not receive the long connection heartbeat keep-alive message sent by the target gateway within the second threshold time, the gateway server releases the long connection with the target gateway, releases idle resources, and facilitates the rational use of gateway server resources. Optionally, if the gateway server can also establish a short connection with the target gateway, the gateway server establishes a short connection with the target gateway.
[0062] Based on the IoT connection method provided in the embodiment of the present application, the embodiment of the present application further provides a gateway server. The gateway server provided in the embodiment of the present application is configured as:
[0063] Receiving a short connection heartbeat keep-alive message sent by a target gateway, wherein the target gateway in a short connection state with the gateway server sends a short connection heartbeat keep-alive message to the gateway server, and the target gateway is the gateway that the target user needs to configure;
[0064] If the target gateway ID sent by the application server is received, a message is replied to the target gateway corresponding to the target gateway ID according to the target gateway ID, wherein the application server obtains the target gateway ID according to the login information of the target user and sends the target gateway ID to the gateway server;
[0065] Receive a persistent connection message sent by the target gateway.
[0066] Attached Figure 4 Another timing diagram of Internet of Things connection is provided for the embodiment of the present application. Figure 4 As shown, the gateway server in this embodiment includes two servers (gateway server A and gateway server B), and gateway server A and gateway server B cooperate to complete information exchange and data processing between the application server and the gateway. The distributed computing of the gateway server is realized by the cooperation of gateway server A and gateway server B, which can be applied when the number of users increases, such as millions of users.
[0067] Optionally, gateway server A is responsible for processing the heartbeat keep-alive message initiated by the gateway and notifying gateway server B to initiate a long connection with the gateway. Gateway server B is responsible for maintaining the long connection with the gateway. Gateway server A is responsible for short-connection gateway and processing gateway keep-alive, and is the server that maintains the connection; gateway server B maintains gateway login, long connection and data exchange, and is the server that handles the business.
[0068] Specific:
[0069] When the user opens and logs in to the corresponding APP on the mobile terminal, the mobile terminal sends the target user's login information to the application server, and the application server receives the target user's login information, verifies the target user's login information, and determines whether the target user is allowed to log in. In this embodiment, the mobile terminal can log in and connect to the application server using the HTTP protocol. If the application server verifies that the target user's login information meets the requirements, on the one hand, it allows the user to successfully log in to the application server through the APP, and on the other hand, obtains the gateway ID bound to the target user ID in the login information, and then sends the target gateway ID bound to the target user ID to the gateway server A.
[0070] Gateway server A receives the target gateway ID, and gateway server A receives the heartbeat keep-alive message sent by the target gateway and other gateways. If gateway server A receives the heartbeat keep-alive message from the target gateway, it is considered that the corresponding target gateway is online at this moment. Gateway server A replies to the corresponding target gateway according to the target gateway ID, so that the corresponding target gateway establishes a long connection with gateway server B, such as the target gateway sends a long connection keep-alive heartbeat, operation reply result, etc. to gateway server B.
[0071] The target gateway receives the reply message from gateway server A, and sends a request to establish a persistent connection to gateway server B according to the received reply message. Gateway server B receives the request to establish a persistent connection sent by the target gateway, and then completes the establishment of a persistent connection between gateway server B and the target gateway. The target gateway sends a persistent connection keepalive heartbeat, a persistent connection operation reply result, etc. to gateway server B.
[0072] When gateway server B receives the request to establish a persistent connection sent by the target gateway and establishes a persistent connection with the target gateway, gateway server B notifies the application server that the target gateway bound to the target user has established a persistent connection with gateway server B. The application server notifies the mobile terminal that the target gateway bound to the target user has established a persistent connection with gateway server B. The target user's bound target gateway has established a persistent connection with gateway server B and is ready to initiate an operation. Then the user can control the APP to send a control message to the application server, and the control message will reach the target gateway via the application server and gateway server B. At the same time, gateway server B synchronizes messages to gateway server A, and gateway server A stops monitoring the target gateway's request to establish a persistent connection, releasing gateway server A's monitoring of the target gateway, which can avoid gateway server A repeatedly sending requests to establish a persistent connection to the target gateway.
[0073] When gateway server B disconnects the persistent connection with the target gateway, gateway server B sends a notification to gateway server A that the persistent connection between gateway server B and the target gateway has been disconnected. Gateway server A continues to monitor the target gateway and resumes receiving short connection heartbeat keep-alive messages sent by the target gateway. At the same time, gateway server B sends a notification to the application server that the persistent connection between gateway server B and the target gateway has been disconnected, informing the target gateway bound to the target user that the persistent connection with gateway server B has been disconnected.
[0074] In this embodiment, when the gateway server establishes a long connection with the target gateway, the target address specifies gateway server B, and the URLs of the target addresses of gateway server B and gateway server A are different. The target gateway establishes a long connection with gateway server B, and gateway server B begins to process the connection service of the target gateway. According to the network model of the Internet of Things product, the gateway is in a short connection (keep-alive) state for more than 99% of the time, and is in a long connection (processing specific business) state for less than 1% of the time. Therefore, for the gateway server, the CPU and I / O overhead of processing a business behavior alone is a hundred times the overhead of processing the keep-alive message, and then using two servers helps to balance the load. And calculated from the hardware cost, the cost of doubling the server performance is much greater than maintaining two identical servers. From a business perspective, the separation of connection and business is more convenient for system maintenance.
[0075] Based on the IoT connection method provided in the embodiment of the present application, the embodiment of the present application also provides a gateway. The gateway provided in the embodiment of the present application is configured to: send a short connection heartbeat keep-alive message to the gateway server;
[0076] If a reply message from the gateway server is received, when a message needs to be sent to the gateway server, a long connection message is sent to the gateway server.
[0077] In the gateway provided by the present application, when the gateway and the gateway server are in a short connection state, the gateway sends a short connection heartbeat keep-alive message to the gateway server; when the gateway and the gateway server are in a long connection state, the gateway sends a long connection heartbeat keep-alive message to the gateway server. If the gateway receives a reply message from the gateway server, the heartbeat keep-alive message sent by the gateway to the gateway server is converted from a short connection keep-alive message to a long connection heartbeat keep-alive message.
[0078] Optionally, in the gateway provided in the embodiment of the present application, if a reply message from the gateway server is received, the gateway sends a persistent connection request to the gateway server. When the gateway establishes a persistent connection with the gateway server, the message sent by the gateway to the gateway server is a persistent connection message.
[0079] Finally, it should be noted that: this embodiment is described in a progressive manner, and different parts can refer to each other; in addition, the above embodiments are only used to illustrate the technical solution of the present invention, rather than to limit it; although the present invention has been described in detail with reference to the aforementioned embodiments, ordinary technicians in this field should understand that: it is still possible to modify the technical solutions recorded in the aforementioned embodiments, or to replace some of the technical features therein with equivalents; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. An Internet of Things connection method, applied to a gateway server, characterized in that: The gateway server is communicatively connected to a plurality of gateways and is communicatively connected to an application server, and the gateway is communicatively connected to a downstream device; The mobile terminal is communicatively connected to the application server, the mobile terminal is not directly communicatively connected to the gateway and the gateway server, the mobile terminal is communicatively connected to the gateway server through the application server and is communicatively connected to the gateway through the gateway server; The method comprises: Receive a short connection heartbeat keep-alive message sent by the target gateway, and set the online flag of the target gateway in the online table entry to online; wherein the target gateway in a short connection state with the gateway server sends a short connection heartbeat keep-alive message to the gateway server, and the target gateway is a gateway that the target user needs to configure and is a gateway for communication connection with the gateway server; If the target gateway ID sent by the application server is received, query whether the online identifier of the target gateway corresponding to the target gateway ID is online; If the online identifier of the target gateway corresponding to the target gateway ID is online, a reply message is sent to the target gateway corresponding to the target gateway ID according to the target gateway ID; wherein the application server obtains the target gateway ID according to the login information of the target user and sends the target gateway ID to the gateway server, and the reply message is used to enable the target gateway to send a message to the gateway server to establish a long connection; Receiving a persistent connection message sent by the target gateway to establish a persistent connection with the target gateway; If a notification of offline of the target user corresponding to the target gateway sent by the application server is received, the long connection with the target gateway is released, and a short connection with the target gateway is established; If the long connection heartbeat keep-alive message sent by the target gateway is not received within the second threshold time, the long connection with the target gateway is released and a short connection with the target gateway is established; If a disconnect long connection request sent by the target gateway is received, a short connection with the target gateway is established; wherein, if the target gateway does not receive an operation instruction within a first threshold time, the target gateway sends a disconnect long connection request to the gateway server.
2. The Internet of Things connection method according to claim 1, characterized in that: Replying a message to a target gateway corresponding to the target gateway ID according to the target gateway ID includes: A heartbeat message is replied to the target gateway corresponding to the target gateway ID according to the target gateway ID, and the heartbeat message enables the target gateway to send a message for establishing a long connection to the gateway server after receiving the heartbeat message.
3. The Internet of Things connection method according to claim 1, characterized in that: The long connection heartbeat keep-alive message includes a TLS heartbeat keep-alive message.
4. The Internet of Things connection method according to claim 1, characterized in that: The method further comprises: The long connection is a TLS or TCP connection, and the short connection is a UDP connection or a DTLS connection.
5. An Internet of Things connection method, applied to a gateway, characterized in that: The gateway is a target gateway that the target user needs to configure, the target gateway is in a short connection state with the gateway server, the gateway server executes the IoT connection method according to claim 1, the gateway server is communicatively connected to the application server, the gateway is communicatively connected to the downstream device, the gateway is not communicatively connected to the application server and the mobile terminal, and the application server is communicatively connected to the mobile terminal; The method comprises: Send a short connection heartbeat keep-alive message to the gateway server; If a reply message from the gateway server is received, when a message needs to be sent to the gateway server, a persistent connection message is sent to the gateway server, where the persistent connection message is used to establish a persistent connection between the target gateway and the gateway server; If no operation instruction is received within the first threshold time, a disconnection request for the long connection is sent to the gateway server; wherein the disconnection request for the long connection is used to enable the gateway server to restore the short connection of the target gateway.
6. A gateway server, characterized in that: The gateway server is communicatively connected to a plurality of gateways and is communicatively connected to an application server, and the gateway is communicatively connected to a downstream device; The mobile terminal is not in communication connection with the gateway and the gateway server, but the mobile terminal is in communication connection with the application server so as to transfer the connection to the gateway server through the application server; The gateway server comprises: The gateway management module is used to: receive a short connection heartbeat keep-alive message sent by a target gateway, and set the online flag of the target gateway in the online table item to online; wherein the target gateway in a short connection state with the gateway server sends a short connection heartbeat keep-alive message to the gateway server, and the target gateway is a gateway that the target user needs to configure and is a gateway for communication connection with the gateway server; The online identification module is used to: if a target gateway ID sent by the application server is received, query whether the online identification of the target gateway corresponding to the target gateway ID is online; A message reply module, used for: if the online identification of the target gateway corresponding to the target gateway ID is online, replying a message to the target gateway corresponding to the target gateway ID according to the target gateway ID; wherein the application server obtains the target gateway ID according to the login information of the target user and sends the target gateway ID to the gateway server, and the reply message is used to make the target gateway send a message to the gateway server to establish a long connection; The long connection control module is used to: receive a long connection message sent by the target gateway to establish a long connection with the target gateway; Short connection control module, used for: If a notification of offline of the target user corresponding to the target gateway sent by the application server is received, the long connection with the target gateway is released, and a short connection with the target gateway is established; If the long connection heartbeat keep-alive message sent by the target gateway is not received within the second threshold time, the long connection with the target gateway is released and a short connection with the target gateway is established; If a disconnect long connection request sent by the target gateway is received, a short connection with the target gateway is established; wherein, if the target gateway does not receive an operation instruction within a first threshold time, the target gateway sends a disconnect long connection request to the gateway server.
7. A gateway, characterized in that: The gateway is a target gateway that the target user needs to configure, the gateway is in a short connection state with the gateway server according to claim 6, the gateway server is in communication connection with the application server, the gateway is in communication connection with the downlink device, the gateway is not in communication connection with the application server and the mobile terminal, and the application server is in communication connection with the mobile terminal; The gateway comprises: The heartbeat message sending module is used to send a short connection heartbeat keep-alive message to the gateway server; A long connection message sending module, used for: if a reply message from the gateway server is received, when a message needs to be sent to the gateway server, sending a long connection message to the gateway server, wherein the long connection message is used for the target gateway to establish a long connection with the gateway server; The long connection disconnection module is used to: if no operation instruction is received within a first threshold time, send a disconnection request for the long connection to the gateway server; wherein the disconnection request for the long connection is used to enable the gateway server to restore the short connection of the target gateway.
