A cross-application communication method and device
By creating main applications and sub-applications on the front end, cross-application communication is realized, cross-application communication is solved, cross-domain communication is realized, server pressure is reduced, and front-end microservice development is suitable for the development trend.
Patent Information
- Application Number
- CN202111674435.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-31
- Publication Date
- 2024-12-27
- Estimated Expiration
- 2041-12-31
AI Technical Summary
The existing cross-domain communication methods in the existing technology have problems such as low efficiency, high server pressure, security risks and inability to transmit large data. Especially under the trend of front-end microservices, multiple front-end teams lack communication solutions that take into account both security and efficiency when developing together. .
Create main application and multiple sub-apps on the front end, each application corresponds to a different page. By subscribing to channels, unsubscribe to channels and sending messages, cross-application communication is achieved. Sub-applications judge that the target channel exists and broadcast messages, reducing server pressure, and adapting to multiple applications. Page communication requirements.
It realizes true cross-page communication, reduces server pressure, is suitable for the communication needs between front-end pages of large enterprises, is in line with the development trend of front-end microservices, is suitable for collaborative development and mutual calls of multiple front-end teams, and meets the needs of business development.
Smart Images

Figure CN114416398B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of front-end and message queue technology, and in particular to a cross-application communication method and a cross-application communication device. Background Art
[0002] In the prior art, the method of realizing cross-domain communication by URL jump between different websites requires multiple round trips to the server for each data communication, which puts great pressure on the server and is inefficient. Attackers can see the data information transmitted by the URL from the address bar of the browser, which poses a security risk, and due to the URL length limit, large data cannot be transmitted; the method of realizing cross-domain communication by cross-domain script reference requires requesting the server to generate a new script for each communication, which increases the complexity, and one party completely exposes its own data to the script of the other party, which poses a security risk; the method of realizing cross-domain communication by using the security vulnerabilities of the browser requires continuous mining of the security vulnerabilities of the browser to achieve cross-domain access and exchange of data, but malicious websites can also use security vulnerabilities to attack, and as the browser is patched and upgraded, new vulnerabilities need to be found, which is more troublesome; at the same time, with the trend of front-end microservices, there are more and more scenarios in which multiple front-end teams conduct collaborative development and call each other. Therefore, there is an urgent need for a front-end communication solution that can be universally applicable to various browsers while taking into account both security and efficiency to meet the needs of business development. Summary of the invention
[0003] In view of the above problems, embodiments of the present invention are proposed to provide a cross-application communication method and a corresponding cross-application communication device that overcome the above problems or at least partially solve the above problems.
[0004] In order to solve the above problems, an embodiment of the present invention discloses a cross-application communication method, which is applied to a front end, wherein the front end creates a main application and multiple sub-applications, each application corresponding to a different page, and the method includes:
[0005] When performing a channel subscription operation, the sub-application sends a subscription event to the main application; the main application updates application data according to the subscription event;
[0006] When performing a channel unsubscription operation, the sub-application sends an unsubscription event to the main application; the main application updates application data according to the subscription event;
[0007] When performing a message sending operation, the first sub-application determines whether a target channel corresponding to the message and a corresponding second sub-application exist according to the application data synchronized with the main application;
[0008] In the case where the second sub-application and the target channel exist, the first sub-application broadcasts and sends a message;
[0009] The second sub-application receives the message, and sends the message to a page corresponding to the second sub-application through the target channel.
[0010] Optionally, when performing a channel subscription operation, the sub-application sends a subscription event to the main application; the main application updates application data according to the subscription event, and the method includes:
[0011] When performing a channel subscription operation, the sub-application determines whether the subscription channel exists in the corresponding subscription list;
[0012] In the case that the subscription channel does not exist, the sub-application sends a subscription event to the main application, and adds the subscription channel to the subscription list;
[0013] The main application broadcasts the subscription event and updates the application data;
[0014] The sub-application receives the broadcast of the subscription event and synchronizes the application data.
[0015] Optionally, when performing a channel unsubscription operation, the sub-application sends an unsubscription event to the main application; the main application updates application data according to the subscription event, and the method includes:
[0016] When performing a subscription cancellation operation, the sub-application determines whether a target channel exists in the corresponding subscription list;
[0017] In the case that the target channel exists, the sub-application sends a subscription cancellation event to the main application, and cancels the target channel in the subscription list;
[0018] The main application broadcasts the unsubscribe event and updates application data;
[0019] The sub-application receives the broadcast of the unsubscribe event and synchronizes the application data.
[0020] Optionally, it also includes:
[0021] The main application broadcasts heartbeat messages regularly;
[0022] The sub-application synchronizes the heartbeat time of the main application;
[0023] If the first sub-application detects that the heartbeat time of the main application is abnormal, a competitive broadcast is initiated; and the sub-application with the highest application strength is determined as the main application.
[0024] Optionally, when the second sub-application and the target channel exist, the first sub-application broadcasts and sends a message, including:
[0025] In the case where the second sub-application and the target channel exist, the first sub-application compresses the message and sends the compressed message to the link adapter;
[0026] The first sub-application selects a link, and broadcasts the compressed message through the link.
[0027] Optionally, it also includes:
[0028] The second sub-application receives the message and determines whether to subscribe to the target channel;
[0029] If yes, the second sub-application decompresses the message and sends the decompressed message to the page corresponding to the second sub-application through the target channel;
[0030] If not, the message is discarded.
[0031] Optionally, it also includes:
[0032] If the second sub-application and / or the target channel does not exist, the first sub-application discards the message.
[0033] Optionally, it also includes:
[0034] When the third sub-application is created, the third sub-application broadcasts a main application query request message;
[0035] When the main application receives the main application query request information, the main application broadcasts the main application query response information and updates the application data;
[0036] The third sub-application receives the query response information within a preset heartbeat period and synchronizes the application data.
[0037] Optionally, it also includes:
[0038] If the third sub-application does not receive the query response information within the preset heartbeat period, a contention broadcast is initiated to determine the sub-application with the highest application strength as the main application.
[0039] The embodiment of the present invention further discloses a cross-application communication device, which is applied to a front end, wherein the front end creates a main application and multiple sub-applications, each application corresponding to a different page, and the device includes:
[0040] A first sending module, configured to enable the sub-application to send a subscription event to the main application when performing a channel subscription operation; and the main application to update application data according to the subscription event;
[0041] A second sending module is used for, when performing a channel unsubscription operation, the sub-application sends an unsubscription event to the main application; the main application updates application data according to the subscription event;
[0042] A first judgment module, configured to, when performing a message sending operation, determine, by the first sub-application, whether a target channel corresponding to the message and a corresponding second sub-application exist according to application data synchronized with the main application;
[0043] A first broadcast module, configured to broadcast a message by the first sub-application when the second sub-application and the target channel exist;
[0044] The first receiving module is used for the second sub-application to receive the message and send the message to the page corresponding to the second sub-application through the target channel.
[0045] Optionally, the first sending module further includes:
[0046] A second judgment module is used for, when performing a channel subscription operation, the sub-application to judge whether the subscription channel exists in the corresponding subscription list;
[0047] A first sending submodule, configured to, when the subscription channel does not exist, cause the sub-application to send a subscription event to the main application and add the subscription channel to the subscription list;
[0048] A first update module, used for the main application to broadcast the subscription event and update the application data;
[0049] The second receiving module is used for the sub-application to receive the broadcast of the subscription event and synchronize the application data.
[0050] Optionally, the second sending module further includes:
[0051] A third judgment module, used for the sub-application to judge whether a target channel exists in the corresponding subscription list when performing a subscription cancellation operation;
[0052] A second sending submodule, configured to, when the target channel exists, cause the sub-application to send a subscription cancellation event to the main application, and cancel the target channel in the subscription list;
[0053] A second updating module, configured for the main application to broadcast the unsubscribe event and update application data;
[0054] The third receiving module is used for the sub-application to receive the broadcast of the unsubscribe event and synchronize the application data.
[0055] Optionally, it also includes:
[0056] A second broadcast module, used for the main application to broadcast heartbeat messages regularly;
[0057] A synchronization module, used for the sub-application to synchronize the heartbeat time of the main application;
[0058] The first initiating module is configured to initiate a contention broadcast if the first sub-application detects that the heartbeat time of the main application is abnormal; and determine the sub-application with the highest application strength as the main application.
[0059] Optionally, the first broadcast module includes:
[0060] A compression module, configured to compress the message by the first sub-application when the second sub-application and the target channel exist, and send the compressed message to the link adapter;
[0061] A selection module is used for the first sub-application to select a link and broadcast and send the compressed message through the link.
[0062] Optionally, it also includes:
[0063] A fourth determination module, configured for the second sub-application to receive the message and determine whether to subscribe to the target channel;
[0064] If yes, the second sub-application decompresses the message and sends the decompressed message to the page corresponding to the second sub-application through the target channel;
[0065] If not, the message is discarded.
[0066] Optionally, it also includes:
[0067] A discarding module is configured to cause the first sub-application to discard the message if the second sub-application and / or the target channel does not exist.
[0068] Optionally, it also includes:
[0069] A third broadcast module, configured to broadcast a main application query request message to the third sub-application after the third sub-application is created;
[0070] a fourth broadcast module, configured to broadcast main application query response information and update the application data after the main application receives the main application query request information;
[0071] The fourth receiving module is used for the third sub-application to receive the query response information within a preset heartbeat period and synchronize the application data.
[0072] Optionally, it also includes:
[0073] The second initiating module is configured to initiate a contention broadcast if the third sub-application does not receive the query response information within a preset heartbeat period, and determine the sub-application with the highest application strength as the main application.
[0074] The embodiments of the present invention include the following advantages:
[0075] In an embodiment of the present invention, a main application and multiple sub-applications are created in the front end, each application corresponds to a different page, when the target channel subscription operation is performed, the sub-application determines whether the target channel exists in the corresponding subscription list; when the target channel does not exist, the sub-application sends a subscription event to the main application and adds the target channel to the subscription list; the main application broadcasts the subscription event and updates the application data; the sub-application receives the broadcast of the subscription event and synchronizes the application data; when the message sending operation is performed, the first sub-application determines whether the target channel corresponding to the message and the corresponding second sub-application exist according to the application data; when the second sub-application and the target channel exist, the first sub-application broadcasts the message; the second sub-application receives the message and sends the message to the page corresponding to the second sub-application through the target channel. Real cross-page communication is realized, not just parent-child page communication, which can ensure the communication needs between multiple front-end pages of large enterprises and reduce server pressure. At the same time, it conforms to the development trend of front-end microservices, is suitable for collaborative development and mutual calling of multiple front-end teams, and meets the needs of business development. BRIEF DESCRIPTION OF THE DRAWINGS
[0076] Figure 1 is a flow chart of steps of a cross-application communication method provided by an embodiment of the present invention;
[0077] Figure 2 It is a schematic diagram of the architecture of a cross-application communication method provided by an embodiment of the present invention;
[0078] Figure 3 It is a structural block diagram of a cross-application communication device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0079] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0080] In the prior art, secure communication between different domain names is achieved by embedding receiving pages and response pages, but embedding the pages twice reduces the interaction efficiency and is heavily dependent on the server, and cannot be achieved independently on the front end; based on the collection of browser and page feature data, a cross-domain communication method that is most suitable for the current operating environment is selected from the combined cross-domain communication technology, which makes up for the shortcoming of insufficient support for cross-domain communication due to the browser homology policy, and can automatically identify the operating environment and provide cross-domain communication capability processing, but it is only applicable to the field of cross-domain communication between parent and child pages, and cannot achieve true cross-page communication, not just parent-child page communication.
[0081] In order to solve the above technical problems, the present invention provides a cross-application communication method, the core concept of which is: a main application and multiple sub-applications are created on the front end, each application corresponds to a different page, when sending a message, the first sub-application determines whether the target channel corresponding to the message and the corresponding second sub-application exist according to the application data stored locally; when the second sub-application and the target channel exist, the first sub-application broadcasts the message; the second sub-application receives the message and sends the message to the page corresponding to the second sub-application through the target channel. It realizes true cross-page communication, not just parent-child page communication, can ensure the communication needs between multiple front-end pages of large enterprises, reduce server pressure, and has the characteristics of wide applicability, safety and efficiency. At the same time, it conforms to the development trend of front-end microservices and can provide support for collaborative development of different front-end teams.
[0082] Reference Figure 1 , shows a flow chart of steps of a cross-application communication method provided by an embodiment of the present invention, which is applied to a front end, wherein the front end creates a main application and multiple sub-applications, each application corresponding to a different page, and the method may specifically include the following steps:
[0083] Step 101: When performing a channel subscription operation, the sub-application sends a subscription event to the main application; the main application updates application data according to the subscription event.
[0084] In the embodiment of the present invention, the front end can create a main application and multiple sub-applications, and one application corresponds to one page. Figure 2The figure shows an architecture diagram of a cross-application communication method provided by an embodiment of the present invention, in which functions such as subscribing to channels, canceling subscriptions, and sending messages can be set in the page; methods such as registration, subscription, canceling subscriptions, sending, broadcasting, synchronizing data, and link adaptation can be configured in the application layer, and the application layer handles the interaction with the page, performs some data checks, and security checks; multiple links such as link A, link B, link C, link D, and link E can be configured in the link layer, and the link layer is responsible for encapsulating browser difference lines and providing links for the application layer and the implementation layer; application modules, subscription modules, message modules, competition mechanisms, heartbeat mechanisms, compression modules, and other modules can be configured in the implementation layer, and the implementation layer is responsible for application management, subscription management, message management and sending, and ensuring that the method runs stably and reliably; application lists, subscription lists, message queues, and the like can be configured in the data layer.
[0085] Exemplarily, when performing a channel subscription operation, the sub-application may send a channel subscription event to the main application to notify the main application of the channel subscription; the main application updates the application data according to the channel subscription event.
[0086] In a preferred embodiment of the present invention, step 101 may include the following sub-steps:
[0087] Sub-step S11, when performing a subscription channel operation, the sub-application determines whether the subscription channel exists in the corresponding subscription list; if the subscription channel does not exist, the sub-application sends a subscription event to the main application and adds the subscription channel to the subscription list; the main application broadcasts the subscription event and updates the application data; the sub-application receives the broadcast of the subscription event and synchronizes the application data.
[0088] For example, the page corresponding to the sub-application can call the channel subscription method of the sub-application. When performing a cancel subscription operation, the page can call the channel subscription method of the sub-application, and the sub-application can first determine whether the subscription channel exists in the subscription list. If it is determined that there is no subscription channel in the subscription list, the subscription channel can be obtained from the locally stored data, added to the subscription list, and a subscription event can be sent to the main application at the same time to notify the main application of the addition of the subscription channel.
[0089] For example, when sub-application 1 subscribes to channel A, it first determines whether channel A exists in the subscription list. If channel A does not exist, sub-application 1 obtains channel A from the locally stored data, notifies the main application to subscribe to the channel A event, and then adds channel A to the subscription list.
[0090] After the sub-application sends a subscription event to the main application, the main application can broadcast the subscription event to all sub-applications, update the application data, and maintain the subscription list. The sub-application can receive the broadcast of the subscription event and synchronize the application data. For example, in page 1, add a subscription to channel A to sub-application 1, the main application adds it to the global broadcast channel A, sub-application 1 synchronizes the application data, and the subscription list is updated; add a subscription to channel B to sub-application 2, the main application adds it to the global broadcast channel B, and the subscription list is updated again.
[0091] In the embodiment of the present invention, in order to ensure the stable operation of the communication method, a synchronization mechanism can be set at the data layer. When the main application registers and subscribes to the channel of the sub-application, it will modify the application data and broadcast the data synchronously. After receiving the broadcast, the sub-application will synchronize the application data.
[0092] Step 102: When performing a channel unsubscription operation, the sub-application sends an unsubscription event to the main application; the main application updates application data according to the subscription event.
[0093] Exemplarily, when performing an unsubscription operation, the sub-application may send an unsubscription event to the main application 1 to notify the main application of the unsubscription of the channel; the main application updates the application data according to the unsubscription channel event.
[0094] In a preferred embodiment of the present invention, step 102 may include the following sub-steps:
[0095] Sub-step S21, when performing a unsubscribe operation, the sub-application determines whether a target channel exists in the corresponding subscription list; if the target channel exists, the sub-application sends an unsubscribe event to the main application, and cancels the target channel in the subscription list; the main application broadcasts the unsubscribe event and updates the application data; the sub-application receives the broadcast of the unsubscribe event and synchronizes the application data.
[0096] Exemplarily, the page corresponding to the sub-application can call the sub-application's unsubscribe method. When performing an unsubscribe operation, the page can call the sub-application's unsubscribe channel method, and the sub-application can first determine whether the target channel exists in the subscription list. If it is determined that the target channel exists in the subscription list, the subscription of the target channel can be unsubscribed from the locally stored data, and the target channel can be unsubscribed from the subscription list, and an unsubscribe event can be sent to the main application at the same time to notify the main application of the cancellation of the target channel.
[0097] For example, when sub-application 1 unsubscribes from channel A, it first determines whether channel A is in the subscription list. If channel A exists, sub-application 1 unsubscribes from channel A from the locally stored data, notifies the main application of the unsubscribe event for channel A, and then removes channel A from the subscription list.
[0098] After the sub-application sends the unsubscribe event to the main application, the main application can broadcast the unsubscribe event to all sub-applications, update the application data, and maintain the subscription list. The sub-application can receive the broadcast of the unsubscribe event and synchronize the application data. For example, on page 1, if you cancel the subscription to channel A from sub-application 1, the main application broadcasts the cancellation of channel A globally, sub-application 1 synchronizes the application data, and the subscription list is updated; if you cancel the subscription to channel B from sub-application 2, the main application broadcasts the cancellation of channel B globally, and the subscription list is updated again.
[0099] In the embodiment of the present invention, in order to ensure the stable operation of the communication method, a synchronization mechanism can be set at the data layer. When the main application registers or cancels the subscription of the sub-application, it will modify the application data and broadcast the data synchronously. After receiving the broadcast, the sub-application will synchronize the application data.
[0100] Step 103, when performing a message sending operation, the first sub-application determines whether a target channel corresponding to the message and a corresponding second sub-application exist based on the application data.
[0101] Exemplarily, when performing a message sending operation, the first sub-application may determine whether the target channel corresponding to the message and the second sub-application exist through the subscription list and the application list of the data layer.
[0102] In an embodiment of the present invention, the method may further include: the main application regularly broadcasts heartbeat messages; the sub-application synchronizes the heartbeat time of the main application; if the first sub-application detects that the heartbeat time of the main application is abnormal, it initiates a competitive broadcast; and determines the sub-application with the highest application strength as the main application.
[0103] For example, in order to ensure the stable operation of the communication method, the embodiment of the present invention can set a heartbeat mechanism at the data layer. Therefore, during the communication process, the main application can broadcast heartbeat messages regularly, and the sub-application can synchronize the heartbeat time of the main application. The sub-application will check the status of the main application before each operation. If the main application is found to be offline, all data will be stored, the application will be locked, and a competitive broadcast will be initiated to determine the sub-application with the highest application strength as the main application.
[0104] Exemplarily, in order to ensure the stable operation of the communication method, the data layer of the embodiment of the present invention can also set a competition mechanism. For example, if sub-application 1 is the main application, when sub-application 2 detects that the heartbeat time of the main application is abnormal, sub-application 2 initiates a competition broadcast, and other sub-applications receive the competition broadcast and respond. Each sub-application randomly generates a corresponding application strength. Within a certain heartbeat cycle, the sub-application with the highest application strength is determined to become the main application, and a notification of switching the main application is broadcast. After receiving the switching notification, the sub-application switches to the main application, unlocks, and enters the normal process.
[0105] Step 104: When the second sub-application and the target channel exist, the first sub-application broadcasts a message.
[0106] Exemplarily, when it is determined that the second sub-application and the target channel exist, the first sub-application may broadcast the message to all sub-applications.
[0107] Sub-step S31, in the case where the second sub-application and the target channel exist, the first sub-application compresses the message and sends the compressed message to the link adapter; the first sub-application selects a link and broadcasts the compressed message through the link.
[0108] Exemplarily, when the second sub-application and the target channel exist, the first sub-application can compress the message and send the compressed message to the link adapter. The link adapter can select the most efficient link through the user environment, and the first sub-application can broadcast the compressed message through the link.
[0109] As an example, the link layer is responsible for encapsulating browser difference lines and providing links for the application layer and the implementation layer. The link adapter can be configured with multiple links such as link A, link B, link C, link D, link E, etc., where each link is suitable for a different browser. Those skilled in the art can select a link according to actual conditions, and the embodiments of the present invention are not limited to this.
[0110] The present invention integrates multiple link implementation modes, classifies and processes differences in user browsers, can select the most efficient solution that can be implemented according to the client browser, and can adapt to all browsers.
[0111] In addition, the sent message may also include the message number, message source, usage path, and target path, etc. For example, in page 1, through sub-application 1, a message is sent to channel B, the message content is "message 1, from page 1, using channel B", the target sub-application 2, sub-application 3; in page 1, through sub-application 1, a message is sent to channel A, the message content is "message 2, from page 1, using channel A", the target sub-application 2.
[0112] Those skilled in the art should understand that the message content sent above is only an example of the present invention, and those skilled in the art may also send other message contents, which is not limited in the present invention.
[0113] Step 105: The second sub-application receives the message and sends the message to a page corresponding to the second sub-application through the target channel.
[0114] Exemplarily, the second sub-application may receive a message broadcasted by the first sub-application, and send the message to a page corresponding to the second sub-application through a target channel.
[0115] For example, in page 1, a message is sent to channel B through sub-application 1. The message content is "Message 1, from page 1, using channel B", and the target sub-applications are sub-applications 2 and 3. Sub-application 1 compresses the message and broadcasts it. Sub-applications 2 and 3 receive the message and forward it to corresponding pages 2 and 3 respectively.
[0116] In an embodiment of the present invention, the method may further include: the second sub-application receives the message and determines whether to subscribe to the target channel; if so, the second sub-application decompresses the message and sends the decompressed message to a page corresponding to the second sub-application through the target channel; if not, discarding the message.
[0117] Exemplarily, after receiving the message broadcast by the first sub-application, the second sub-application can determine whether it has subscribed to the target channel. If it has subscribed to the target channel, it decompresses the message and sends the decompressed message to the page corresponding to the second sub-application through the target channel; if the second sub-application has not subscribed to the target channel, the message is discarded.
[0118] For example, in page 1, a message is sent to channel B through sub-application 1, and the message content is "Message 1, from page 1, using channel B", and the target sub-applications are sub-applications 2 and 3; sub-application 1 compresses the message and broadcasts it, and after sub-applications 2 and 3 determine that the message is sent to themselves and have subscribed to channel B, they receive and decompress the message respectively, and forward the decompressed message to the corresponding pages 2 and 3 through channel B respectively.
[0119] For example, in page 1, a message is sent to channel A through sub-application 1. The message content is "Message 2, from page 1, using channel A", and the target sub-application 2; sub-application 1 compresses the message and broadcasts it to all sub-applications. Sub-application 2 determines that the message is sent to itself, but it has not subscribed to channel A, so the message is ignored.
[0120] It should be noted that, in the embodiment of the present invention, for the sake of communication security, a discard strategy is adopted instead of an exception strategy, thereby preventing users from traversing channels or spamming messages.
[0121] In the embodiment of the present invention, the method may further include: if the second sub-application and / or the target channel does not exist, the first sub-application discards the message.
[0122] For example, if the second sub-application and / or the target channel do not exist, the first sub-application can discard the message; the corresponding page of the first sub-application is closed, and after the page is closed, the first sub-application is synchronously logged out. For example, if sub-application 1 determines that the target channel and / or the target sub-application do not exist, the message can be discarded, the communication ends, page 1 is closed, and sub-application 1 is synchronously closed.
[0123] In an embodiment of the present invention, the method may further include: when the third sub-application is created, the third sub-application broadcasts a main application query request message; when the main application receives the main application query request information, it broadcasts the main application query response information and updates the application data; the third sub-application receives the query response information within a preset heartbeat cycle and synchronizes the application data.
[0124] For example, the present invention can create a new sub-application according to the application parameters passed in by the page. The creation process can check whether the user's APPNAME, SECURE, and DOMAIN match. If they do not match, an exception is thrown. If they match, initialization is performed. The newly created sub-application is in a locked state and cannot provide external services. Data synchronization and identification of the main application are required.
[0125] As an example, after the third sub-application is created, the third sub-application can broadcast the main application query request message; when the main application receives the main application query request information, it can broadcast the main application query response information and update the application data; within the preset heartbeat period, the third sub-application receives the query response information, and can synchronize the application data to complete the initialization.
[0126] For example, after sub-application 4 is created, it first broadcasts the main application query request message. When the main application receives the main application query request information, it can broadcast the main application query response information and update the application data. If sub-application 4 receives the query response information within the preset heartbeat cycle, the information in the response can be synchronized to the data part of this application to complete the initialization process.
[0127] In addition, after the newly created sub-application is initialized successfully, it can notify the page that the application is ready. After the page receives the application ready message, it can perform other operations such as subscribing to channels and unsubscribing.
[0128] In the embodiment of the present invention, the method may further include: if the third sub-application does not receive the query response information within a preset heartbeat period, initiating a contention broadcast and determining the sub-application with the highest application strength as the main application.
[0129] In an embodiment of the present invention, if the third sub-application does not receive the query response information of the main application within the preset heartbeat period, the main application is determined to be abnormal, a competition broadcast is initiated, the main application is re-determined, and the sub-application with the highest application strength is determined to become the main application.
[0130] In an embodiment of the present invention, by creating a main application and multiple sub-applications in the front end, each application corresponds to a different page. When performing a target channel subscription operation, the sub-application can determine whether the target channel exists in the corresponding subscription list; if the target channel does not exist, the sub-application can send a subscription event to the main application and add the target channel to the subscription list; the main application can broadcast the subscription event and update the application data; the sub-application can receive the broadcast of the subscription event and synchronize the application data; when performing a message sending operation, the first sub-application can determine whether the target channel corresponding to the message and the corresponding second sub-application exist according to the application data; if the second sub-application and the target channel exist, the first sub-application broadcasts the message; the second sub-application can receive the message and send the message to the page corresponding to the second sub-application through the target channel; thereby achieving true cross-page communication, not just parent-child page communication, which can ensure the communication needs between multiple front-end pages of large enterprises and reduce server pressure. At the same time, it conforms to the development trend of front-end microservices, is suitable for collaborative development and mutual calls by multiple front-end teams, and meets the needs of business development.
[0131] It should be noted that, for the sake of simplicity, the method embodiments are described as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present invention are not limited by the order of the actions described, because according to the embodiments of the present invention, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present invention.
[0132] Reference Figure 3 , shows a structural block diagram of a cross-application communication device provided by an embodiment of the present invention, which is applied to a front end, wherein the front end creates a main application and multiple sub-applications, each application corresponds to a different page, and specifically may include the following modules:
[0133] The first sending module 201 is used for, when performing a channel subscription operation, the sub-application sends a subscription event to the main application; the main application updates application data according to the subscription event;
[0134] The second sending module 202 is used for, when performing a channel unsubscription operation, the sub-application sends an unsubscription event to the main application; the main application updates application data according to the subscription event;
[0135] The first judgment module 203 is used for, when performing a message sending operation, the first sub-application to judge whether a target channel corresponding to the message and a corresponding second sub-application exist according to the application data synchronized with the main application;
[0136] A first broadcast module 204, configured to broadcast a message by the first sub-application when the second sub-application and the target channel exist;
[0137] The first receiving module 205 is used for the second sub-application to receive the message and send the message to the page corresponding to the second sub-application through the target channel.
[0138] In an embodiment of the present invention, the first sending module 201 may further include:
[0139] A second judgment module is used for, when performing a channel subscription operation, the sub-application to judge whether the subscription channel exists in the corresponding subscription list;
[0140] A first sending submodule, configured to, when the subscription channel does not exist, cause the sub-application to send a subscription event to the main application and add the subscription channel to the subscription list;
[0141] A first update module, used for the main application to broadcast the subscription event and update the application data;
[0142] The second receiving module is used for the sub-application to receive the broadcast of the subscription event and synchronize the application data.
[0143] In an embodiment of the present invention, the second sending module 202 may further include:
[0144] A third judgment module, used for the sub-application to judge whether a target channel exists in the corresponding subscription list when performing a subscription cancellation operation;
[0145] A second sending submodule, configured to, when the target channel exists, cause the sub-application to send a subscription cancellation event to the main application, and cancel the target channel in the subscription list;
[0146] A second updating module, configured for the main application to broadcast the unsubscribe event and update application data;
[0147] The third receiving module is used for the sub-application to receive the broadcast of the unsubscribe event and synchronize the application data.
[0148] In one embodiment of the present invention, the device may further include:
[0149] A second broadcast module, used for the main application to broadcast heartbeat messages regularly;
[0150] A synchronization module, used for the sub-application to synchronize the heartbeat time of the main application;
[0151] The first initiating module is configured to initiate a contention broadcast if the first sub-application detects that the heartbeat time of the main application is abnormal; and determine the sub-application with the highest application strength as the main application.
[0152] In one embodiment of the present invention, the first broadcast module 204 may include:
[0153] A compression module, configured to compress the message by the first sub-application when the second sub-application and the target channel exist, and send the compressed message to the link adapter;
[0154] A selection module is used for the first sub-application to select a link and broadcast and send the compressed message through the link.
[0155] In one embodiment of the present invention, the device may further include:
[0156] A fourth determination module, configured for the second sub-application to receive the message and determine whether to subscribe to the target channel;
[0157] If yes, the second sub-application decompresses the message and sends the decompressed message to the page corresponding to the second sub-application through the target channel;
[0158] If not, the message is discarded.
[0159] In one embodiment of the present invention, the device may further include:
[0160] A discarding module is configured to cause the first sub-application to discard the message if the second sub-application and / or the target channel does not exist.
[0161] In one embodiment of the present invention, the device may further include:
[0162] A third broadcast module, configured to broadcast a main application query request message to the third sub-application after the third sub-application is created;
[0163] a fourth broadcast module, configured to broadcast main application query response information and update the application data after the main application receives the main application query request information;
[0164] The fourth receiving module is used for the third sub-application to receive the query response information within a preset heartbeat period and synchronize the application data.
[0165] In one embodiment of the present invention, the device may further include:
[0166] The second initiating module is configured to initiate a contention broadcast if the third sub-application does not receive the query response information within a preset heartbeat period, and determine the sub-application with the highest application strength as the main application.
[0167] In an embodiment of the present invention, by creating a main application and multiple sub-applications in the front end, each application corresponds to a different page. When performing a target channel subscription operation, the sub-application can determine whether the target channel exists in the corresponding subscription list; if the target channel does not exist, the sub-application can send a subscription event to the main application and add the target channel to the subscription list; the main application can broadcast the subscription event and update the application data; the sub-application can receive the broadcast of the subscription event and synchronize the application data; when performing a message sending operation, the first sub-application can determine whether the target channel corresponding to the message and the corresponding second sub-application exist according to the application data; if the second sub-application and the target channel exist, the first sub-application broadcasts the message; the second sub-application can receive the message and send the message to the page corresponding to the second sub-application through the target channel; thereby achieving true cross-page communication, not just parent-child page communication, which can ensure the communication needs between multiple front-end pages of large enterprises and reduce server pressure. At the same time, it conforms to the development trend of front-end microservices, is suitable for collaborative development and mutual calls by multiple front-end teams, and meets the needs of business development.
[0168] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0169] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0170] In the description of the present invention, it is to be understood that the terms "first" and "second" are used for descriptive purposes only and are not to be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of the present invention, unless otherwise specified, "plurality" means two or more.
[0171] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0172] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.
[0173] The above is a detailed introduction to a cross-application communication method and device provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for general technical personnel in this field, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A cross-application communication method, characterized in that: The front end is applied to create a main application and multiple sub-applications, each application corresponds to a different page, and the method includes: When performing a channel subscription operation, the sub-application sends a subscription event to the main application; the main application updates application data according to the subscription event; When performing a channel unsubscription operation, the sub-application sends an unsubscription event to the main application; the main application updates application data according to the subscription event; When performing a message sending operation, the first sub-application determines whether a target channel corresponding to the message and a corresponding second sub-application exist according to the application data synchronized with the main application; In the case where the second sub-application and the target channel exist, the first sub-application broadcasts and sends a message; The second sub-application receives the message, and sends the message to a page corresponding to the second sub-application through the target channel; Wherein, the method further comprises: The main application broadcasts heartbeat messages regularly; The sub-application synchronizes the heartbeat time of the main application; If the first sub-application detects that the heartbeat time of the main application is abnormal, a competitive broadcast is initiated; the sub-application with the highest application strength is determined as the main application; and the application strength is randomly generated for the sub-application.
2. The method according to claim 1, characterized in that When performing a channel subscription operation, the sub-application sends a subscription event to the main application; The main application updates the application data according to the subscription event, including: When performing a channel subscription operation, the sub-application determines whether the subscription channel exists in the corresponding subscription list; In the case that the subscription channel does not exist, the sub-application sends a subscription event to the main application, and adds the subscription channel to the subscription list; The main application broadcasts the subscription event and updates the application data; The sub-application receives the broadcast of the subscription event and synchronizes the application data.
3. The method according to claim 1, characterized in that When performing a channel unsubscription operation, the sub-application sends an unsubscription event to the main application; The main application updates the application data according to the subscription event, including: When performing a subscription cancellation operation, the sub-application determines whether a target channel exists in the corresponding subscription list; In the case that the target channel exists, the sub-application sends a subscription cancellation event to the main application, and cancels the target channel in the subscription list; The main application broadcasts the unsubscribe event and updates application data; The sub-application receives the broadcast of the unsubscribe event and synchronizes the application data.
4. The method according to claim 1, characterized in that In the case where the second sub-application and the target channel exist, the first sub-application broadcasts and sends a message, including: In the case where the second sub-application and the target channel exist, the first sub-application compresses the message and sends the compressed message to the link adapter; The first sub-application selects a link, and broadcasts the compressed message through the link.
5. The method according to claim 4, characterized in that Also includes: The second sub-application receives the message and determines whether to subscribe to the target channel; If yes, the second sub-application decompresses the message and sends the decompressed message to the page corresponding to the second sub-application through the target channel; If not, the message is discarded.
6. The method according to claim 4, characterized in that Also includes: If the second sub-application and / or the target channel does not exist, the first sub-application discards the message.
7. The method according to claim 1, characterized in that Also includes: When the third sub-application is created, the third sub-application broadcasts a main application query request message; When the main application receives the main application query request information, the main application broadcasts the main application query response information and updates the application data; The third sub-application receives the query response information within a preset heartbeat period and synchronizes the application data.
8. The method according to claim 7, characterized in that Also includes: If the third sub-application does not receive the query response information within the preset heartbeat period, a contention broadcast is initiated to determine the sub-application with the highest application strength as the main application.
9. A cross-application communication device, characterized in that: The front end is applied to create a main application and multiple sub-applications, each application corresponds to a different page, and the device includes: A first sending module, configured to enable the sub-application to send a subscription event to the main application when performing a channel subscription operation; and the main application to update application data according to the subscription event; A second sending module is used for, when performing a channel unsubscription operation, the sub-application sends an unsubscription event to the main application; the main application updates application data according to the subscription event; A first judgment module, configured to, when performing a message sending operation, determine, by the first sub-application, whether a target channel corresponding to the message and a corresponding second sub-application exist according to application data synchronized with the main application; A first broadcast module, configured to broadcast a message by the first sub-application when the second sub-application and the target channel exist; A first receiving module, configured for the second sub-application to receive the message and send the message to a page corresponding to the second sub-application through the target channel; Wherein, the device further comprises: A second broadcast module, used for the main application to broadcast heartbeat messages regularly; A synchronization module, used for the sub-application to synchronize the heartbeat time of the main application; The first initiating module is used to initiate a competitive broadcast if the first sub-application detects that the heartbeat time of the main application is abnormal; determine the sub-application with the highest application strength as the main application, and the application strength is randomly generated by the sub-application.