A method, system, medium and device capable of realizing ordered retransmission based on WebSocket
By using the WebSocket-based method in the IoT device management platform, combined with the identity identification number and caching mechanism, the orderly retransmission of information is achieved, which solves the problem of unreliable information transmission in the existing technology and ensures that the information is not stored in the device. Reliable transmission during online or network fluctuations, suitable for scenarios with small system scale.
Patent Information
- Application Number
- CN202111157116.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-30
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2041-09-30
AI Technical Summary
In the IoT device management platform, since the system itself is small and does not use message queues, the existing technology lacks a lightweight tool to achieve reliable and orderly sending and receiving of information, especially in the face of network problems or device shutdown. In other cases, the information may not be received by the device.
Using a method based on WebSocket, the information to be sent is encapsulated with an identity number plus a user ID, interface name ID and timestamp, and an identity token is attached when the client establishes a connection to achieve asynchronous and synchronous message sending, combined with caching The mechanism performs orderly retransmission to ensure reliable transmission of information.
When the system is small and the message queue cannot be used, reliable and orderly sending and receiving of information is achieved, ensuring that information can be successfully transmitted when the network fluctuates or the device is not online, and improves the stability and reliability of the system.
Smart Images

Figure CN113835908B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Java Web technology, and in particular to a method, system, medium and electronic device capable of realizing an ordered retransmission mechanism based on WebSocket. Background Art
[0002] For an IoT device management platform, since two-way communication is required between the platform and the device, but the system itself is not large, there is no need to use a message queue. Due to various reasons such as network problems or device shutdown, some information may not be received by the device after being sent once.
[0003] However, under the premise that the system itself is not large in size and does not use a message queue, there is no suitable relatively lightweight tool in the prior art to achieve reliable and orderly sending and receiving of information. Summary of the invention
[0004] In view of at least one defect or improvement requirement of the prior art mentioned in the background technology, the present invention provides a method for realizing an ordered retransmission mechanism based on WebSocket, comprising the following steps:
[0005] S1. The information to be sent is encapsulated with an identity number including a user identifier, an interface name identifier and a timestamp, and the encapsulated information to be sent is put into a cache, and then the information to be sent carrying the identity number is sent to the client via an asynchronous message sending method;
[0006] S2. When the information to be sent is not successfully sent to the client, the information to be sent is retained in the cache;
[0007] S3. When the client can receive information normally, based on the identity number of the information to be sent in the cache, sorting according to the timestamp and removing expired messages, the information to be sent is sent to the client by synchronously sending a message;
[0008] S4. If the method of synchronously sending messages fails to send the information to be sent to the client, the task in this round will no longer send the information to be sent to the current client until the current client can receive the information normally; if the method of synchronously sending messages succeeds in sending the information to be sent to the client, the next information to be sent will continue to be sent until all the information to be sent is sent to the client.
[0009] According to the method for realizing an ordered retransmission mechanism based on WebSocket provided by the present invention, the following steps are also included before encapsulating the information to be sent with an identity identification number including a user identification, an interface name identification and a timestamp:
[0010] S01. When the client requests to establish a WebSocket connection, the client's own identity token is attached when requesting a connection;
[0011] S02. After receiving the WebSocket connection request, the identity token in the request information is parsed. If it is confirmed to be a legal token, the user identifier containing the client identity information is parsed, the current connection is accepted and the user identifier is bound to the current connection; if it is confirmed to be an illegal token, the current connection is directly closed.
[0012] The method for realizing an ordered retransmission mechanism based on WebSocket provided by the present invention further includes the following steps:
[0013] S03. When the ordered retransmission mechanism is not adopted, the information to be sent is directly sent by a synchronous or asynchronous message sending method; if the user identifier is passed in, the information to be sent is sent to the client corresponding to the user identifier; if the user identifier is not passed in, the group sending mechanism is used to send the information to be sent to all clients.
[0014] The method for realizing an ordered retransmission mechanism based on WebSocket provided by the present invention further includes the following steps:
[0015] S21. When the information to be sent is successfully sent to the client, the information to be sent in the cache is deleted.
[0016] According to the WebSocket-based method for implementing an ordered retransmission mechanism provided by the present invention, the identity token is issued by the server to the client when the client logs in to the server.
[0017] The present invention also provides a system based on WebSocket that can realize an ordered retransmission mechanism, comprising the following modules:
[0018] Encapsulation module: used to encapsulate the information to be sent with an identity number including a user identifier, an interface name identifier and a timestamp, put the encapsulated information to be sent into a cache, and then send the information to be sent carrying the identity number to the client through an asynchronous message sending method;
[0019] Cache module: used for retaining the information to be sent in the cache if the information to be sent is not successfully sent to the client;
[0020] Ordered sending module: used for sending the information to be sent to the client through a synchronous message sending method based on the identity identification number of the information to be sent in the cache, sorting according to the timestamp and eliminating expired messages when the client can receive information normally;
[0021] Resending module: if the sending of the information to be sent to the client through the synchronous message sending method fails, then this round of task will no longer send the information to be sent to the current client until the current client can receive the information normally; if the sending of the information to be sent to the client through the synchronous message sending method is successful, then continue to send the next piece of information to be sent until all the information to be sent is sent to the client.
[0022] The system for realizing the ordered retransmission mechanism based on WebSocket provided by the present invention also includes the following modules:
[0023] Connection module: used for attaching the identity token of the client itself when requesting to establish a WebSocket connection by the client;
[0024] Confirmation module: used to parse the identity token in the request information after receiving the WebSocket connection request. If it is confirmed to be a legal token, parse out the user ID containing the client identity information, accept the current connection and bind the user ID to the current connection; if it is confirmed to be an illegal token, directly close the current connection.
[0025] The system for realizing the ordered retransmission mechanism based on WebSocket provided by the present invention also includes the following modules:
[0026] Non-ordered retransmission sending module: when the ordered retransmission mechanism is not adopted, the information to be sent is directly sent by synchronous or asynchronous message sending method; if the user identifier is passed in, the information to be sent is sent to the client corresponding to the user identifier; if the user identifier is not passed in, the group sending mechanism is used to send the information to be sent to all clients.
[0027] The present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, wherein the computer program can implement the steps of the aforementioned method when executed by a processor.
[0028] The present invention further provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor can implement the steps of the aforementioned method when executing the program.
[0029] The present invention provides a method, system, medium and electronic device that can realize an orderly retransmission mechanism based on WebSocket. By making a WebSocket service implementation class inherit a service abstract class, a connection success calling method and a message receiving calling method are realized. Ultimately, reliable and orderly sending and receiving of information can be realized in relatively lightweight application scenarios where the system scale is small and it is not necessary or impossible to use a message queue service. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0031] Figure 1 It is a flowchart of the WebSocket-based orderly retransmission mechanism of the present invention;
[0032] Figure 2 It is a flow chart of a method for realizing an ordered retransmission mechanism based on WebSocket of the present invention. DETAILED DESCRIPTION
[0033] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0034] like Figure 1 and Figure 2 As shown, in one embodiment, a method for implementing an ordered retransmission mechanism based on WebSocket of the present invention includes steps S1-S4:
[0035] S1. Encapsulate the information to be sent with an identity number including a user identifier, an interface name identifier and a timestamp, as shown in Table 1 below, and put the encapsulated information to be sent into a cache. Then, the information to be sent carrying the unique identity number is sent to the client through an asynchronous message sending method.
[0036] userId User ID interfaceName Interface name identifier timeStamp Timestamp msg Messages to be sent
[0037] Table 1
[0038] S2. If the client receives the information to be sent, it will send a successful reception message to the server, and the server will delete the information to be sent in the cache; if the client does not receive the information to be sent, it will not send a successful reception message to the server, and the unreceived information to be sent will remain in the cache.
[0039] The reason why the client did not receive the information to be sent may be that the client is offline or because of network failure. There are two trigger points for resending, one is that the server receives the client's online notification, and the other is that the scheduled task executes the resending task.
[0040] S3. When the client can receive information normally, that is, when the client is online or the network returns to normal, based on the identity identification number of the information to be sent in the cache, the information is sorted according to the timestamp and expired messages are removed, and the information to be sent is sent to the client through the synchronous message sending method.
[0041] S4. If the information to be sent fails to be sent to the client through the synchronous message sending method, this round of tasks will no longer send the information to be sent to the current client until the current client can receive information normally again; if the information to be sent is successfully sent to the client through the synchronous message sending method, the next information to be sent will continue to be sent until all the information to be sent is sent to the client.
[0042] When the server receives the successful reception feedback message from the client, it removes this information from the cache, otherwise it continues the above resending process.
[0043] In one embodiment, before encapsulating the information to be sent with the identity identification number including the user identification, the interface name identification and the timestamp, the following steps are also included:
[0044] S01. The client requests to establish a WebSocket connection with the server and attaches its own identity token when requesting the connection; the identity token is sent by the server to the client when the client logs in to the server.
[0045] S02. After receiving the WebSocket connection request, the server parses the identity token in the request information. If it is confirmed to be a legal token, it parses out the user ID containing the client's identity information, accepts the current connection and binds the user ID to the current connection; if it is confirmed to be an illegal token, it directly closes the current connection.
[0046] Preferably, the method for implementing an ordered retransmission mechanism based on WebSocket further includes the following steps:
[0047] S03. When the ordered retransmission mechanism is not adopted, the information to be sent is directly sent by synchronous or asynchronous message sending method. If the user ID is passed in, the information to be sent is sent to the client corresponding to the user ID; if the user ID is not passed in, the group sending mechanism is adopted to send the information to be sent to all clients.
[0048] In one embodiment, preferably, functions such as encapsulating the information to be sent with an identity number, sending the information to be sent in order based on the timestamp sorting in the identity number, binding the user identifier with the current connection and managing and sending the information to be sent using synchronous or asynchronous message sending methods can be implemented by a manager in the server. Specifically, preferably, the manager can be designed to include four components: a service class interface, a service abstract class, a management class interface, and a general management class.
[0049] Among them, the service class interface can define the four basic methods required by a WebSocket service and define other corresponding methods for collaboration with the general management class. Other corresponding methods for collaboration with the general management class are defined in the service class interface, and the implementers are the service abstract class and the service implementation class. The implementer of the methods defined in the management class interface is the general management class. Collaboration is mentioned here because the specific implementation of various information sending methods implemented in the general management class is to call the methods of the service implementation class.
[0050] The service abstract class cannot be instantiated, but can be inherited by the WebSocket service implementation class. The instance of the management class is injected into the properties of the service abstract class, and the injection process is implemented in the constructor. Except for open and getMessage, all methods in the service class interface are implemented in the service abstract class. The purpose of this is to allow the client to implement its own initialization process (such as token parsing, interface name initialization, etc.) and process received information when the connection is established.
[0051] The management interface can define methods for sending messages synchronously, asynchronously, and with a retransmission mechanism. It can also define methods for managing connections, adding connections, removing connections, and counting the current number of connections.
[0052] Among them, the management class can implement the management class interface and inject its own instance as an attribute into the WebSocket service implementation class. The instance of the management class is a singleton mode. There is a mapping in the attributes of the management class, and the key value is defined as "interface name" + "user name", and the value is defined as a WebSocket connection. TIME_RANGE is defined as the time that the information to be resent needs to be retained.
[0053] The existing technology is the implementation of the WebSocket protocol. The combination design of the above four preferred components is a further encapsulation of this implementation. Its advantage is that multiple WebSocket services can be created quickly and easily. Because the common methods extracted in the abstract class have been implemented, the implementation class does not need to repeat the operation; further, all service-related sessions are managed uniformly, and it is very convenient to send single or group messages to the managed sessions, and this system has added an orderly retransmission mechanism. If some existing related technologies are used, it will result in all methods being implemented once for each server, and session management must be done, the work is repeated and there is no unified management center, which is also easy to cause confusion in use.
[0054] In one embodiment, the present invention further provides a system based on WebSocket that can implement an ordered retransmission mechanism, comprising the following modules:
[0055] Encapsulation module: used to encapsulate the information to be sent with an identity number including a user identifier, an interface name identifier and a timestamp, put the encapsulated information to be sent into a cache, and then send the information to be sent carrying the identity number to the client through an asynchronous message sending method;
[0056] Cache module: if the client receives the information to be sent, it will send a successful reception message to the server, and the server will delete the information to be sent in the cache; if the client does not receive the information to be sent, it will not send a successful reception message to the server, and the information to be sent that has not been received will be retained in the cache;
[0057] Ordered sending module: used for sending the information to be sent to the client through a synchronous message sending method based on the identity identification number of the information to be sent in the cache, sorting according to the timestamp and eliminating expired messages when the client can receive information normally;
[0058] Resending module: if the sending of the information to be sent to the client through the synchronous message sending method fails, then this round of task will no longer send the information to be sent to the current client until the current client can receive the information normally; if the sending of the information to be sent to the client through the synchronous message sending method is successful, then continue to send the next piece of information to be sent until all the information to be sent is sent to the client.
[0059] In one embodiment, the system capable of implementing an ordered retransmission mechanism based on WebSocket further includes the following modules:
[0060] Connection module: used for the client to request to establish a WebSocket connection with the server, and attach its own identity token when requesting a connection;
[0061] Confirmation module: used to parse the identity token in the request information after the server receives the WebSocket connection request. If it is confirmed to be a legal token, the user identifier containing the client identity information is parsed, the current connection is accepted and the user identifier is bound to the current connection; if it is confirmed to be an illegal token, the current connection is directly closed.
[0062] Preferably, the system capable of implementing an ordered retransmission mechanism based on WebSocket further includes the following modules:
[0063] Non-ordered retransmission sending module: when the ordered retransmission mechanism is not adopted, the information to be sent is directly sent by synchronous or asynchronous message sending method; if the user identifier is passed in, the information to be sent is sent to the client corresponding to the user identifier; if the user identifier is not passed in, the group sending mechanism is used to send the information to be sent to all clients.
[0064] In one embodiment, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, wherein the computer program can implement the steps of any of the aforementioned methods when executed by a processor.
[0065] In one embodiment, the present invention further provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor can implement the steps of any of the aforementioned methods when executing the program.
[0066] It will be easily understood by those skilled in the art that the above are only some preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for realizing an ordered retransmission mechanism based on WebSocket, characterized in that: The steps include: S1. The information to be sent is encapsulated with an identity number including a user identifier, an interface name identifier and a timestamp, and the encapsulated information to be sent is put into a cache, and then the information to be sent carrying the identity number is sent to the client via an asynchronous message sending method; S2. When the information to be sent is not successfully sent to the client, the information to be sent is retained in the cache; S3. When the client can receive information normally, based on the identity number of the information to be sent in the cache, sorting according to the timestamp and removing expired messages, the information to be sent is sent to the client by synchronously sending a message; S4. If the method of synchronously sending a message fails to send the information to be sent to the client, the task will no longer send the information to be sent to the current client until the current client can receive the information normally; If the information to be sent is successfully sent to the client through the synchronous message sending method, continue to send the next piece of information to be sent until all the information to be sent is sent to the client; The method for realizing an ordered retransmission mechanism based on WebSocket is implemented by a manager in the server, and the manager includes four components: a service class interface, a service abstract class, a management class interface, and a management general class; Among them, the implementers of the methods defined in the service class interface that collaborate with the management class are the service abstract class and the service implementation class, and the implementers of the methods defined in the management class interface are the management class; the WebSocket service implementation class inherits the service abstract class, and injects an instance of the management class into the properties of the service abstract class; there is a mapping in the properties of the management class, defining the key value as the interface name + user name, and the value as a WebSocket connection.
2. The method for realizing an ordered retransmission mechanism based on WebSocket according to claim 1, characterized in that: Before encapsulating the information to be sent with the identity identification number including the user identification, the interface name identification and the timestamp, the following steps are also included: S01. When the client requests to establish a WebSocket connection, the client's own identity token is attached when requesting a connection; S02. After receiving the WebSocket connection request, the identity token in the request information is parsed. If it is confirmed to be a legal token, the user ID containing the client identity information is parsed, the current connection is accepted and the user ID is bound to the current connection; If it is confirmed that the token is illegal, the current connection is closed directly.
3. The method for realizing an ordered retransmission mechanism based on WebSocket according to claim 1, characterized in that: The following steps are also included: S03. When the ordered retransmission mechanism is not adopted, the information to be sent is directly sent by a synchronous or asynchronous message sending method; if the user identifier is passed in, the information to be sent is sent to the client corresponding to the user identifier; if the user identifier is not passed in, the group sending mechanism is used to send the information to be sent to all clients.
4. The method for realizing an ordered retransmission mechanism based on WebSocket according to claim 1, characterized in that: The following steps are also included: S21. When the information to be sent is successfully sent to the client, the information to be sent in the cache is deleted.
5. The method for realizing an ordered retransmission mechanism based on WebSocket according to claim 2, characterized in that: The identity token is issued by the server to the client when the client logs in to the server.
6. A system based on WebSocket that can implement an ordered retransmission mechanism, characterized in that: Includes the following modules: Encapsulation module: used to encapsulate the information to be sent with an identity number including a user identifier, an interface name identifier and a timestamp, put the encapsulated information to be sent into a cache, and then send the information to be sent carrying the identity number to the client through an asynchronous message sending method; Cache module: used for retaining the information to be sent in the cache if the information to be sent is not successfully sent to the client; Ordered sending module: used for sending the information to be sent to the client through a synchronous message sending method based on the identity identification number of the information to be sent in the cache, sorting according to the timestamp and eliminating expired messages when the client can receive information normally; Resending module: if the sending of the to-be-sent information to the client fails by the synchronous message sending method, then the task in this round will not send the to-be-sent information to the current client until the current client can receive the information normally; If the information to be sent is successfully sent to the client through the synchronous message sending method, continue to send the next piece of information to be sent until all the information to be sent is sent to the client; The function of the system capable of realizing an ordered retransmission mechanism based on WebSocket is realized by a manager in the server, and the manager includes four components: a service class interface, a service abstract class, a management class interface, and a management general class; Among them, the implementers of the methods defined in the service class interface that collaborate with the management class are the service abstract class and the service implementation class, and the implementers of the methods defined in the management class interface are the management class; the WebSocket service implementation class inherits the service abstract class, and injects an instance of the management class into the properties of the service abstract class; there is a mapping in the properties of the management class, defining the key value as the interface name + user name, and the value as a WebSocket connection.
7. The system capable of implementing an ordered retransmission mechanism based on WebSocket according to claim 6, characterized in that: Also includes the following modules: Connection module: used for attaching the identity token of the client itself when requesting to establish a WebSocket connection by the client; Confirmation module: used to parse the identity token in the request information after receiving the WebSocket connection request. If it is confirmed to be a legal token, parse out the user ID containing the client identity information, accept the current connection and bind the user ID to the current connection; If it is confirmed that the token is illegal, the current connection is closed directly.
8. The system capable of implementing an ordered retransmission mechanism based on WebSocket according to claim 6, characterized in that: Also includes the following modules: Non-ordered retransmission sending module: when the ordered retransmission mechanism is not adopted, the information to be sent is directly sent by synchronous or asynchronous message sending method; if the user identifier is passed in, the information to be sent is sent to the client corresponding to the user identifier; if the user identifier is not passed in, the group sending mechanism is used to send the information to be sent to all clients.
9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.
10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the steps of the method according to any one of claims 1 to 5 are implemented.
