Method, program and medium for keeping Web side WebSocket heartbeat persistence
By using WebWorker technology to create child threads and implement a heartbeat mechanism on the web side, the problem of WebSocket connection being disconnected due to long-term no data transmission or browser resource management policies is solved, and the stability and reliability of the connection are achieved.
Patent Information
- Application Number
- CN202411474800.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-22
- Publication Date
- 2025-03-07
AI Technical Summary
In complex network environments, WebSocket connections are easily automatically disconnected due to long-term no data transmission or browser resource management policies, resulting in the impact of connection stability and availability.
By creating child threads based on WebWorker technology, implementing a heartbeat mechanism, sending heartbeat messages to the main thread regularly, and sending heartbeat packets to the server in the main thread to ensure the active status of the connection.
Effectively prevent WebSocket connection from being automatically disconnected due to long-term data transmission or browser mechanism, ensuring the stability and reliability of the connection, and avoiding memory leakage.
Smart Images

Figure CN119583624A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of Web real-time communication, and in particular to a method for maintaining WebSocket heartbeat persistence at a Web end. Background Art
[0002] WebSocket protocol, as an innovative network communication technology, implements efficient full-duplex communication mode on a single TCP connection. This feature makes it show incomparable advantages in processing real-time data push and fast two-way interaction. WebSocket is very useful in Web applications that require real-time data exchange, such as online games, real-time notification systems, etc.
[0003] When developing real-time communication applications based on WebSocket, it is crucial to ensure the stability and availability of the connection. However, in a complex network environment, the stability of the WebSocket connection has become a challenge. Common risk factors include connection timeouts caused by long periods of silence, unstable network fluctuations, browser behavior (such as hiding or switching tabs), potential server-side failures, etc., all of which may lead to WebSocket connection interruptions.
[0004] The conventional operation to ensure the stability of WebSocket connection is to apply the heartbeat mechanism, start the timer in the main thread to send heartbeat packets, maintain the active state of the connection, detect whether the other end is still online, and ensure that the WebSocket connection between the client and the server is not interrupted. However, this solution will be affected by the browser's resource management strategy (such as resource restrictions or power saving mode when the page tab is not active), resulting in the failure of the heartbeat packet timing task and the failure of the heartbeat packet to send, which affects the stability of the WebSocket connection and causes disconnection. Summary of the invention
[0005] In view of the above-mentioned deficiencies in the current technology, the present invention provides a method for maintaining the persistence of the WebSocket heartbeat on the Web side. By creating and implementing a heartbeat mechanism based on WebWorker technology, it can effectively prevent the WebSocket connection from being automatically disconnected due to long-term no data transmission and browser mechanism, thereby ensuring the stability and reliability of the connection.
[0006] To achieve the above object, the embodiments of the present invention adopt the following technical solutions:
[0007] A method for maintaining WebSocket heartbeat persistence on a Web client, comprising the following steps:
[0008] Create a websocket singleton, initialize and establish the main thread of the websocket connection;
[0009] Create a webworker thread, configure it to execute the heartbeat timing task and listen to the opening and closing of WebSocket;
[0010] After the websocket connection is established, the webworker thread monitors the opening of WebSocket and executes the heartbeat timing task to send heartbeat messages to the main thread regularly.
[0011] After receiving the heartbeat message, the main thread sends a heartbeat packet to the websocket server;
[0012] After the WebSocket connection is disconnected, the webworker thread listens to the closure of the WebSocket, closes the heartbeat timing task, and stops sending heartbeat messages to the main thread.
[0013] According to one aspect of the present invention, the method for maintaining WebSocket heartbeat persistence on the Web side is applied in a browser JavaScript script.
[0014] According to one aspect of the present invention, the method for maintaining WebSocket heartbeat persistence on the Web side includes: setting an event listener in the main thread to receive messages sent from the webworker thread.
[0015] According to one aspect of the present invention, the method for maintaining WebSocket heartbeat persistence on the Web side includes: setting an event listener in a webworker thread to receive messages sent from a main thread.
[0016] According to one aspect of the present invention, a heartbeat timer is set in a webworker thread to execute a heartbeat sending timing task.
[0017] According to one aspect of the present invention, the method for maintaining WebSocket heartbeat persistence on the Web side also includes: setting a logging function in the main thread and the webworker thread.
[0018] According to one aspect of the present invention, the method for maintaining WebSocket heartbeat persistence on the Web side also includes: diagnosing WebSocket connection problems or performance bottlenecks based on log records.
[0019] According to one aspect of the present invention, the method for maintaining Web-side WebSocket heartbeat persistence also includes: when the WebSocket connection is no longer needed, correctly terminating the webworker thread, cleaning up the WebSocket connection, and avoiding memory leaks.
[0020] A computer program, when executed, implements the steps of the method for maintaining WebSocket heartbeat persistence on a Web side as described above.
[0021] A readable storage medium stores the computer program as described above, and when the computer program is executed, the steps of maintaining the persistence of the WebSocket heartbeat on the Web side are implemented as described above.
[0022] Advantages of the present invention:
[0023] The present invention provides a method for maintaining WebSocket heartbeat persistence on a Web end. By creating a webworker subthread based on WebWorker technology to implement a heartbeat mechanism, the WebSocket connection can be effectively prevented from being automatically disconnected due to no data transmission for a long time or a browser mechanism, thereby ensuring the stability and reliability of the connection.
[0024] The present invention can also correctly terminate the webworker child thread and clean up the WebSocket connection when the WebSocket connection is no longer needed, thereby avoiding memory leaks; and by providing a logging function in the main thread and the webworker child thread, the connection problem or performance bottleneck of the WebSocket can be diagnosed based on the logging. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only 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.
[0026] Figure 1 This is a working diagram of a method for maintaining WebSocket heartbeat persistence on a Web side according to Embodiment 1 of the present invention. DETAILED DESCRIPTION
[0027] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0028] Embodiment 1
[0029] like Figure 1As shown, a method for maintaining WebSocket heartbeat persistence on a Web side includes the following steps:
[0030] Create a websocket singleton, initialize and establish the main thread of the websocket connection.
[0031] Create a webworker thread, configure it to execute the scheduled task of sending heartbeats and listen for the opening and closing of WebSocket.
[0032] After the websocket connection is established, the webworker thread monitors the opening of WebSocket and executes the heartbeat timing task to send heartbeat messages to the main thread regularly.
[0033] After receiving the heartbeat message, the main thread sends a heartbeat packet to the websocket server;
[0034] After the WebSocket connection is disconnected, the webworker thread listens to the closure of the WebSocket, closes the heartbeat timing task, and stops sending heartbeat messages to the main thread.
[0035] The method for maintaining the persistence of WebSocket heartbeat on the Web side is applied in the browser JavaScript script.
[0036] WebWorker technology is a powerful feature introduced by HTML5, which allows developers to create background threads in web applications to perform computationally intensive or time-consuming tasks, thereby avoiding blocking the main thread and improving page performance and responsiveness.
[0037] WebWorker runs in an independent thread and executes in parallel with the main thread to improve page responsiveness. WebWorker cannot directly access objects of the main thread such as DOM, window, document, etc., ensuring that it will not affect the state and structure of the page.
[0038] WebWorker communicates with the main thread through message passing, using the postMessage method to send and receive messages to achieve data exchange.
[0039] The main thread is used to establish and maintain the WebSocket connection between the client and the server, listen to webworker thread messages, and notify the webworker thread.
[0040] Set up an event listener in the main thread to receive messages sent from the webworker thread.
[0041] Set up an event listener in the webworker thread to receive messages sent from the main thread.
[0042] Set a heartbeat timer in the webworker thread to execute the heartbeat timing task. After the webworker thread listens to the message that the WebSocket connection is successful, it starts the heartbeat timer and sends heartbeat messages to the main thread regularly according to the timer. After the webworker thread listens to the message that the WebSocket connection is disconnected, it turns off the heartbeat timer and stops sending heartbeat messages.
[0043] When the webworker thread turns off the heartbeat timer, clear the timer.
[0044] The beneficial effect of this embodiment is that the method implements the heartbeat mechanism by creating a webworker sub-thread based on the WebWorker technology, which can effectively prevent the WebSocket connection from being automatically disconnected due to long-term lack of data transmission and browser mechanism, thereby ensuring the stability and reliability of the connection.
[0045] Embodiment 2
[0046] like Figure 1 As shown, a method for maintaining WebSocket heartbeat persistence on a Web side includes the following steps:
[0047] Create a websocket singleton, initialize and establish the main thread of the websocket connection.
[0048] Create a webworker thread, configure it to execute the scheduled task of sending heartbeats and listen for the opening and closing of WebSocket.
[0049] After the websocket connection is established, the webworker thread monitors the opening of WebSocket and executes the heartbeat timing task to send heartbeat messages to the main thread regularly.
[0050] After receiving the heartbeat message, the main thread sends a heartbeat packet to the websocket server;
[0051] After the WebSocket connection is disconnected, the webworker thread listens to the closure of the WebSocket, closes the heartbeat timing task, and stops sending heartbeat messages to the main thread.
[0052] The method for maintaining the persistence of WebSocket heartbeat on the Web side is applied in the browser JavaScript script.
[0053] WebWorker technology is a powerful feature introduced by HTML5, which allows developers to create background threads in web applications to perform computationally intensive or time-consuming tasks, thereby avoiding blocking the main thread and improving page performance and responsiveness.
[0054] WebWorker runs in an independent thread and executes in parallel with the main thread to improve page responsiveness. WebWorker cannot directly access objects of the main thread such as DOM, window, document, etc., ensuring that it will not affect the state and structure of the page.
[0055] WebWorker communicates with the main thread through message passing, using the postMessage method to send and receive messages to achieve data exchange.
[0056] The main thread is used to establish and maintain the WebSocket connection between the client and the server, listen to webworker thread messages, and notify the webworker thread.
[0057] Set up an event listener in the main thread to receive messages sent from the webworker thread.
[0058] Set up an event listener in the webworker thread to receive messages sent from the main thread.
[0059] Set a heartbeat timer in the webworker thread to execute the heartbeat timing task. After the webworker thread listens to the message that the WebSocket connection is successful, it starts the heartbeat timer and sends heartbeat messages to the main thread regularly according to the timer. After the webworker thread listens to the message that the WebSocket connection is disconnected, it turns off the heartbeat timer and stops sending heartbeat messages.
[0060] When the webworker thread turns off the heartbeat timer, clear the timer.
[0061] When the WebSocket connection is no longer needed, terminate the webworker thread correctly and clean up the WebSocket connection to avoid memory leaks.
[0062] The beneficial effect of this embodiment is that when the WebSocket connection is no longer needed, this method correctly terminates the webworker child thread and cleans up the WebSocket connection, thereby avoiding memory leaks.
[0063] Embodiment 3
[0064] like Figure 1As shown, a method for maintaining WebSocket heartbeat persistence on a Web side includes the following steps:
[0065] Create a websocket singleton, initialize and establish the main thread of the websocket connection.
[0066] Create a webworker thread, configure it to execute the scheduled task of sending heartbeats and listen for the opening and closing of WebSocket.
[0067] After the websocket connection is established, the webworker thread monitors the opening of WebSocket and executes the heartbeat timing task to send heartbeat messages to the main thread regularly.
[0068] After receiving the heartbeat message, the main thread sends a heartbeat packet to the websocket server;
[0069] After the WebSocket connection is disconnected, the webworker thread listens to the closure of the WebSocket, closes the heartbeat timing task, and stops sending heartbeat messages to the main thread.
[0070] The method for maintaining the persistence of WebSocket heartbeat on the Web side is applied in the browser JavaScript script.
[0071] WebWorker technology is a powerful feature introduced by HTML5, which allows developers to create background threads in web applications to perform computationally intensive or time-consuming tasks, thereby avoiding blocking the main thread and improving page performance and responsiveness.
[0072] WebWorker runs in an independent thread and executes in parallel with the main thread to improve page responsiveness. WebWorker cannot directly access objects of the main thread such as DOM, window, document, etc., ensuring that it will not affect the state and structure of the page.
[0073] WebWorker communicates with the main thread through message passing, using the postMessage method to send and receive messages to achieve data exchange.
[0074] The main thread is used to establish and maintain the WebSocket connection between the client and the server, listen to webworker thread messages, and notify the webworker thread.
[0075] Set up an event listener in the main thread to receive messages sent from the webworker thread.
[0076] Set up an event listener in the webworker thread to receive messages sent from the main thread.
[0077] Set a heartbeat timer in the webworker thread to execute the heartbeat timing task. After the webworker thread listens to the message that the WebSocket connection is successful, it starts the heartbeat timer and sends heartbeat messages to the main thread regularly according to the timer. After the webworker thread listens to the message that the WebSocket connection is disconnected, it turns off the heartbeat timer and stops sending heartbeat messages.
[0078] When the webworker thread turns off the heartbeat timer, clear the timer.
[0079] When the WebSocket connection is no longer needed, terminate the webworker thread correctly and clean up the WebSocket connection to avoid memory leaks.
[0080] Logging functions are also set up in the main thread and webworker thread. The logged information may include WebSocket connections, message transmissions, errors and exceptions, performance data, etc.
[0081] You can use the log records to diagnose WebSocket connection problems or performance bottlenecks.
[0082] The beneficial effect of this embodiment is that: by setting the logging function in the main thread and the webworker sub-thread, the method can diagnose the connection problem or performance bottleneck of the WebSocket according to the log records.
[0083] Embodiment 4
[0084] A computer program, when executed, implements the steps of the method for maintaining WebSocket heartbeat persistence on a Web side as described in Embodiment 1, 2 or 3.
[0085] Embodiment 5
[0086] A readable storage medium stores a computer program as described in Example 4, and when the computer program is executed, the steps of the method for maintaining WebSocket heartbeat persistence on a Web side as described in Example 1, 2 or 3 are implemented.
[0087] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by any technician familiar with the art within the technical scope disclosed in the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.
Claims
1. A method for maintaining WebSocket heartbeat persistence on a Web client, characterized in that: The following steps are involved: Create a websocket singleton, initialize and establish the main thread of the websocket connection; Create a webworker thread, configure it to execute the heartbeat timing task and listen to the opening and closing of WebSocket; After the websocket connection is established, the webworker thread monitors the opening of WebSocket and executes the heartbeat timing task to send heartbeat messages to the main thread regularly. After receiving the heartbeat message, the main thread sends a heartbeat packet to the websocket server; After the WebSocket connection is disconnected, the webworker thread listens to the closure of the WebSocket, closes the heartbeat timing task, and stops sending heartbeat messages to the main thread.
2. The method for maintaining WebSocket heartbeat persistence on the Web side according to claim 1, characterized in that: The method for maintaining the persistence of WebSocket heartbeat on the Web side is applied in the browser JavaScript script.
3. The method for maintaining WebSocket heartbeat persistence on the Web side according to claim 1, characterized in that: The method for maintaining WebSocket heartbeat persistence on the Web side includes: setting an event listener in the main thread to receive messages sent from the webworker thread.
4. The method for maintaining WebSocket heartbeat persistence on the Web side according to claim 1, characterized in that: The method for maintaining WebSocket heartbeat persistence on the Web side includes: setting an event listener in a webworker thread to receive messages sent from a main thread.
5. The method for maintaining WebSocket heartbeat persistence on the Web side according to claim 1, characterized in that: Set a heartbeat timer in the webworker thread to execute the task of sending heartbeat timing.
6. The method for maintaining WebSocket heartbeat persistence on a Web terminal according to claim 1, characterized in that: The method for maintaining WebSocket heartbeat persistence at the Web end also includes: setting a logging function in the main thread and the webworker thread.
7. The method for maintaining WebSocket heartbeat persistence on the Web side according to claim 6, characterized in that: The method for maintaining WebSocket heartbeat persistence at the Web end also includes: diagnosing WebSocket connection problems or performance bottlenecks based on log records.
8. The method for maintaining WebSocket heartbeat persistence on a Web terminal according to claim 1, characterized in that: The method for maintaining WebSocket heartbeat persistence on the Web side also includes: when the WebSocket connection is no longer needed, correctly terminating the webworker thread, cleaning up the WebSocket connection, and avoiding memory leaks.
9. A computer program, characterized in that When the computer program is executed, the steps of the method for maintaining WebSocket heartbeat persistence on the Web side are implemented as described in any one of claims 1 to 8.
10. A readable storage medium, characterized in that: The computer program according to claim 9 is stored on the readable storage medium, and when the computer program is executed, the step of maintaining the persistence of the WebSocket heartbeat on the Web side as described in any one of claims 1 to 8 is implemented.