Message processing method, device and electronic device
By establishing a message mapping table in the terminal device to store pre-stored data, the problem of low efficiency in reply message processing in the WebSocket communication protocol is solved, and the effect of efficient reply message processing is achieved.
Patent Information
- Application Number
- CN202211641471.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-20
- Publication Date
- 2025-09-23
- Estimated Expiration
- 2042-12-20
AI Technical Summary
When using the WebSocket communication protocol, the terminal device cannot obtain the relevant data of the sent message corresponding to the reply message, resulting in querying all sent messages, affecting the performance of the network communication system, wasting system resources, and reducing the efficiency of reply message processing.
By establishing a message mapping table in the terminal device, pre-stored data of sent messages is stored, and the message identifier is used to obtain the pre-stored data to process the reply message, thereby avoiding re-querying the sent message.
It reduces the computing resource consumption of terminal devices, improves the processing efficiency of reply messages, and ensures the efficiency and accuracy of message processing.
Smart Images

Figure CN115914155B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technology, and in particular to a message processing method, device and electronic device. Background Art
[0002] Swoole is a parallel network communication engine based on asynchronous event-driven and coroutines, providing coroutine and high-performance network programming support for PHP (hypertext preprocessor). Swoole provides network server and client modules for multiple communication protocols, which can easily and quickly implement TCP / UDP services, high-performance Web, WebSocket services, real-time communication, etc., making PHP no longer limited to the traditional Web field.
[0003] Among them, unlike the HTTP communication protocol that must have a request and response mechanism, the WebSocket communication protocol only sends and receives messages and does not care about the connection between each message. Therefore, when the terminal device using the WebSocket communication protocol processes the reply message, it cannot obtain the relevant data of the sent message corresponding to the reply message. Therefore, if the relevant data is needed, the terminal device needs to re-query all sent messages. When the number of sent messages is large, this query method will not only affect the performance of the network communication system on the terminal device and waste system resources, but also easily cause query errors, thereby affecting the processing efficiency of the reply message.
[0004] To address the above-mentioned problems, no effective solutions have been proposed so far. Summary of the Invention
[0005] The embodiments of the present application provide a message processing method, device, and electronic device to at least solve the technical problem in the prior art of low efficiency of terminal devices in processing received reply messages.
[0006] According to one aspect of an embodiment of the present application, a message processing method is provided, including: sending a first message to a target terminal device through a first terminal device, wherein the first message is a message that requires a reply from the target terminal device, and when multiple messages are transmitted between the target terminal device and the first terminal device, the target terminal device and the first terminal device do not store an association relationship between the multiple messages; receiving a second message returned by the target terminal device based on the first message, wherein the message identifier of the second message is the same as the message identifier of the first message; obtaining pre-stored data corresponding to the first message from a preset message mapping table according to the message identifier of the first message or the message identifier of the second message, wherein the pre-stored data is data that can be used when processing the second message, and the message mapping table stores a mapping relationship between the pre-stored data and the message identifier of the first message; processing the second message according to the pre-stored data to obtain a processing result.
[0007] Furthermore, the message processing method also includes: storing the message identifier of the first message, pre-stored data and the message processor identifier of the target message processor in a message mapping table through the first terminal device, wherein the target message processor is a script for processing the second message; determining the first status identifier corresponding to the first message based on the message identifier of the first message, wherein the first status identifier is used to represent that the current status of the first message is an unreply status; and sending the first message to the target terminal device.
[0008] Furthermore, the message processing method also includes: after obtaining the pre-stored data corresponding to the first message from the preset message mapping table according to the message identifier of the first message or the message identifier of the second message, updating the first status identifier to the second status identifier, wherein the second status identifier is used to represent that the current status of the first message is a replied status.
[0009] Furthermore, the message processing method also includes: after determining the first state identifier corresponding to the first message based on the message identifier of the first message, creating a timed task corresponding to the first message, wherein the timed task is used to detect whether the first state identifier is updated to the second state identifier within a preset time length; when it is determined according to the timed task that the first state identifier is updated to the second state identifier within the preset time length, deleting the message identifier, pre-stored data and message processor identifier of the first message from the message mapping table.
[0010] Furthermore, the message processing method also includes: after creating a scheduled task corresponding to the first message, when it is determined based on the scheduled task that the first status identifier has not been updated to the second status identifier within a preset time period, generating a target log, wherein the log content in the target log is used to indicate that the first message has not been replied to within the preset time period.
[0011] Furthermore, the message processing method also includes: receiving multiple target messages sent by the target terminal device; detecting whether each target message contains a message type identifier to obtain a detection result, wherein the message type identifier is used to characterize the message type corresponding to each target message; based on the detection result, determining at least one to-be-processed message from multiple target messages, wherein the to-be-processed message does not contain a message type identifier; determining the second message based on the message identifier of each to-be-processed message and the message identifier of the first message.
[0012] Furthermore, the message processing method also includes: after detecting whether each target message contains a message type identifier and obtaining the detection result, determining at least one third message from multiple target messages based on the detection result, wherein the message type identifier of the third message is the first message type identifier, and the first message type identifier indicates that the third message is a message sent by the target terminal device and requires a reply from the first terminal device.
[0013] Furthermore, the message processing method further includes: obtaining a message processor identifier from a message mapping table; determining a target message processor from a plurality of message processors according to the message processor identifier; and controlling the target message processor to perform a processing operation on the second message according to pre-stored data.
[0014] According to another aspect of an embodiment of the present application, a message processing device is also provided, including: a sending module, used to send a first message to a target terminal device through a first terminal device, wherein the first message is a message that requires a reply from the target terminal device, and when multiple messages are transmitted between the target terminal device and the first terminal device, the target terminal device and the first terminal device do not store an association relationship between the multiple messages; a receiving module, used to receive a second message returned by the target terminal device based on the first message, wherein the message identifier of the second message is the same as the message identifier of the first message; an acquisition module, used to obtain pre-stored data corresponding to the first message from a preset message mapping table according to the message identifier of the first message or the message identifier of the second message, wherein the pre-stored data is data that can be used when processing the second message, and the message mapping table stores a mapping relationship between the pre-stored data and the message identifier of the first message; a processing module, used to process the second message according to the pre-stored data to obtain a processing result.
[0015] According to another aspect of an embodiment of the present application, an electronic device is also provided, which includes one or more processors; a storage device for storing one or more programs, which, when the one or more programs are executed by one or more processors, enables the one or more processors to run the programs, wherein the programs are configured to execute the above-mentioned message processing method when running.
[0016] In the present application, the pre-stored data of a first message requiring a response from a target terminal device is stored in a message mapping table. First, the first message is sent to the target terminal device via the first terminal device. Then, a second message returned by the target terminal device based on the first message is received. The pre-stored data corresponding to the first message is retrieved from a preset message mapping table based on the message identifier of the first message or the message identifier of the second message. Finally, the second message is processed based on the pre-stored data to obtain a processing result. The first message is a message requiring a response from the target terminal device. When multiple messages are transmitted between the target terminal device and the first terminal device, the target terminal device and the first terminal device do not store an association between the multiple messages. The message identifier of the second message is the same as the message identifier of the first message. The pre-stored data is data that can be used when processing the second message. The message mapping table stores a mapping relationship between the pre-stored data and the message identifier of the first message.
[0017] It can be seen from the above content that the first message in this application is a pending reply message sent by the first terminal device and requires a reply from the target terminal device. This application adopts the method of storing the pre-stored data of the first message in the message mapping table. Since the pre-stored data is the data in the first message that can be used when processing the second message, the first terminal device does not need to re-query all the sent messages when processing the second message. It only needs to process the second message according to the pre-stored data, thereby reducing the consumption of computing resources of the terminal device and improving the processing efficiency of the second message.
[0018] It can be seen that the technical solution of the present application achieves the purpose of avoiding the need to query all sent messages when processing reply messages, thereby achieving the technical effect of reducing the computing resource consumption of the terminal device, and further solving the technical problem of low efficiency of terminal devices in processing received reply messages in the prior art. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0020] Figure 1 is a flowchart of an optional message processing method according to an embodiment of the present application;
[0021] Figure 2 is a flowchart of another optional message processing method according to an embodiment of the present application;
[0022] Figure 3 is a schematic diagram of another optional message distributor according to an embodiment of the present application;
[0023] Figure 4is a schematic diagram of another optional message mapping table according to an embodiment of the present application;
[0024] Figure 5 This is a schematic diagram of an optional message processing device according to an embodiment of the present application. DETAILED DESCRIPTION
[0025] In order to enable those skilled in the art to better understand the present invention, the following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of this application.
[0026] 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 are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in a sequence other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0027] Example 1
[0028] Swoole is a parallel network communication engine based on asynchronous event-driven and coroutines, providing coroutine and high-performance network programming support for PHP. Swoole provides network server and client modules for multiple communication protocols, which can easily and quickly implement TCP / UDP services, high-performance Web, WebSocket services, real-time communication, etc., making PHP no longer limited to the traditional Web field.
[0029] Among them, unlike the HTTP communication protocol that must have a request and response mechanism, the WebSocket communication protocol only sends and receives messages and does not care about the connection between each message. Therefore, when the terminal device using the WebSocket communication protocol processes the reply message, it cannot obtain the relevant data of the sent message corresponding to the reply message. Therefore, if the relevant data is needed, the terminal device needs to re-query all sent messages. When the number of sent messages is large, this query method will not only affect the performance of the network communication system on the terminal device and waste system resources, but also easily cause query errors, thereby affecting the processing efficiency of the reply message.
[0030] For example, terminal device A and terminal device B communicate using the WebSocket protocol. Terminal device A sends a pending reply message to terminal device B. When terminal device A receives terminal device B's reply to the pending reply message, terminal device A cannot obtain the relevant data of the pending reply message when processing the reply message. For example, the user information at the time of sending and the message content. Therefore, terminal device A needs to query all sent messages to find the corresponding pending reply message. When the number of sent messages is large, each query consumes a certain amount of system resources. When the number of reply messages is large, the number of queries also increases, which continuously exacerbates the consumption of system resources and leads to low reply message processing efficiency.
[0031] In order to solve the problem of low reply message processing efficiency when the above-mentioned terminal devices use the WebSocket communication protocol for communication, the present application provides an embodiment of a message processing method. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0032] Figure 1 is a flowchart of an optional message processing method according to an embodiment of the present application, such as Figure 1 As shown, the method includes the following steps:
[0033] Step S101: Send a first message to a target terminal device through a first terminal device.
[0034] In step S101, the first message is a message that requires a reply from the target terminal device. When multiple messages are transmitted between the target terminal device and the first terminal device, the target terminal device and the first terminal device do not store an association relationship between the multiple messages.
[0035] Specifically, the first terminal device and the target terminal device communicate using the WebSocket communication protocol. Based on the design principle of the WebSocket communication protocol, neither the first terminal device nor the target terminal device stores the association relationship between multiple messages transmitted during the communication process. In addition, the first terminal device and the target terminal device can be servers, smart phones, laptops, desktop computers, smart wearable devices and other devices. Assuming that the first terminal device is server A and the target terminal device is smart phone B, server A can send a message to smart phone B that requires a reply from smart phone B. For example, the first message is a positioning instruction. After receiving the positioning instruction, smart phone B needs to return the geographical location information of the current area of smart phone B to server A.
[0036] It should be noted that, in the description of the solution of the embodiment of the present application, the first terminal device is the executor of the message processing method in the embodiment of the present application.
[0037] Step S102: Receive a second message returned by the target terminal device based on the first message.
[0038] In step S102, the message identifier of the second message is the same as the message identifier of the first message.
[0039] Specifically, after receiving the first message sent by the first terminal device, the target terminal device generates a reply message based on the first message, that is, a second message, and then the target terminal device returns the second message to the first terminal device.
[0040] It should be noted that in the present application, when the first terminal device sends the first message, it will assign a message identifier to the first message, and the message identifier is a unique identifier for the first message. When the target terminal device generates a second message based on the first message, the target terminal device will not regenerate a new message identifier for the second message, but will directly use the message identifier of the first message as the message identifier of the second message, that is, the message identifier of the second message is the same as the message identifier of the first message. For example, assuming that the message identifier of the first message is 001, the message identifier of the second message is also 001. After receiving the second message, the first terminal device can determine that the second message corresponds to the first message based on the message identifier "001".
[0041] Step S103: acquiring pre-stored data corresponding to the first message from a preset message mapping table according to the message identifier of the first message or the message identifier of the second message.
[0042] In step S103, the pre-stored data is data that can be used when processing the second message, and the message mapping table stores a mapping relationship between the pre-stored data and the message identifier of the first message.
[0043] Specifically, the above-mentioned pre-stored data includes but is not limited to user information, message content and other data in the first message. When processing the second message, the first terminal device can directly reference the pre-stored data without having to query the first message in all messages sent and parse the pre-stored data from the first message, thereby improving the processing efficiency of the second message.
[0044] Step S104: Process the second message according to the pre-stored data to obtain a processing result.
[0045] In step S104, the first terminal device can directly use pre-stored data to process the second message. For example, the first message is a positioning instruction sent by the first terminal device to the target terminal device, and the second message is the geographic location information returned by the target terminal device to the first terminal device according to the positioning instruction. The first terminal device can determine the location of the user corresponding to the user information based on the user information in the first message.
[0046] Based on the contents of steps S101 to S104 above, it can be seen that in this application, the pre-stored data of the first message that requires a reply from the target terminal device is stored in a message mapping table. First, the first message is sent to the target terminal device via the first terminal device. Then, a second message returned by the target terminal device based on the first message is received. The pre-stored data corresponding to the first message is obtained from a preset message mapping table based on the message identifier of the first message or the message identifier of the second message. Finally, the second message is processed based on the pre-stored data to obtain a processing result. The first message is a message that requires a reply from the target terminal device. When multiple messages are transmitted between the target terminal device and the first terminal device, the target terminal device and the first terminal device do not store the association relationship between the multiple messages. The message identifier of the second message is the same as the message identifier of the first message. The pre-stored data is data that can be used when processing the second message. The message mapping table stores the mapping relationship between the pre-stored data and the message identifier of the first message.
[0047] It can be seen from the above content that the first message in this application is a pending reply message sent by the first terminal device and requires a reply from the target terminal device. This application adopts the method of storing the pre-stored data of the first message in the message mapping table. Since the pre-stored data is the data in the first message that can be used when processing the second message, the first terminal device does not need to re-query all the sent messages when processing the second message. It only needs to process the second message according to the pre-stored data, thereby reducing the consumption of computing resources of the terminal device and improving the processing efficiency of the second message.
[0048] It can be seen that the technical solution of the present application achieves the purpose of avoiding the need to query all sent messages when processing reply messages, thereby achieving the technical effect of reducing the computing resource consumption of the terminal device, and further solving the technical problem of low efficiency of terminal devices in processing received reply messages in the prior art.
[0049] In an optional embodiment, the first terminal device may send various types of terminal messages to the target terminal device. When the first terminal device sends a first message requiring a reply from the target terminal device to the target terminal device, the following process is required:
[0050] First, the first terminal device stores the message identifier of the first message, pre-stored data, and the message processor identifier of the target message processor in a message mapping table. Then, based on the message identifier of the first message, the first terminal device determines the first status identifier corresponding to the first message, where the first status identifier indicates that the current status of the first message is unanswered. Finally, the first terminal device sends the first message to the target terminal device. The target message processor is a script for processing the second message.
[0051] like Figure 2 As shown, for a first terminal device and a target device communicating using the WebSocket communication protocol, when the first terminal device needs to send a first message to the target terminal device that requires a reply from the target terminal device, the first terminal device will first call the message sending method of the proxy connection object, and then store the message identifier of the first message, pre-stored data, and the message processor identifier of the target message processor in the message mapping table of the proxy connection object. Furthermore, the first terminal device will also determine the first status identifier corresponding to the first message based on the first status identifier, that is, Figure 2 A status flag is set for the message identifier of the first message, wherein the first status flag may be “false”. Only after the above operation is completed will the first terminal device send the first message to the target terminal device.
[0052] To facilitate understanding of the above content, the following describes the contents of the proxy connection object:
[0053] According to the design principle of the websocket communication protocol, after the first terminal device and the target terminal device establish a websocket communication connection, the first terminal device and the target terminal device can both obtain a connection object, and use the message sending method of the object to send messages to the other end. On this basis, the present application creates a proxy object for the connection object and rewrites the message sending method, adding state, callbackHandler, timeoutHandler and time parameters to the original message sending method. These parameters are all optional and are used when the sent message requires a reply from the receiving end. For example, for the first message sent by the first terminal device to the target terminal device, state is used to save the pre-stored data in the first message; callbackHandler is used to specify which message processor or class method on the receiving end will process the reply message (that is, store the message processor identifier of the target message processor); timeoutHandler is used to specify the timeout message processor. If the first message times out in reply, the timeout message processor is used to process the first message, such as generating a target log; time is used to specify the preset duration corresponding to the scheduled task.
[0054] Optionally, since the first terminal device may receive messages of various message types sent by the target terminal device, before obtaining the pre-stored data corresponding to the first message from the preset message mapping table according to the message identifier of the first message or the message identifier of the second message, the first terminal device also needs to perform the following process:
[0055] First, the first terminal device receives multiple target messages sent by a target terminal device. The first terminal device then uses a message distributor to detect whether each target message contains a message type identifier, and obtains a detection result. The message type identifier is used to identify the message type corresponding to each target message. Finally, based on the detection result, the first terminal device determines at least one to-be-processed message from the multiple target messages, and determines a second message based on the message identifier of each to-be-processed message and the message identifier of the first message. The to-be-processed message does not contain a message type identifier.
[0056] Furthermore, based on the detection results, the first terminal device will also determine at least one third message from multiple target messages, wherein the message type identifier of the third message is the first message type identifier, and the first message type identifier indicates that the third message is a message sent by the target terminal device and requires a reply from the first terminal device.
[0057] Specifically, this solution divides message types into three types: ordinary messages, messages that require a reply (for example, a first message sent by a first terminal device to a target terminal device and a third message sent by a target terminal device to the first terminal device), and reply messages (for example, a second message sent by a target terminal device to the first terminal device). The difference between messages that require a reply and ordinary messages and reply messages is that messages that require a reply will be specially processed when they are sent, that is, the pre-stored data of the messages that require a reply will be stored in a message mapping table. The difference between a reply message and the other two types of messages is that the message identifier of the reply message and the message identifier of the corresponding message that requires a reply must be the same, and the reply message has no message type.
[0058] Combining the above content, we can see that Figure 2 As shown, when the message distributor recognizes that a target message does not contain a message type identifier, it indicates that the target message is a reply message. When the message distributor recognizes that a target message contains a message type identifier of a normal message, it determines that the target message is a normal message. When the message distributor recognizes that a target message contains a first message type identifier, it determines that the target message is a message that requires a reply.
[0059] In addition, if Figure 3 As shown, the message dispatcher is actually an instance object, which maintains a message type as the key (for example Figure 3 The message handler is a message type mapping table for value. Value stores three message handlers: a normal message handler, a reply message handler, and a timeout message handler. The normal message handler handles normal messages and messages requiring a reply. The reply message handler handles reply messages. The timeout message handler handles messages requiring a reply that haven't been replied to within a preset timeout period.
[0060] In an optional embodiment, after obtaining the pre-stored data corresponding to the first message from a preset message mapping table based on the message identifier of the first message or the message identifier of the second message, the first terminal device also updates the first status identifier to a second status identifier, wherein the second status identifier is used to represent that the current status of the first message is a replied status.
[0061] like Figure 2As shown, the first terminal device can obtain the pre-stored data corresponding to the first message and the message processor identifier of the target message processor from the message mapping table based on the message identifier of the first message, wherein the target message processor is the reply message processor specified by the first terminal device when sending the first message to process the reply message of the first message. After obtaining the pre-stored data and the message processor identifier, the first terminal device will update the first state identifier corresponding to the first message, for example, updating the first state identifier "false" to the second state identifier "true". Finally, after completing the update of the first state identifier, the first terminal device controls the target message processor to process the second message based on the pre-stored data.
[0062] In addition, if Figure 2 As shown, for ordinary messages and messages requiring a reply received by the first terminal device, the message distributor will distribute them to the corresponding ordinary message processor for processing. Since the data structure of ordinary messages and messages requiring a reply is the same, whether to reply to these messages is decided by the developer.
[0063] In an optional embodiment, after generating a first state identifier corresponding to the first message based on the message identifier of the first message, the first terminal device also creates a timed task corresponding to the first message, wherein the timed task is used to detect whether the first state identifier is updated to the second state identifier within a preset duration. If it is determined based on the timed task that the first state identifier is updated to the second state identifier within the preset duration, the first terminal device deletes the message identifier, pre-stored data, and message processor identifier of the first message from the message mapping table.
[0064] When it is determined according to the scheduled task that the first status identifier has not been updated to the second status identifier within the preset time period, the first terminal device generates a target log, wherein the log content in the target log is used to indicate that the first message has not been replied within the preset time period.
[0065] Optional, such as Figure 2 As shown, after determining the first status identifier corresponding to the first message, the first terminal device first creates a scheduled task for the first message, and then sends the first message to the target terminal device.
[0066] Among them, the timed task is used to detect whether the first state identifier is updated to the second state identifier within a preset time period, and the preset time period can be customized. Specifically, starting from the time when the timed task is successfully created, after the preset time period has passed, the first terminal device executes the timed task. First, the first terminal device reads the value corresponding to the first message from the message mapping table. Then, the first terminal device deletes the message identifier of the first message from the message mapping table, and then determines whether the first state identifier of the first message has been updated to the second state identifier based on isReply in value, that is, determines whether the first message has been replied. If there is no reply, the first terminal device controls the timeout message processor in value to generate a target log, wherein the log content in the target log is used to indicate that the first message has not been replied within the preset time period.
[0067] It should be noted that in the existing websocket communication process, for messages that require a terminal device to reply, if the terminal device does not reply, the sender will not be able to perceive that the terminal device has not replied, and the sender will not be able to proceed to the next step, which will lead to the problem that the business process cannot run smoothly.
[0068] In this application, by creating a scheduled task for the message that needs to be replied, the purpose of obtaining the timeout message in time is achieved, thereby improving the operating efficiency of the business process, and the operation and maintenance personnel can process the timeout message in time.
[0069] As can be seen from the above content, the technical solution provided by this application is mainly applicable to scenarios based on Swoole communication and websocket communication. The core of this solution is to realize the automatic distribution of websocket messages, automatic timeout detection of reply messages, and storage of pre-stored data. In this solution, the entire interaction process is divided into the sending end and the receiving end, each with different processing processes. It has the following main features:
[0070] 1) The sender and receiver communicate by sending JSON-formatted messages and agreeing on a key as the message type to distinguish different messages. This application encapsulates the original WebSocket message sending and receiving methods using annotations and reflection. It supports configuring the key of the message type and supports automatic message distribution. Developers can use the @msgHandler annotation to define a normal message handler, the @callbackHandler annotation to define a reply message handler, and the @timeoutHandler annotation to define a timeout message handler.
[0071] 2) The JSON format message of the communication contains a message identifier. This application uses uuid as the message identifier of each message. When sending a reply message, you can use the message identifier of the message to be replied to, and there is no need to define additional message types.
[0072] 3) After the websocket connection is successfully created, a connection object can be obtained. The message sending method of this object can be used to send messages to the receiving end. This application creates a proxy object for this object and rewrites the message sending method. The state, callbackHandler, timeoutHandler and time parameters are added to the original message sending method. These parameters are optional and are used when the sent message requires a reply from the receiving end. For example, for the first message sent by the first terminal device to the target terminal device, state is used to save the pre-stored data in the first message; callbackHandler is used to specify which message processor or class method on the receiving end will process the reply message (that is, store the message processor identifier of the target message processor); timeoutHandler is used to specify the message processor or class method after the timeout; time is used to specify the preset duration corresponding to the scheduled task.
[0073] 4) Create a global message mapping table. When a terminal device sends a message that requires a reply, the terminal device will use the message ID as the key and the state, callbackHandler, timeoutHandler, and isReply (state ID) as the value to add them to the message ID mapping table.
[0074] like Figure 4 As shown, in a message mapping table, message identifier 1 and message identifier 2 stored in the key represent two first messages. The value corresponding to each message identifier includes the pre-stored data of the first message corresponding to the message identifier, an identifier of a reply message processor (corresponding to the message processor identifier of the target message processor), a state identifier (corresponding to the first state identifier and the second state identifier), and an identifier of a timeout message processor (the timeout message processor is used to process the first message corresponding to the message identifier after it times out).
[0075] The following three application scenarios are used to illustrate the technical solution of this application:
[0076] In the scenario where LMS issues a license, before LMS (License Management System) issues a license to a device, it first needs to mark the license selected by the system as occupied to prevent the license from being issued to other devices. Then the license is issued to the device. After the device receives the license file sent by LMS, it installs it. After the installation is complete, it will reply to LMS whether the installation is successful. LMS updates the status of the device and license based on the device's reply message.
[0077] When adopting the technical solution of the present application, it is only necessary to use annotations to define the reply message processor and the timeout message processor. When the device replies to the message, the message will be automatically distributed to the reply message processor for processing. If there is no reply to the timeout message, the timeout message processor will be automatically called. In addition, the device data and license data queried before issuing the license can be directly obtained in the message processor without having to search again in the database. In this scenario, the LMS requires the device to reply after issuing the license, and can update the device and license status in a timely manner. If the device does not reply after the timeout, the device's occupation of the license can be released in a timely manner so that the license can be issued to other devices.
[0078] In the Websocket message subscription scenario, the client subscribes to a certain type of message from the server. When the server receives a message of this type, it will actively push it to the client. The server needs to know whether the client has received it and record the processing result of the message.
[0079] When adopting the technical solution of the present application, the server defines a reply message processor and a timeout message processor. After pushing the message, the client's processing result can be obtained in the reply message processor. If the websocket connection between the client and the server is suddenly disconnected, or the client fails to process the message and does not reply, the server can also perceive it in the timeout message processor.
[0080] It can be seen that the technical solution of the present application improves the reliability of the system and ensures that messages are not lost.
[0081] In the Websocket scenario of resumable large file transfer, when transmitting large files, in order to ensure the resumability of large file transfers, the file is usually split into several small files and numbered. Each time the client receives a slice file, it needs to reply to the server for confirmation, and the server synchronizes the transmission status.
[0082] When a slice file times out, the file can be directly located in the timeout message processor and retransmitted.
[0083] When the client network is disconnected and retransmission is required, it only needs to check the local download record and then send the latest slice file number to resume the transfer. The server can also perceive the transmission status and progress in time.
[0084] It can be seen that through the technical solution of the present application, data transmission status monitoring can be easily achieved to ensure the real-time, consistency and integrity of the data.
[0085] Example 2
[0086] According to an embodiment of the present application, an embodiment of a message processing device is also provided, such as Figure 5 As shown, the device includes: a sending module 501, which is used to send a first message to a target terminal device through a first terminal device, wherein the first message is a message that requires a reply from the target terminal device, and when multiple messages are transmitted between the target terminal device and the first terminal device, the target terminal device and the first terminal device do not store the association relationship between the multiple messages; a receiving module 502, which is used to receive a second message returned by the target terminal device based on the first message, wherein the message identifier of the second message is the same as the message identifier of the first message; an obtaining module 503, which is used to obtain pre-stored data corresponding to the first message from a preset message mapping table according to the message identifier of the first message or the message identifier of the second message, wherein the pre-stored data is data that can be used when processing the second message, and the message mapping table stores a mapping relationship between the pre-stored data and the message identifier of the first message; a processing module 504, which is used to process the second message according to the pre-stored data to obtain a processing result.
[0087] In an optional embodiment, the first terminal device and the target terminal device communicate using the WebSocket communication protocol. Based on the design principle of the WebSocket communication protocol, neither the first terminal device nor the target terminal device stores the association relationship between multiple messages transmitted during the communication process. In addition, the first terminal device and the target terminal device can be servers, smart phones, laptops, desktop computers, smart wearable devices and other devices. Assuming that the first terminal device is server A and the target terminal device is smart phone B, server A can send a message to smart phone B that requires a reply from smart phone B. For example, the first message is a positioning instruction. After receiving the positioning instruction, smart phone B needs to return the geographical location information of the current location of smart phone B to server A.
[0088] It should be noted that, in the description of the solution of the embodiment of the present application, the first terminal device is the executor of the message processing method in the embodiment of the present application.
[0089] In addition, after receiving the first message sent by the first terminal device, the target terminal device will generate a reply message based on the first message, that is, a second message, and then the target terminal device returns the second message to the first terminal device.
[0090] It should be noted that in the present application, when the first terminal device sends the first message, it will assign a message identifier to the first message, and the message identifier is a unique identifier for the first message. When the target terminal device generates a second message based on the first message, the target terminal device will not regenerate a new message identifier for the second message, but will directly use the message identifier of the first message as the message identifier of the second message, that is, the message identifier of the second message is the same as the message identifier of the first message. For example, assuming that the message identifier of the first message is 001, the message identifier of the second message is also 001. After receiving the second message, the first terminal device can determine that the second message corresponds to the first message based on the message identifier "001".
[0091] In an optional embodiment, the pre-stored data includes, but is not limited to, user information, message content, and other data in the first message. When processing the second message, the first terminal device can directly reference the pre-stored data without having to search for the first message in all previously sent messages and parse the pre-stored data from the first message, thereby improving the processing efficiency of the second message. For example, the first message is a positioning instruction sent by the first terminal device to a target terminal device, and the second message is geographic location information returned by the target terminal device to the first terminal device based on the positioning instruction. The first terminal device can determine the location of the user corresponding to the user information based on the user information in the first message.
[0092] It can be seen from the above content that the first message in this application is a pending reply message sent by the first terminal device and requires a reply from the target terminal device. This application adopts the method of storing the pre-stored data of the first message in the message mapping table. Since the pre-stored data is the data in the first message that can be used when processing the second message, the first terminal device does not need to re-query all the sent messages when processing the second message. It only needs to process the second message according to the pre-stored data, thereby reducing the consumption of computing resources of the terminal device and improving the processing efficiency of the second message.
[0093] It can be seen that the technical solution of the present application achieves the purpose of avoiding the need to query all sent messages when processing reply messages, thereby achieving the technical effect of reducing the computing resource consumption of the terminal device, and further solving the technical problem of low efficiency of the terminal device in processing received reply messages in the prior art. Optionally, the sending module also includes: a storage unit, a first determination unit and a first sending unit. The storage unit is used to store the message identifier of the first message, pre-stored data and the message processor identifier of the target message processor in the message mapping table through the first terminal device, wherein the target message processor is a script for processing the second message; the first determination unit is used to determine the first state identifier corresponding to the first message according to the message identifier of the first message, wherein the first state identifier is used to indicate that the current state of the first message is an unreply state; and the first sending unit is used to send the first message to the target terminal device.
[0094] First, the first terminal device stores the message identifier of the first message, pre-stored data, and the message processor identifier of the target message processor in a message mapping table. Then, based on the message identifier of the first message, the first terminal device determines the first status identifier corresponding to the first message, where the first status identifier indicates that the current status of the first message is unanswered. Finally, the first terminal device sends the first message to the target terminal device. The target message processor is a script for processing the second message.
[0095] like Figure 2 As shown, for a first terminal device and a target device communicating using the WebSocket communication protocol, when the first terminal device needs to send a first message to the target terminal device that requires a reply from the target terminal device, the first terminal device will first call the message sending method of the proxy connection object, and then store the message identifier of the first message, pre-stored data, and the message processor identifier of the target message processor in the message mapping table of the proxy connection object. Furthermore, the first terminal device will also determine the first status identifier corresponding to the first message based on the first status identifier, that is, Figure 2 A status flag is set for the message identifier of the first message, wherein the first status flag may be “false”. Only after the above operation is completed will the first terminal device send the first message to the target terminal device.
[0096] To facilitate understanding of the above content, the following describes the contents of the proxy connection object:
[0097] According to the design principle of the websocket communication protocol, after the first terminal device and the target terminal device establish a websocket communication connection, the first terminal device and the target terminal device can both obtain a connection object, and use the message sending method of the object to send messages to the other end. On this basis, the present application creates a proxy object for the connection object and rewrites the message sending method, adding state, callbackHandler, timeoutHandler and time parameters to the original message sending method. These parameters are all optional and are used when the sent message requires a reply from the receiving end. For example, for the first message sent by the first terminal device to the target terminal device, state is used to save the pre-stored data in the first message; callbackHandler is used to specify which message processor or class method on the receiving end will process the reply message (that is, store the message processor identifier of the target message processor); timeoutHandler is used to specify the timeout message processor. If the first message times out in reply, the timeout message processor is used to process the first message, such as generating a target log; time is used to specify the preset duration corresponding to the scheduled task. Optionally, the message processing device further includes: an updating module, configured to update the first status identifier to a second status identifier, wherein the second status identifier is used to indicate that the current status of the first message is a replied status.
[0098] Optionally, the message processing device further includes: a task creation module and a deletion module. The task creation module is configured to create a scheduled task corresponding to the first message, wherein the scheduled task is configured to detect whether the first state identifier is updated to the second state identifier within a preset duration; and the deletion module is configured to delete the message identifier, pre-stored data, and message processor identifier of the first message from the message mapping table if it is determined, based on the scheduled task, that the first state identifier is updated to the second state identifier within the preset duration.
[0099] Optionally, the message processing device also includes: a log generation module, which is used to generate a target log when it is determined that the first status identifier has not been updated to the second status identifier within a preset time period according to a scheduled task, wherein the log content in the target log is used to indicate that the first message has not been replied within the preset time period.
[0100] Optionally, the receiving module further includes: a first receiving unit, a detection unit, a second determining unit, and a third determining unit. The first receiving unit is configured to receive multiple target messages sent by a target terminal device; the detection unit is configured to detect whether each target message contains a message type identifier and obtain a detection result, wherein the message type identifier is used to characterize the message type corresponding to each target message; the second determining unit is configured to determine, based on the detection result, at least one message to be processed from the multiple target messages, wherein the message to be processed does not contain a message type identifier; and the third determining unit is configured to determine the second message based on the message identifier of each message to be processed and the message identifier of the first message.
[0101] Optionally, the message processing device also includes: a first determination module, used to determine at least one third message from multiple target messages based on the detection results, wherein the message type identifier of the third message is the first message type identifier, and the first message type identifier indicates that the third message is a message sent by the target terminal device and requires a reply from the first terminal device.
[0102] Optionally, the processing module further includes: a first acquisition unit, a fourth determination unit, and a control unit. The first acquisition unit is configured to acquire a message processor identifier from a message mapping table; the fourth determination unit is configured to determine a target message processor from a plurality of message processors based on the message processor identifier; and the control unit is configured to control the target message processor to perform a processing operation on the second message based on pre-stored data.
[0103] As can be seen from the above content, the technical solution provided by this application is mainly applicable to scenarios based on Swoole communication and websocket communication. The core of this solution is to realize the automatic distribution of websocket messages, automatic timeout detection of reply messages, and storage of pre-stored data. In this solution, the entire interaction process is divided into the sending end and the receiving end, each with different processing processes. It has the following main features:
[0104] 1) The sender and receiver communicate by sending JSON-formatted messages and agreeing on a key as the message type to distinguish different messages. This application encapsulates the original WebSocket message sending and receiving methods using annotations and reflection. It supports configuring the key of the message type and supports automatic message distribution. Developers can use the @msgHandler annotation to define a normal message handler, the @callbackHandler annotation to define a reply message handler, and the @timeoutHandler annotation to define a timeout message handler.
[0105] 2) The JSON format message of the communication contains a message identifier. This application uses uuid as the message identifier of each message. When sending a reply message, you can use the message identifier of the message to be replied to, and there is no need to define additional message types.
[0106] 3) After the websocket connection is successfully created, a connection object can be obtained. The message sending method of this object can be used to send messages to the receiving end. This application creates a proxy object for this object and rewrites the message sending method. The state, callbackHandler, timeoutHandler and time parameters are added to the original message sending method. These parameters are optional and are used when the sent message requires a reply from the receiving end. For example, for the first message sent by the first terminal device to the target terminal device, state is used to save the pre-stored data in the first message; callbackHandler is used to specify which message processor or class method on the receiving end will process the reply message (that is, store the message processor identifier of the target message processor); timeoutHandler is used to specify the message processor or class method after the timeout; time is used to specify the preset duration corresponding to the scheduled task.
[0107] 4) Create a global message mapping table. When a terminal device sends a message that requires a reply, the terminal device will use the message ID as the key and the state, callbackHandler, timeoutHandler, and isReply (state ID) as the value to add them to the message ID mapping table.
[0108] like Figure 4 As shown, in a message mapping table, message identifier 1 and message identifier 2 stored in the key represent two first messages. The value corresponding to each message identifier includes the pre-stored data of the first message corresponding to the message identifier, an identifier of a reply message processor (corresponding to the message processor identifier of the target message processor), a state identifier (corresponding to the first state identifier and the second state identifier), and an identifier of a timeout message processor (the timeout message processor is used to process the first message corresponding to the message identifier after it times out).
[0109] The following three application scenarios are used to illustrate the technical solution of this application:
[0110] In the scenario where LMS issues a license, before LMS (License Management System) issues a license to a device, it first needs to mark the license selected by the system as occupied to prevent the license from being issued to other devices. Then the license is issued to the device. After the device receives the license file sent by LMS, it installs it. After the installation is complete, it will reply to LMS whether the installation is successful. LMS updates the status of the device and license based on the device's reply message.
[0111] When adopting the technical solution of the present application, it is only necessary to use annotations to define the reply message processor and the timeout message processor. When the device replies to the message, the message will be automatically distributed to the reply message processor for processing. If there is no reply to the timeout message, the timeout message processor will be automatically called. In addition, the device data and license data queried before issuing the license can be directly obtained in the message processor without having to search again in the database. In this scenario, the LMS requires the device to reply after issuing the license, and can update the device and license status in a timely manner. If the device does not reply after the timeout, the device's occupation of the license can be released in a timely manner so that the license can be issued to other devices.
[0112] In the Websocket message subscription scenario, the client subscribes to a certain type of message from the server. When the server receives a message of this type, it will actively push it to the client. The server needs to know whether the client has received it and record the processing result of the message.
[0113] When adopting the technical solution of the present application, the server defines a reply message processor and a timeout message processor. After pushing the message, the client's processing result can be obtained in the reply message processor. If the websocket connection between the client and the server is suddenly disconnected, or the client fails to process the message and does not reply, the server can also perceive it in the timeout message processor.
[0114] It can be seen that the technical solution of the present application improves the reliability of the system and ensures that messages are not lost.
[0115] In the Websocket scenario of resumable large file transfer, when transmitting large files, in order to ensure the resumability of large file transfers, the file is usually split into several small files and numbered. Each time the client receives a slice file, it needs to reply to the server for confirmation, and the server synchronizes the transmission status.
[0116] When a slice file times out, the file can be directly located in the timeout message processor and retransmitted.
[0117] When the client network is disconnected and retransmission is required, it only needs to check the local download record and then send the latest slice file number to resume the transfer. The server can also perceive the transmission status and progress in time.
[0118] It can be seen that through the technical solution of the present application, data transmission status monitoring can be easily achieved to ensure the real-time, consistency and integrity of the data.
[0119] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.
[0120] In the above embodiments of the present application, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.
[0121] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only exemplary. For example, the division of the units can be a logical function division. In actual implementation, there may be other division methods, such as 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 mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0122] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0123] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0124] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially 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, and the computer software product is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk.
[0125] The above is only a preferred embodiment of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A message processing method, characterized in that: include: Sending a first message to a target terminal device through a first terminal device, wherein the first message is a message requiring a reply from the target terminal device, and when multiple messages are transmitted between the target terminal device and the first terminal device, the target terminal device and the first terminal device do not store an association relationship between the multiple messages; Receiving a second message returned by the target terminal device based on the first message, wherein the message identifier of the second message is the same as the message identifier of the first message; Retrieving pre-stored data corresponding to the first message from a preset message mapping table according to the message identifier of the first message or the message identifier of the second message, wherein the pre-stored data is data that can be used when processing the second message, and the message mapping table stores a mapping relationship between the pre-stored data and the message identifier of the first message; Processing the second message according to the pre-stored data to obtain a processing result; The sending of the first message to the target terminal device through the first terminal device includes: The message identifier of the first message, the pre-stored data and the message processor identifier of the target message processor are stored in the message mapping table through the first terminal device, wherein the target message processor is a script for processing the second message; the first status identifier corresponding to the first message is determined according to the message identifier of the first message, wherein the first status identifier is used to represent that the current status of the first message is an unreply status; and the first message is sent to the target terminal device.
2. The method according to claim 1, characterized in that After acquiring pre-stored data corresponding to the first message from a preset message mapping table according to the message identifier of the first message or the message identifier of the second message, the method further includes: The first status identifier is updated to a second status identifier, wherein the second status identifier is used to indicate that the current status of the first message is a replied status.
3. The method according to claim 2, characterized in that After determining the first status identifier corresponding to the first message according to the message identifier of the first message, the method further includes: Creating a scheduled task corresponding to the first message, wherein the scheduled task is used to detect whether the first status identifier is updated to the second status identifier within a preset time period; When it is determined according to the timed task that the first state identifier is updated to the second state identifier within the preset time period, the message identifier of the first message, the pre-stored data, and the message processor identifier are deleted from the message mapping table.
4. The method according to claim 3, characterized in that After creating the scheduled task corresponding to the first message, the method further includes: When it is determined according to the scheduled task that the first status identifier has not been updated to the second status identifier within the preset time period, a target log is generated, wherein the log content in the target log is used to indicate that the first message has not been replied within the preset time period.
5. The method according to claim 1, wherein Receiving a second message returned by the target terminal device based on the first message includes: receiving multiple target messages sent by a target terminal device; Detecting whether each target message contains a message type identifier to obtain a detection result, wherein the message type identifier is used to characterize the message type corresponding to each target message; Determining, according to the detection result, at least one message to be processed from the multiple target messages, wherein the message to be processed does not include the message type identifier; The second message is determined according to the message identifier of each to-be-processed message and the message identifier of the first message.
6. The method according to claim 5, characterized in that After detecting whether each target message contains a message type identifier and obtaining a detection result, the method further includes: According to the detection result, at least one third message is determined from the multiple target messages, wherein the message type identifier of the third message is the first message type identifier, and the first message type identifier indicates that the third message is a message sent by the target terminal device and requires a reply from the first terminal device.
7. The method according to claim 1, characterized in that Processing the second message according to the pre-stored data to obtain a processing result includes: Obtain the message processor identifier from the message mapping table; determining the target message processor from a plurality of message processors according to the message processor identifier; The target message processor is controlled to perform processing operations on the second message according to the pre-stored data.
8. A message processing device, characterized in that: include: a sending module, configured to send a first message to a target terminal device through a first terminal device, wherein the first message is a message requiring a reply from the target terminal device, and when multiple messages are transmitted between the target terminal device and the first terminal device, the target terminal device and the first terminal device do not store an association relationship between the multiple messages; A receiving module, configured to receive a second message returned by the target terminal device based on the first message, wherein the message identifier of the second message is the same as the message identifier of the first message; an acquisition module, configured to acquire, from a preset message mapping table, pre-stored data corresponding to the first message according to the message identifier of the first message or the message identifier of the second message, wherein the pre-stored data is data that can be used when processing the second message, and the message mapping table stores a mapping relationship between the pre-stored data and the message identifier of the first message; a processing module, configured to process the second message according to the pre-stored data to obtain a processing result; Among them, the sending module also includes: a storage unit, used to store the message identifier of the first message, the pre-stored data and the message processor identifier of the target message processor in the message mapping table through the first terminal device, wherein the target message processor is a script for processing the second message; a first determination unit, used to determine the first status identifier corresponding to the first message according to the message identifier of the first message, wherein the first status identifier is used to represent that the current status of the first message is an unreplied status; a first sending unit, used to send the first message to the target terminal device.
9. An electronic device, characterized in that: The electronic device includes one or more processors; a storage device for storing one or more programs, which, when executed by the one or more processors, enables the one or more processors to run the programs, wherein the programs are configured to execute the message processing method described in any one of claims 1 to 7 when running.
Citation Information
Patent Citations
Method and system of shortcut reversion
CN101202953A
MQTT message processing method, device, electronic device and storage medium
CN109495375A
Message pushing method and device, electronic equipment and storage medium
CN113379501A