Communication method, device, computing equipment and storage medium based on WebSocket and BroadcastChannel
By using the BroadcastChannel API to simulate WebSocket communication on the front end, the data push and reception problems when the backend interface is unavailable is solved, and WebSocket communication in a pure front-end environment is realized, ensuring the normal operation of the spread-page application.
Patent Information
- Application Number
- CN202411017741.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-29
- Publication Date
- 2024-11-05
- Estimated Expiration
- 2044-07-29
AI Technical Summary
In front-end web application development, when the back-end interface is not provided in time or cannot provide services, the front-end cannot realize WebSocket-based data communication in a pure front-end environment, especially in spanning the page, it is difficult to simulate effective data push and reception.
Use the BroadcastChannel API to create channel instances on the front end, simulate the sending and listening of WebSocket events, and implement event processing by binding the listening function to ensure communication between the same-origin pages. Even when the backend cannot provide WebSocket service Send and receive.
Pure front-end WebSocket message sending and receiving in single pages or homologous cross-label multiple pages, ensuring the normal processing of business logic and the continuity of development, and avoiding dependence on real back-end services.
Smart Images

Figure CN118555275B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a communication method, device, computing equipment and storage medium based on WebSocket and BroadcastChannel. Background Art
[0002] WebSocket is a protocol for full-duplex communication over a single TCP connection. WebSocket makes data exchange between clients and servers much simpler, allowing the server to actively push data to the client. In the WebSocket API, the browser and server only need to complete a handshake to directly create a persistent connection between the two and perform two-way data transmission. Currently, in the development of front-end web applications, the WebSocket communication protocol is used for application scenarios that require real-time communication, data push, real-time monitoring, etc., to achieve business needs with high real-time and flexibility requirements.
[0003] In the actual front-end development process, we usually face the situation that the back-end interface cannot be provided in time, or after the development is completed, the demonstration environment cannot provide back-end services. In short, when the back-end interface cannot be provided, the front-end often uses mock data, such as editing JSON data or JS scripts, building mock services, etc., to retrieve simulated data through requests to continue business logic processing. However, the above solutions are mostly applied to the WebSocket data communication effect in the single-page case. In the case of cross-page, if it does not rely on the back-end WebSocket communication service, the data communication effect based on WebSocket cannot be achieved in a pure front-end environment. Summary of the invention
[0004] In order to solve the above problems, an embodiment of the present invention provides a communication method, apparatus, computing device and storage medium based on WebSocket and BroadcastChannel. The method achieves independence from the backend WebSocket communication service, simulates the WebSocket information sending and pushing form in a pure front-end environment, and can send and receive information in a single-page application or multiple pages across the same source tabs.
[0005] In a first aspect, an embodiment of the present invention provides a communication method based on WebSocket and BroadcastChannel, including:
[0006] Establish a communication connection between the front-end client and the back-end server based on WebSocket;
[0007] When the communication connection is not successfully established, a channel instance is created on the front-end client based on BroadcastChannel to simulate communication; the channel instance is used to simulate the sending of events between the front-end client and the back-end server and to implement the monitoring of the events by binding a monitoring function.
[0008] In a second aspect, an embodiment of the present invention further provides a communication device based on WebSocket and BroadcastChannel, including:
[0009] The communication module is used to establish a communication connection between the front-end client and the back-end server based on WebSocket;
[0010] The simulation communication module is used to create a channel instance based on BroadcastChannel on the front-end client to simulate communication when the communication connection is not successfully established; the channel instance is used to simulate the sending of events between the front-end client and the back-end server and to monitor the events by binding a monitoring function.
[0011] In a third aspect, an embodiment of the present invention further provides a computing device, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, it implements any of the above-mentioned communication methods based on WebSocket and BroadcastChannel.
[0012] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed in a computer, the computer is caused to execute any one of the above-mentioned communication methods based on WebSocket and BroadcastChannel.
[0013] In a fifth aspect, an embodiment of the present invention further provides a computer program product, comprising computer instructions, which, when executed by a processor, implement the steps of the method described in any first aspect of this specification.
[0014] The embodiment of the present invention provides a communication method, device, computing equipment and storage medium based on WebSocket and BroadcastChannel. The method is based on the original WebSocket communication docking coding logic mode. When the communication connection is not successfully established between the front-end client and the back-end server, the front-end client can apply BroadcastChannel to create a channel instance, and all the sending events and listening events in the WebSocket can be converted into channel instances with corresponding event names, so as to simulate the sending of events between the front-end client and the back-end server through the channel instance, and bind the listening function in the channel instance to realize the listening of the event, simulating the communication between the front-end and the back-end. In this way, the present invention applies the BroadcastChannel API to communicate between pages of the same source. When the back-end server cannot provide the WebSocket interface service, the back-end is used to monitor the front-end sending events and the back-end is used to simulate the sending events to the front-end through the channel instance, so as to realize the sending and receiving of messages based on WebSocket in a single page or multiple pages across tags of the same source. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0016] Figure 1 It is a flow chart of a communication method based on WebSocket and BroadcastChannel provided by one embodiment of the present invention;
[0017] Figure 2 is a hardware architecture diagram of a computing device provided by an embodiment of the present invention;
[0018] Figure 3 It is a structural diagram of a communication device based on WebSocket and BroadcastChannel provided by an embodiment of the present invention;
[0019] Figure 4 This is a structural diagram of another communication device based on WebSocket and BroadcastChannel provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0020] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0021] As mentioned above, for front-end web application development scenarios that use the WebSocket communication protocol, there may be situations where the back-end interface fails to be provided in a timely manner during the actual front-end development process, or the front-end demonstration environment cannot provide back-end services after the front-end web development is completed. Once this happens, the front-end often uses mock data (for example, editing JSON data or JS scripts, building mock services, etc.), and retrieves the simulated data through requests in order to continue the front-end business logic processing.
[0022] However, this solution is mostly used in the simulation of data interfaces requested by HTTP protocol, and in the simulation of WebSocket data communication effects in single-page situations. In the case of cross-page situations, a certain page data request needs to rely on other pages of the same source to interact and then request communication. This solution cannot effectively simulate data requests.
[0023] To solve the above problems, you can consider using BroadcastChannelAPI as the core to achieve cross-tab same-origin page communication in the same browser. By writing a specified message simulation script, a series of functions are encapsulated, which imitate the behavior of WebSocket. Specifically, a series of key functions with the same name as those in the actual application WebSocket library can be encapsulated, so as to simulate the message sending and receiving process of WebSocket in a pure front-end environment without actually establishing a WebSocket connection.
[0024] It should be noted that WebSocket is a network communication protocol that supports two-way communication. The server can actively push information to the client, and the client can also actively send information to the server.
[0025] BroadcastChannel: An API for implementing cross-tab communication in the browser. Allows messages and data to be shared between different tabs and between all tabs in the same browser session.
[0026] Mock: refers to replacing real data with simulated virtual data, so that front-end development and docking can continue when the back-end interface is not completed or the data is abnormal.
[0027] The following is the concept of the present invention: Figure 1 As shown, an embodiment of the present invention provides a communication method based on WebSocket and BroadcastChannel, the method comprising:
[0028] Step 100, establishing a communication connection between the front-end client and the back-end server based on WebSocket;
[0029] Step 102, when the communication connection is not successfully established, a channel instance is created on the front-end client based on BroadcastChannel to simulate communication; the channel instance is used to simulate the sending of events between the front-end client and the back-end server and to implement event monitoring by binding a monitoring function.
[0030] In the present invention, based on the original WebSocket communication docking coding logic, when the communication connection is not successfully established between the front-end client and the back-end server, the front-end client can apply BroadcastChannel to create a channel instance, and all the sending events and listening events in WebSocket can be converted into channel instances with corresponding event names, so as to simulate the sending of events between the front-end client and the back-end server through the channel instance, and bind the listening function in the channel instance to realize the listening of the event, simulating the communication between the front-end and the back-end. In this way, the present invention applies the BroadcastChannelAPI to communicate between pages of the same source. When the back-end server cannot provide the WebSocket interface service, the back-end is used to monitor the front-end sending events and the back-end sends events to the front-end through the channel instance, so as to simulate the sending and receiving of WebSocket messages by using the pure front-end in a single page or multiple pages across tags of the same source.
[0031] Described below Figure 1 How the various steps are performed.
[0032] In step 100, a WebSocket-based communication connection is established between a front-end client and a back-end server using existing technology, so that the server can actively push information to the client, and the client can also actively send information to the server.
[0033] Specifically, when the WebSocket-based communication connection is successfully established, that is, when the interface of the back-end server can provide or can provide back-end services in a timely manner, the front-end client and the back-end server communicate using the communication connection, and common interaction methods include: the front-end client sends event A to the back-end server, the back-end server listens to event A and performs corresponding business logic processing after receiving the data to obtain new business data; then, based on the new business data, the back-end server sends event B to the front-end client, and the front-end client listens to event B and performs front-end business processing on the new business data after receiving the new business data.
[0034] With respect to step 102, a channel instance is created based on BroadcastChannel on the front-end client to simulate communication, including:
[0035] Obtain data and a first name included in a first event sent by a front-end client to a back-end server;
[0036] Create a corresponding first channel instance based on the first name, and bind a first listening function;
[0037] Create a temporary channel instance with the same name as the first channel instance on the front-end client;
[0038] When data is sent to the backend server through the temporary channel instance, the first monitoring function is triggered and the temporary channel instance is destroyed.
[0039] In the present invention, when simulating the front-end client sending an event to the back-end server, sending a message to the first channel instance will not trigger the monitoring function of the channel instance of this page. Therefore, in order to process the data of the first event, after sending the data through the first channel instance, it is necessary to create a temporary channel instance with the same name and send data again to trigger the monitoring execution of the first monitoring function and destroy the temporary channel instance. In this way, the front-end sending an event to the back-end is simulated through the first channel instance.
[0040] In a preferred implementation, creating a corresponding first channel instance based on the first name includes the following two cases:
[0041] The first case: when the application channel instance library stores the first channel instance corresponding to the first name, the first channel instance is called from the application channel instance library;
[0042] The second situation: when the application channel instance library does not store the first channel instance corresponding to the first name, it is necessary to determine the simulation data script based on the first name, generate the first channel instance based on the simulation data script, and store the first channel instance and the first name correspondingly in the application channel instance library.
[0043] It should be noted that the first channel instance has been bound to the corresponding listening function when it is created based on the simulation data script.
[0044] Specifically, since the front-end client and the back-end server will define event names during normal communication, in order to simulate the corresponding events, the corresponding simulation data scripts will be constructed in advance for the defined event names, that is, the simulation data scripts corresponding to different event names are different, so that the simulation data script can be used to create the corresponding channel instance and bind the listening function.
[0045] In a preferred embodiment, the created channel instances and the event names corresponding to the channel instances are stored in the application channel instance library in a one-to-one correspondence; and the created channel instances and the listening functions bound to them are stored in the event library in a one-to-one correspondence. Different event names correspond to different channel instances, and the listening functions bound to the same channel instance in different application scenarios or modules are different.
[0046] With respect to step 102, a channel instance is created based on BroadcastChannel on the front-end client to simulate communication, including:
[0047] The front-end client simulates the simulated data generated by the back-end server after performing business processing when monitoring the first event;
[0048] Generate a second event according to the simulation data; wherein the second event is sent from the back-end server to the front-end client;
[0049] Create a corresponding second channel instance based on the second name of the second event, and bind a second listening function;
[0050] When the simulated data is sent to the front-end client through the second channel instance, the second monitoring function is triggered.
[0051] In the present invention, since the communication connection is not successfully established, the backend cannot monitor the first event, and thus cannot process the first event to obtain new data, and the frontend cannot monitor the new data, so it is necessary to rely on the frontend client to simulate the backend server and replace the new data with simulated data. The simulated data has the same format as the new data. In this way, after obtaining the simulated data, the second event including the simulated data can be simulated to be sent from the backend server to the frontend client through the second channel instance, and the frontend client monitors the second event to trigger the second monitoring function bound to the second channel instance, simulating the frontend monitoring the sending event of the backend, thereby realizing the business logic processing of the frontend client, and continuing the development and docking of the frontend.
[0052] In a preferred embodiment, the simulation data is determined by the following method:
[0053] The front-end client creates a corresponding first channel instance based on the first name of the first event;
[0054] A first listening function bound to a first channel instance is triggered based on a first event, and simulation data is obtained by executing the first listening function; wherein the first listening function is used to simulate business processing of a backend server.
[0055] It should be noted that, before determining the simulation data, the step further includes: applying a channel instance library to store a first channel instance corresponding to the first name.
[0056] In the present invention, only after the application channel instance library stores the first channel instance corresponding to the first name can the first channel instance be called to send the first event. The first channel instance constructs the data included in the first event by triggering the bound first listening function, that is, simulates the business processing of the back-end server, thereby obtaining simulated data with the same data format as returned by the back-end server.
[0057] In a preferred embodiment, it also includes:
[0058] Receive a cancellation instruction including the target name and target listening function;
[0059] According to the cancel instruction, the channel instance corresponding to the target name is found from the application channel instance library, and the target listening function bound to the channel instance is found from the event library for deletion.
[0060] In the present invention, the event monitoring of a specified event and a specified monitoring function can be canceled by deleting the target name and the target monitoring function.
[0061] In a preferred embodiment, it also includes:
[0062] receiving a clear instruction including a target name;
[0063] According to the clear instruction, the channel instance corresponding to the target name is found from the application channel instance library, and the listening function bound to the channel instance is found from the event library for deletion.
[0064] In the present invention, the cancellation of all event monitoring of a specified event name can be simulated by deleting all monitoring functions bound to the channel instance corresponding to the target name.
[0065] In a preferred embodiment, it also includes:
[0066] Receive a closing instruction;
[0067] According to the closing instruction, all channel instances in the application channel instance library are closed, and the application channel instance library and event library are cleared.
[0068] In the present invention, the closing of a WebSocket instance can be simulated by closing all channel instances and simultaneously clearing the application channel instance library and the event library.
[0069] In a preferred embodiment, the method further includes: uniformly encapsulating the communication connection established based on WebSocket and the channel instance;
[0070] When the front-end client receives a request event, it obtains the current user environment;
[0071] If the current user environment is that a communication connection is successfully established between the front-end client and the back-end server, the communication connection communicates the request event;
[0072] If the current user environment is that a communication connection is not successfully established between the front-end client and the back-end server, the request event is communicated based on the channel instance.
[0073] Specifically, the current user environment can be determined in the following two ways:
[0074] The first method: By defining global variables to specify the current WebSocket connection mode, determine whether the user environment is a real backend environment (that is, the communication connection has been successfully established) or a pure front-end simulation environment (that is, the communication connection has not been successfully established), and then return the corresponding communication connection (that is, the real instance) or channel instance established based on WebSocket according to the variable value.
[0075] The second method: The front-end client first tries to establish a WebSocket communication connection with the back-end server through real user environment detection. If the connection is normal, the front-end client continues to communicate with the real instance. If the connection is abnormal, the channel instance is returned for simulation.
[0076] In the present invention, by uniformly encapsulating the real instance and the channel instance established based on WebSocket, a Socket instance (including the real instance and the channel instance) is obtained, so that the corresponding Socket instance can be selected according to the real environment. There is no need to process the real back-end environment or the pure front-end simulation environment separately. The front-end can apply the Socket instance to write a set of business logic, thereby seamlessly connecting the back-end WebSocket and the pure front-end simulation data.
[0077] It should be noted that the present invention includes but is not limited to being applied to simulating the Socket.io library and other WebSocket related libraries to connect to WebSocket on the front-end client. When used, the method name and parameter definition can be changed according to the WebSocket library actually used. All WebSocket sending and listening events in web application development are converted into channel instances with corresponding event names, and the PostMessage method of the channel instance is applied to simulate the emit event sending method of the real instance in Socket.io, and the message listening function is bound to the channel instance to simulate the on event listening method of the real instance in Socket.io.
[0078] To further illustrate the present invention, taking Socket.io library as an example, the method of simulating the connection of Socket.io library with WebSocket on the front-end client includes: applying BroadcastChannel to simulate some key methods of Socket.io library, encapsulating Channel class, and defining two storage objects in the constructor of Channel class, list is used to store all channel instances, and event is used to store the listening functions of all listening class events. And define some key methods with the same name in Socket.io library to simulate listening and sending simulated data, and the parameters defined in the method are consistent with the corresponding method parameters in Socket.io library, so that no matter whether the communication connection is successfully established, the method of Socket.io library can be used for WebSocket communication.
[0079] Specifically, when the communication connection is not successfully established, the methods of simulating the Socket.io library to connect to WebSocket on the client include:
[0080] 1) Use the emit method to simulate the front-end client sending event A to the back-end server. This method receives the event name A and data. Generate the BroadcastChannel channel instance of the event name and the message listening function bound to the channel instance through the specified simulation data script corresponding to event A, store the channel instance a and the corresponding event A in list, and store the listening function of the channel instance a in event. Because using emit to send a message to the channel instance will not trigger the listening function bound to the channel on this page, a temporary channel instance with the same name must be created again after the emit method and data data must be sent to trigger the back-end server to listen to the event A and execute the listening function, and then the temporary channel is destroyed. In this way, the PostMessage of the channel instance is encapsulated as the sending method of WebSocket through this method.
[0081] 2) The on method simulates the front-end client listening to the back-end server's sending event B. This method receives the event name B and the callback event callback function to be executed by the front-end. By generating a channel instance corresponding to the event name B and storing it in the list, binding the channel message monitoring function (i.e., the callback event callback function), and triggering the callback callback function in the monitoring function, the callback callback function and the monitoring function are stored in the event, which is convenient for the subsequent removal of event monitoring.
[0082] It should be noted that, when monitoring the sending event B, the simulation data included in the sending event B needs to be simulated by the front end according to the above method.
[0083] 3) The off method is used to simulate the cancellation of event monitoring of the specified event name and the specified monitoring function. This method receives the specified event name and the previously bound monitoring function. By searching the channel instance of the specified event name in the list and searching the specified monitoring function corresponding to the channel instance in the event, the monitoring event corresponding to the specified monitoring function corresponding to the channel instance is removed, and the specified monitoring function in the event is deleted.
[0084] 4) The remove All Listeners method is used to simulate the cancellation of all event listeners for an event name. This method receives the event name, searches for the channel instance of the event name in the list, and all listener functions corresponding to the event name in the event, traverses all listener functions and removes them one by one, and deletes all listener function storages for the event name in the event.
[0085] 5) The close method is used to simulate closing a socket instance. It closes all channel instances in the list and clears the list and event storage.
[0086] In the present invention, the pure front-end simulates WebSocket information push, and when the back-end cannot provide WebSocket interface service, it replaces the back-end processing logic to return simulated data, ensuring that the front-end development or interface demonstration proceeds normally, and the front-end still uses the original WebSocket communication docking coding logic to perform interface docking. It is convenient to verify the accuracy of functions and ensure the smoothness of business in a pure front-end environment.
[0087] like Figure 2 , Figure 3 As shown, the embodiment of the present invention provides a communication device based on WebSocket and BroadcastChannel. The device embodiment can be implemented by software, or by hardware or a combination of software and hardware. From the hardware level, Figure 2As shown in FIG. 1 , a hardware architecture diagram of a computing device where a communication device based on WebSocket and BroadcastChannel is provided in an embodiment of the present invention is located, except that Figure 2 In addition to the processor, memory, network interface, and non-volatile memory shown in the figure, the computing device in which the device is located in the embodiment may also generally include other hardware, such as a forwarding chip responsible for processing messages, etc. Taking software implementation as an example, Figure 3 As shown, as a device in a logical sense, the CPU of the computing device in which it is located reads the corresponding computer program in the non-volatile memory into the memory and runs it. This embodiment provides a communication device based on WebSocket and BroadcastChannel, including:
[0088] The communication module 300 is used to establish a communication connection between the front-end client and the back-end server based on WebSocket;
[0089] The simulation communication module 302 is used to create a channel instance based on BroadcastChannel on the front-end client to simulate communication when the communication connection is not successfully established; the channel instance is used to simulate the sending of events between the front-end client and the back-end server and to implement event monitoring by binding a monitoring function.
[0090] In some specific implementations, the communication module 300 may be used to execute the above step 100 , and the analog communication module 302 may be used to execute the above step 102 .
[0091] In some specific implementations, the analog communication module 302 is further configured to perform the following operations:
[0092] Obtain data and a first name included in a first event sent by a front-end client to a back-end server;
[0093] Determining whether a first channel instance corresponding to the first name is stored in the application channel instance library;
[0094] If the judgment result is yes, then the first channel instance is called from the application channel instance library and the first listening function is bound;
[0095] If the judgment result is no, determining the simulation data script according to the first name, generating the first channel instance according to the simulation data script, and binding the first listening function; and storing the first channel instance and the first name in the application channel instance library in correspondence;
[0096] Create a temporary channel instance with the same name as the first channel instance on the front-end client;
[0097] When data is sent to the backend server through the temporary channel instance, the first monitoring function is triggered and the temporary channel instance is destroyed.
[0098] In some specific implementations, the analog communication module 302 is further configured to perform the following operations:
[0099] The front-end client creates a corresponding first channel instance based on the first name of the first event;
[0100] Triggering a first listening function bound to a first channel instance based on a first event, and obtaining simulation data by executing the first listening function; wherein the first listening function is used to simulate business processing of a backend server;
[0101] Generate a second event according to the simulation data; wherein the second event is sent from the back-end server to the front-end client;
[0102] Create a corresponding second channel instance based on the second name of the second event, and bind a second listening function;
[0103] When the simulated data is sent to the front-end client through the second channel instance, the second monitoring function is triggered.
[0104] In some specific embodiments, Figure 4 As shown, the device also includes a management module 304, and the management module 304 is used to perform the following operations:
[0105] The channel instance is stored in the application channel instance library, and the listening function bound to the channel instance is stored in the event library.
[0106] In some specific implementations, the management module 304 is further configured to perform the following operations:
[0107] Receive a cancellation instruction including the target name and target listening function;
[0108] According to the cancel instruction, the channel instance corresponding to the target name is found from the application channel instance library, and the target listening function bound to the channel instance is found from the event library for deletion.
[0109] In some specific implementations, the management module 304 is further configured to perform the following operations:
[0110] receiving a clear instruction including a target name;
[0111] According to the clear instruction, the channel instance corresponding to the target name is found from the application channel instance library, and the listening function bound to the channel instance is found from the event library for deletion.
[0112] In some specific implementations, the management module 304 is further configured to perform the following operations:
[0113] Receive a closing instruction;
[0114] According to the closing instruction, all channel instances in the application channel instance library are closed, and the application channel instance library and event library are cleared.
[0115] It is to be understood that the structure illustrated in the embodiment of the present invention does not constitute a specific limitation on a communication device based on WebSocket and BroadcastChannel. In other embodiments of the present invention, a communication device based on WebSocket and BroadcastChannel may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0116] The information interaction, execution process and other contents between the modules in the above-mentioned device are based on the same concept as the embodiment of the method of the present invention. For the specific contents, please refer to the description in the embodiment of the method of the present invention, and no further description is given here.
[0117] An embodiment of the present invention further provides a computing device, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, a communication method based on WebSocket and BroadcastChannel in any embodiment of the present invention is implemented.
[0118] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the processor executes a communication method based on WebSocket and BroadcastChannel in any embodiment of the present invention.
[0119] An embodiment of the present application also provides a computer program product, which includes a computer program. A processor of a computer device reads the computer program from a computer-readable storage medium, and the processor executes the computer program, so that the computer device executes a communication method based on WebSocket and BroadcastChannel as described in any of the above embodiments.
[0120] Specifically, a system or device equipped with a storage medium can be provided, on which software program code that implements the functions of any of the above-mentioned embodiments is stored, and a computer (or CPU or MPU) of the system or device can be enabled to read and execute the program code stored in the storage medium.
[0121] In this case, the program code itself read from the storage medium can realize the function of any one of the above-mentioned embodiments, and thus the program code and the storage medium storing the program code constitute a part of the present invention.
[0122] The storage medium embodiments for providing the program code include a floppy disk, a hard disk, a magneto-optical disk, an optical disk (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, DVD-RW, DVD+RW), a magnetic tape, a non-volatile memory card, and a ROM. Alternatively, the program code can be downloaded from a server computer via a communication network.
[0123] In addition, it should be clear that the functions of any of the above embodiments can be implemented not only by executing the program code read by the computer, but also by enabling an operating system operating on the computer to complete part or all of the actual operations based on instructions from the program code.
[0124] In addition, it can be understood that the program code read from the storage medium is written to a memory provided in an expansion board inserted into the computer or to a memory provided in an expansion module connected to the computer, and then based on the instructions of the program code, a CPU installed on the expansion board or expansion module is enabled to perform part or all of the actual operations, thereby realizing the functions of any of the above-mentioned embodiments.
[0125] It should be noted that, in this article, relational terms such as first and second 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 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 device. In the absence of further restrictions, the elements defined by the statement "comprise a ..." do not exclude the presence of other identical factors in the process, method, article or device including the elements.
[0126] A person of ordinary skill in the art can understand that all or part of the steps of implementing the above method embodiments can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above method embodiments; and the aforementioned storage medium includes: ROM, RAM, magnetic disk or optical disk, etc., various media that can store program codes.
[0127] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A communication method based on WebSocket and BroadcastChannel, characterized in that: include: Establish a communication connection between the front-end client and the back-end server based on WebSocket; When the communication connection is not successfully established, a channel instance is created on the front-end client based on BroadcastChannel to simulate communication; the channel instance is used to simulate the sending of events between the front-end client and the back-end server and to implement the monitoring of the events by binding a monitoring function; The creating a channel instance based on BroadcastChannel on the front-end client to simulate communication includes: Acquire data and a first name included in a first event sent by the front-end client to the back-end server; Creating a corresponding first channel instance based on the first name and binding a first listening function; Creating a temporary channel instance with the same name as the first channel instance on the front-end client; When the data is sent to the backend server through the temporary channel instance, the first monitoring function is triggered, and the temporary channel instance is destroyed; and / or, The front-end client simulates the simulated data generated by the back-end server after performing business processing when monitoring the first event; Generate a second event according to the simulation data; wherein the second event is sent by the back-end server to the front-end client; Creating a corresponding second channel instance based on the second name of the second event, and binding a second listening function; When the simulation data is sent to the front-end client through the second channel instance, the second monitoring function is triggered; The creating a corresponding first channel instance based on the first name includes: Determining whether a first channel instance corresponding to the first name is stored in an application channel instance library; If the judgment result is yes, calling the first channel instance from the application channel instance library; If the judgment result is no, a simulation data script is determined according to the first name, the first channel instance is generated according to the simulation data script, and the first channel instance and the first name are stored in the application channel instance library in correspondence.
2. The method according to claim 1, characterized in that: The simulation data is determined by the following method: The front-end client creates a corresponding first channel instance based on the first name of the first event; A first listening function bound to the first channel instance is triggered based on the first event, and simulation data is obtained by executing the first listening function; wherein the first listening function is used to simulate the business processing of the backend server.
3. The method according to any one of claims 1 to 2, characterized in that: Also includes: The channel instance is stored in an application channel instance library, and the listening function bound to the channel instance is stored in an event library.
4. The method according to claim 3, characterized in that Also includes: Receive a cancellation instruction including the target name and target listening function; According to the cancel instruction, the channel instance corresponding to the target name is found from the application channel instance library, and the target listening function bound to the channel instance is found from the event library for deletion.
5. The method according to claim 3, characterized in that: Also includes: Receive a closing instruction; According to the closing instruction, all channel instances in the application channel instance library are closed, and the application channel instance library and the event library are cleared.
6. A communication device based on WebSocket and BroadcastChannel, characterized in that: Used to perform any of the methods in claims 1 to 5, comprising: The communication module is used to establish a communication connection between the front-end client and the back-end server based on WebSocket; A simulation communication module, used for creating a channel instance based on BroadcastChannel on the front-end client to simulate communication when the communication connection is not successfully established; the channel instance is used to simulate the sending of events between the front-end client and the back-end server and to implement the monitoring of the events by binding a monitoring function; The analog communication module is also used to perform the following operations: Obtain data and a first name included in a first event sent by a front-end client to a back-end server; Determining whether a first channel instance corresponding to the first name is stored in the application channel instance library; If the judgment result is yes, then the first channel instance is called from the application channel instance library and the first listening function is bound; If the judgment result is no, determining the simulation data script according to the first name, generating the first channel instance according to the simulation data script, and binding the first listening function; and storing the first channel instance and the first name in the application channel instance library in correspondence; Create a temporary channel instance with the same name as the first channel instance on the front-end client; When sending data to the backend server through the temporary channel instance, the first listening function is triggered and the temporary channel instance is destroyed; The analog communication module is also used to perform the following operations: The front-end client creates a corresponding first channel instance based on the first name of the first event; Triggering a first listening function bound to a first channel instance based on a first event, and obtaining simulation data by executing the first listening function; wherein the first listening function is used to simulate business processing of a backend server; Generate a second event according to the simulation data; wherein the second event is sent from the back-end server to the front-end client; Create a corresponding second channel instance based on the second name of the second event, and bind a second listening function; When the simulated data is sent to the front-end client through the second channel instance, the second monitoring function is triggered.
7. A computing device, comprising a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the method according to any one of claims 1 to 5 is implemented.
8. A computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to execute the method according to any one of claims 1 to 5.
9. A computer program product, characterized in that The method comprises computer instructions, which, when executed by a processor, implement the steps of the method according to any one of claims 1 to 5.
