Broken line reconnection method, device and equipment based on Java WebSocket and medium
By listening to WebSocket endpoint events and executing corresponding thread logic according to event type, the automatic disconnection and reconnection function of Java WebSocket client is implemented, solving the problem of missing disconnection and reconnection mechanism in the existing technology, improving the stability of the system and Communication efficiency.
Patent Information
- Application Number
- CN202411712169.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-02-18
AI Technical Summary
The existing Java WebSocket client does not provide a disconnection mechanism, which causes the client to be unable to reconnect automatically after the server is disconnected. No communication for a long time leads to a connection interruption, and abnormal events will also interrupt the connection, affecting the normal completion of business logic.
By listening to whether the WebSocket endpoint generates a target event, obtain the event type, and execute the corresponding thread logic according to the event type: For the open event type, start the timed scheduled thread to send a heartbeat request to maintain the connection; for the wrong event or the closed event type, start the disconnection Reconnect the thread to re-establish the WebSocket connection.
It realizes that the WebSocket client can automatically initiate renewal and reconnect operations when encountering disconnection or abnormal events, avoids increasing pressure between the client and the server, and effectively solves the connection interruption caused by service network fluctuations and server abnormalities. Improves system stability and communication efficiency.
Smart Images

Figure CN119484614A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a Java WebSocket-based disconnection reconnection method, device, equipment and medium. Background Art
[0002] In the existing Java technology system, when the Java WebSocket client and the server communicate and interact with each other through the WebSocket protocol, the Java-related WebSocket client does not provide a disconnection reconnection mechanism. Therefore, the following problems may occur in actual applications:
[0003] 1. Unable to automatically reconnect after the server is disconnected: After the client senses the server disconnection, the client will disconnect from the server. After the server comes back online, it cannot automatically reconnect to the server.
[0004] 2. Connection interruption due to long-term lack of communication: When there is no content communication between the client and the server for a long time, the client is actively disconnected and cannot continue to maintain the connection with the server.
[0005] 3. Abnormal events lead to connection interruption: When an abnormal event occurs on the client, the connection with the server will be interrupted, causing the business logic to be unable to complete the interaction with the server normally.
[0006] In summary, how to implement in a Java environment that when a WebSocket client encounters a disconnection or abnormal event during communication, the client automatically initiates renewal and reconnection operations through effective algorithms and variables while avoiding increasing pressure on the client and server is a technical problem that needs to be solved urgently in this field. Summary of the invention
[0007] In view of this, the purpose of the present invention is to provide a Java WebSocket-based disconnection reconnection method, device, equipment and medium, which can realize that when an abnormal event such as disconnection occurs during the WebSocket protocol communication interaction between the client and the server, the client can automatically initiate renewal and reconnection operations, while avoiding increasing the pressure on the client and the server. The specific scheme is as follows:
[0008] In the first aspect, the present application discloses a Java WebSocket-based disconnection reconnection method, which is applied to a WebSocket client and includes:
[0009] Monitor whether the WebSocket endpoint generates a target event, and if so, obtain the event type of the target event;
[0010] If the event type is the first event type indicating that the target event is an open event, executing a timer scheduling thread initiated to the WebSocket server, and receiving a heartbeat reply fed back by the WebSocket server, so as to realize the connection between the WebSocket client and the WebSocket server;
[0011] If the event type is a second event type indicating that the target event is an error event and / or a closing event, a disconnection and reconnection thread initiated to the WebSocket server is executed to reconnect the WebSocket to the WebSocket server.
[0012] Optionally, the monitoring of whether the WebSocket endpoint generates a target event and obtaining the event type of the target event if the target event is generated further includes:
[0013] The WebSocket client and the corresponding WebSocket server are initialized and connected based on a preset initialization method, a connection address and connection parameter information.
[0014] Optionally, before initializing the connection between the WebSocket client and the corresponding WebSocket server based on the preset initialization method, connection address and connection parameter information, the method further includes:
[0015] Retrieve the Java package of classes and interfaces used to perform WebSocket communication connections;
[0016] The connection address and connection parameter information of the WebSocket client and the WebSocket server are set, and the initialization method of the WebSocket client to connect to the WebSocket server based on the Java package is defined.
[0017] Optionally, if the event type is a first event type indicating that the target event is an open event, executing a timed scheduling thread initiated to the WebSocket server, and receiving a heartbeat reply fed back by the WebSocket server to achieve a connection between the WebSocket client and the WebSocket server, including:
[0018] If the event type is the first event type that characterizes that the target event is an open event, a heartbeat request is periodically sent to the WebSocket server through the configured scheduling frequency and time unit, and a heartbeat reply fed back by the WebSocket server is received to realize the connection between the WebSocket client and the WebSocket server.
[0019] Optionally, the step of regularly sending a heartbeat request to a WebSocket server according to a scheduling frequency and a time unit configured for the scheduled scheduling, and receiving a heartbeat reply fed back by the WebSocket server to achieve a connection between the WebSocket client and the WebSocket server includes:
[0020] Send a zero-byte message body to the WebSocket server periodically through the configured scheduling frequency and time unit, receive the message body fed back by the WebSocket server, and then update the first type volatile variable value corresponding to the first event type, so as to determine whether to reconnect based on the first type volatile variable value, so as to realize the connection between the WebSocket client and the WebSocket server.
[0021] Optionally, if the event type is a second event type characterizing that the target event is an error event and / or a close event, executing a disconnection and reconnection thread initiated to the WebSocket server to reconnect the WebSocket to the WebSocket server includes:
[0022] If the event type is a second event type that characterizes that the target event is an error event and / or a shutdown event, a second type volatile variable value corresponding to the second event type is read, and whether to reconnect is determined based on the second type volatile variable value. If reconnected, the connection frequency of the WebSocket client is controlled based on a preset waiting time to achieve reconnection with the WebSocket server.
[0023] Optionally, before controlling the connection frequency of the WebSocket client based on a preset waiting time to achieve reconnection with the WebSocket server, the method further includes:
[0024] pass Determine the waiting time; wherein t represents the waiting time, n represents the exponential power, and random represents a random number in the waiting factor interval.
[0025] In the second aspect, the present application discloses a Java WebSocket-based disconnection reconnection device, which is applied to a WebSocket client, comprising:
[0026] An event monitoring module is used to monitor whether a target event is generated by a WebSocket endpoint, and if the target event is generated, obtain the event type of the target event;
[0027] A first connection module, configured to execute a timed scheduling thread initiated to a WebSocket server if the event type is a first event type indicating that the target event is an open event, and receive a heartbeat reply fed back by the WebSocket server to achieve connection between the WebSocket client and the WebSocket server;
[0028] The second connection module is used to execute a disconnection and reconnection thread initiated to the WebSocket server if the event type is a second event type that indicates that the target event is an error event and / or a closing event, so as to reconnect the WebSocket to the WebSocket server.
[0029] In a third aspect, the present application discloses an electronic device, comprising:
[0030] Memory, used to store computer programs;
[0031] The processor is used to execute the computer program to implement the steps of the aforementioned Java WebSocket-based disconnection reconnection method disclosed above.
[0032] In a fourth aspect, the present application discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the steps of the aforementioned Java WebSocket-based disconnection reconnection method are implemented.
[0033] It can be seen that the present application discloses a Java WebSocket disconnection and reconnection method, which is applied to the WebSocket client, including: monitoring whether the WebSocket endpoint generates a target event, and obtaining the event type of the target event if the target event is generated; if the event type is a first event type that characterizes the target event as an open event, executing a timed scheduling thread initiated to the WebSocket server, and receiving a heartbeat reply fed back by the WebSocket server to achieve the connection between the WebSocket client and the WebSocket server; if the event type is a second event type that characterizes the target event as an error event and / or a closed event, executing a disconnection and reconnection thread initiated to the WebSocket server to achieve reconnection with the WebSocket server. It can be seen that by monitoring the WebSocket endpoint by the WebSocket client to determine whether a target event is generated, when it is determined that a target event is generated, executing the thread logic corresponding to the WebSocket server according to the event type of the target event, the automatic connection management between the client and the server can be effectively realized. This method is implemented in pure Java, does not need to rely on a third-party library, and can be published as a standard SDK, which is convenient for Java programs to be quickly integrated and used, thereby improving the reliability and development efficiency of the system. In this way, it can effectively solve the problem of WebSocket connection interruption caused by service network fluctuations, server abnormalities or other uncontrollable factors, and achieve rapid reconnection and communication status recovery. This method has the advantages of simple implementation, fast response and strong adaptability, and can significantly improve the stability and communication efficiency of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] 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 only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0035] Figure 1 This is a flow chart of a Java WebSocket disconnection reconnection method disclosed in this application;
[0036] Figure 2 A specific flow chart of a Java WebSocket-based disconnection and reconnection method disclosed in this application;
[0037] Figure 3 This is a schematic diagram of the structure of a Java WebSocket-based disconnection reconnection device disclosed in this application;
[0038] Figure 4 This is a structural diagram of an electronic device disclosed in this application. DETAILED DESCRIPTION
[0039] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. 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 making creative work are within the scope of protection of the present invention.
[0040] In the existing Java technology system, when the Java WebSocket client and the server communicate and interact with each other using the WebSocket protocol, the Java-related WebSocket client does not provide a disconnection reconnection mechanism, so the following problems will occur: After the server disconnects and is sensed by the client, the client will disconnect from the server, and will not be able to automatically reconnect to the server after the server comes back online. When there is no content communication between the client and the server for a long time, the client is actively disconnected and cannot continue to maintain the connection with the server. When an abnormal event occurs on the client, the connection with the server will be interrupted, causing the business logic to be unable to effectively handle the interaction with the server.
[0041] To this end, the present invention provides a Java WebSocket-based disconnection and reconnection solution, which can enable the client to automatically initiate renewal and reconnection operations when abnormal events such as disconnection occur during the WebSocket protocol communication interaction between the client and the server, while avoiding increasing the pressure on the client and the server.
[0042] Reference Figure 1 As shown, an embodiment of the present invention discloses a Java WebSocket-based disconnection reconnection method, which is applied to a WebSocket client and includes:
[0043] Step S11: monitor whether the WebSocket endpoint generates a target event, and if the target event is generated, obtain the event type of the target event.
[0044] In this embodiment, after the WebSocket client and the WebSocket server establish an initial communication connection, the WebSocket client monitors whether the WebSocket endpoint generates a target event. When the target event is not monitored, it indicates that the two are communicating normally, and no target event corresponding to the reconnection operation has occurred, and the WebSocket endpoint continues to be monitored; when the target event is monitored, it is necessary to determine the event type of the monitored target event to obtain the event type of the target event. It should be noted that WebSocket is a protocol for full-duplex communication over a single TCP connection. WebSocket enables real-time, low-latency, two-way communication between the client and the server. The WebSocket endpoint refers to the address that listens for WebSocket connections on the server.
[0045] In this embodiment, the monitoring of whether the WebSocket endpoint generates a target event, and before obtaining the event type of the target event if the target event is generated, it also includes: initializing the connection between the WebSocket client and the corresponding WebSocket server based on the preset initialization method, connection address and connection parameter information. It can be understood that to use WebSocket, the client first establishes an initial connection with the server through an HTTP request, and the URL of this HTTP request is the WebSocket endpoint. After the server receives this request, if it supports the WebSocket protocol, it will return a special response, which contains the relevant header information of the WebSocket protocol. Then the client and the server can communicate bidirectionally through this TCP connection without going through the HTTP request / response cycle. The format of the WebSocket endpoint is generally ws: / / or wss: / / prefix plus the server's domain name and port, where ws indicates the use of plain text transmission, wss indicates the use of TLS encrypted transmission, and the above is the connection parameter information. For example, if the server's domain name is example.com and the listening port is 8080, then the WebSocket endpoint can be ws: / / example.com:8080 or wss: / / example.com:8080. In actual applications, the WebSocket endpoint is usually provided by the server, and the client communicates with the server in real time by connecting to this endpoint. Before using WebSocket, the client needs to know the server's WebSocket endpoint address and specify the address as the connection address when establishing a connection.
[0046] In this embodiment, before the WebSocket client and the corresponding WebSocket server are initialized and connected based on the preset initialization method, connection address and connection parameter information, it also includes: calling the Java package of the class and interface for executing the WebSocket communication connection; setting the connection address and connection parameter information for the WebSocket client to connect to the WebSocket server, and defining the initialization method for the WebSocket client to connect to the WebSocket server based on the Java package. It can be understood that the initialization method for connecting to the websocket server through javax.websocket is defined, and the connection address and related connection parameter configuration information are preset. In this way, the WebSocket server can be connected through the javax.websocket standard api.
[0047] Step S12: If the event type is the first event type indicating that the target event is an open event, a timer scheduling thread initiated to the WebSocket server is executed, and a heartbeat reply fed back by the WebSocket server is received to realize the connection between the WebSocket client and the WebSocket server.
[0048] In this embodiment, if the event type is the first event type that characterizes that the target event is an open event, a heartbeat request is sent to the WebSocket server at a fixed time through the configured scheduled scheduling frequency and time unit, and a heartbeat reply fed back by the WebSocket server is received to realize the connection between the WebSocket client and the WebSocket server. It can be understood that when the target event is an open event, the scheduled scheduling thread initiated to the WebSocket server is executed, wherein the scheduled scheduling thread can be implemented by java.util.concurrent.ScheduledThreadPoolExecutor, and its core thread number is 1. Executing the scheduled scheduling thread is to configure the frequency and time unit of the scheduled scheduling, and send the heartbeat request of the WebSocket client to the WebSocket server at a fixed frequency. Then, the pong message sent by the Websocket client scheduled scheduling thread is processed on the Websocket server, that is, after the Websocket server obtains the pong message, it only needs to reply this message to the client intact without any other additional processing. In this way, the Websocket client can not only receive the heartbeat reply from the Websocket server, but also reduce the business concerns of the Websocket server, so that the Websocket server only replies to the WebSocket client's pong message as is, making the WebSocket server processing more convenient and efficient.
[0049] Among them, executing the timed sending of the heartbeat request to the WebSocket server through the configured scheduling frequency and time unit of the timed scheduling includes: sending a zero-byte message body to the WebSocket server through the configured scheduling frequency and time unit of the timed scheduling, and receiving the message body fed back by the WebSocket server, and then updating the first type volatile variable value corresponding to the first event type to determine whether to reconnect based on the first type volatile variable value to achieve the connection between the WebSocket client and the WebSocket server. It can be understood that within the timed scheduling thread, a pong message with a byte of 0 is sent to the server by using the javax.websocket.Session obtained by opening the event to keep the communication between the WebSocket client and the WebSocket server from being interrupted. The timed scheduling thread will send a message body with a fixed size of 0 frames to the server at a frequency of the set time unit. At the same time, the variable value in the pre-defined volatile type variable is updated to ensure thread visibility, to prevent multiple threads from operating reconnection at the same time and to stop executing the reconnection flag after the reconnection is successful. Specifically, in the WebSocket client open event, the javax.websocket.Session object established by the connection is used to register the WebSocket's javax.websocket.PongMessage message mechanism, and the previously defined volatile type variable value is updated in the registration mechanism to indicate that normal communication with the WebSocket server is maintained. The reconnection thread determines whether to continue reconnecting based on this variable.
[0050] Step S13: If the event type is the second event type indicating that the target event is an error event and / or a closing event, a disconnection and reconnection thread initiated to the WebSocket server is executed to reconnect the WebSocket to the WebSocket server.
[0051] In this embodiment, if the event type is a second event type that characterizes that the target event is an error event and / or a closed event, the second type volatile variable value corresponding to the second event type is read, and whether to reconnect is determined based on the second type volatile variable value. If reconnected, the connection frequency of the WebSocket client is controlled based on the preset waiting time to achieve reconnection with the WebSocket server. It can be understood that when the target event is an error event or a closed event, the WebSocket disconnection and reconnection method is executed, and the WebSocket connection is reconnected according to the reconnection variable state represented by the variable value in volatile. And in the reconnection process, it is necessary to control the connection frequency of the WebSocket client based on the preset waiting time to achieve reconnection with the WebSocket server. The core thread and the maximum number of threads of this thread are fixed to 1, which can not affect the use of the main business process when the Websocket is disconnected, and thus will not occupy too many system resources and affect the main business process. The waiting time is a gradual waiting time, which can limit the connection frequency of the WebSocket client through the gradual waiting time to prevent multiple WebSocket clients from being connected to the same WebSocket server at the same time after disconnection and reconnection. It should be noted that a volatile (Java keyword) type variable is pre-defined to ensure thread visibility, to prevent multiple threads from reconnecting at the same time, and to stop the reconnection after the reconnection is successful.
[0052] In this embodiment, before the WebSocket client is controlled based on the preset waiting time to reconnect with the WebSocket server, the method further includes: Determine the waiting time; where t represents the waiting time, n represents the exponential power, and random represents the random number of the waiting factor interval. It can be understood that the reconnection waiting time is defined as a random number equal to the exponential power of 2 multiplied by the waiting factor interval (the waiting factor interval can be flexibly configured through environment variables). In this way, through the exponential backoff algorithm It can effectively avoid the problem of frequent connections after disconnection and reconnecting when the server is not ready in a short period of time, and reduce the performance loss caused by frequent connection errors on the client.
[0053] Reference Figure 2As shown, the present invention discloses a specific Websocket disconnection reconnection method flow chart, and the specific steps are as follows: initialize the WebSocket connection between the client and the server to determine the WebSocket client and the WebSocket server, and then the WebSocket client monitors the target event generated by the WebSocket endpoint in the connection between the two. When the target event type is the first event type, that is, the target event is an open event, when the open event is a registered business logic processing event, the reconnection process is directly terminated; when it is necessary to execute the opening timing scheduling thread based on the open event, send a pong heartbeat message to the WebSocket server, and before the opening timing scheduling thread, register the pong message processing mechanism to update the update of the volatile variable value, and at the same time control the pong reply content of the WebSocket server after receiving the pong heartbeat message, and update the volatile variable value. The pong reply content is the original reply to the pong heartbeat message sent by the client. In this way, the WebSocket server is convenient and fast to process. When the target event type is the second event type, that is, the target event is an abnormal event or a closed event, first register the reconnection method, that is, the defined initialization method for connecting the WebSocket server, and the initialization connection method can be executed by asynchronous call. Then read the volatile variable value, and determine whether reconnection is needed based on the variable value. If not, end directly; if yes, send a connection request to the WebSocket server regularly according to the calculated waiting time, and perform the reconnection operation. When the reconnection operation fails, jump to the step of sending a connection request to the WebSocket server regularly according to the calculated waiting time. If the reconnection is successful, update the variable value in volatile. In the case of disconnection and reconnection variable thread sharing, it can be guaranteed that the reconnection state will not be disordered even in high concurrency. The WebSocket client automatically renews and reconnects, and the main program only needs to focus on the business logic implementation.
[0054] It can be seen that the present application discloses a Java WebSocket disconnection and reconnection method, which is applied to the WebSocket client, including: monitoring whether the WebSocket endpoint generates a target event, and obtaining the event type of the target event if the target event is generated; if the event type is a first event type that characterizes the target event as an open event, executing a timed scheduling thread initiated to the WebSocket server, and receiving a heartbeat reply fed back by the WebSocket server to achieve the connection between the WebSocket client and the WebSocket server; if the event type is a second event type that characterizes the target event as an error event and / or a closed event, executing a disconnection and reconnection thread initiated to the WebSocket server to achieve reconnection with the WebSocket server. It can be seen that by monitoring the WebSocket endpoint by the WebSocket client to determine whether a target event is generated, when it is determined that a target event is generated, executing the thread logic corresponding to the WebSocket server according to the event type of the target event, the automatic connection management between the client and the server can be effectively realized. This method is implemented in pure Java, does not need to rely on a third-party library, and can be published as a standard SDK, which is convenient for Java programs to be quickly integrated and used, thereby improving the reliability and development efficiency of the system. In this way, it can effectively solve the problem of WebSocket connection interruption caused by service network fluctuations, server abnormalities or other uncontrollable factors, and achieve rapid reconnection and communication status recovery. This method has the advantages of simple implementation, fast response and strong adaptability, and can significantly improve the stability and communication efficiency of the system.
[0055] Reference Figure 3 As shown, the present invention also discloses a Java WebSocket-based disconnection reconnection device, which is applied to a WebSocket client and includes:
[0056] An event monitoring module 11 is used to monitor whether a target event is generated by a WebSocket endpoint, and if the target event is generated, obtain the event type of the target event;
[0057] The first connection module 12 is used to execute a timer scheduling thread initiated to the WebSocket server if the event type is a first event type indicating that the target event is an open event, and receive a heartbeat reply fed back by the WebSocket server to achieve connection between the WebSocket client and the WebSocket server;
[0058] The second connection module 13 is used to execute a disconnection and reconnection thread initiated to the WebSocket server if the event type is a second event type indicating that the target event is an error event and / or a closing event, so as to reconnect the WebSocket to the WebSocket server.
[0059] It can be seen that the present application discloses monitoring whether a target event is generated by a WebSocket endpoint, and if the target event is generated, the event type of the target event is obtained; if the event type is a first event type that characterizes the target event as an open event, a timed scheduling thread initiated to the WebSocket server is executed, and a heartbeat reply fed back by the WebSocket server is received to realize the connection between the WebSocket client and the WebSocket server; if the event type is a second event type that characterizes the target event as an error event and / or a closed event, a disconnection and reconnection thread initiated to the WebSocket server is executed to realize the reconnection of the WebSocket with the WebSocket server. It can be seen that by monitoring the WebSocket endpoint by the WebSocket client, it is determined whether a target event is generated. When it is determined that a target event is generated, the thread logic corresponding to the WebSocket server is executed according to the event type of the target event, which can effectively realize the automatic connection management between the client and the server. This method is implemented in pure Java, without relying on a third-party library, and can be published as a standard SDK, which is convenient for Java programs to be quickly integrated and used, thereby improving the reliability and development efficiency of the system. In this way, it can effectively solve the problem of WebSocket connection interruption caused by service network fluctuations, server abnormalities or other uncontrollable factors, and achieve rapid reconnection and communication status recovery. This method has the advantages of simple implementation, fast response and strong adaptability, and can significantly improve the stability and communication efficiency of the system.
[0060] Furthermore, the present application also discloses an electronic device. Figure 4 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content in the diagram cannot be considered as any limitation on the scope of use of the present application.
[0061] Figure 4A schematic diagram of the structure of an electronic device 20 provided in an embodiment of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the Java WebSocket disconnection reconnection method disclosed in any of the aforementioned embodiments. In addition, the electronic device 20 in this embodiment may specifically be an electronic computer.
[0062] In this embodiment, the power supply 23 is used to provide working voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and the external device, and the communication protocol it follows is any communication protocol that can be applied to the technical solution of the present application, and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs and is not specifically limited here.
[0063] Among them, the processor 21 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 21 can be implemented in at least one hardware form of DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). The processor 21 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a CPU (Central Processing Unit); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 21 may be integrated with a GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 21 may also include an AI (Artificial Intelligence) processor, which is used to process computing operations related to machine learning.
[0064] In addition, the memory 22, as a carrier for storing resources, can be a read-only memory, a random access memory, a disk or an optical disk, etc. The resources stored thereon can include an operating system 221, a computer program 222, etc., and the storage method can be temporary storage or permanent storage.
[0065] Among them, the operating system 221 is used to manage and control the hardware devices and computer programs 222 on the electronic device 20, so as to realize the operation and processing of the massive data 223 in the memory 22 by the processor 21, which can be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program that can be used to complete the Java WebSocket disconnection reconnection method executed by the electronic device 20 disclosed in any of the aforementioned embodiments, the computer program 222 can further include a computer program that can be used to complete other specific tasks. In addition to data transmitted from an external device received by the electronic device, the data 223 can also include data collected by its own input and output interface 25.
[0066] Furthermore, the present application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the aforementioned Java WebSocket disconnection reconnection method disclosed above is implemented. The specific steps of the method can refer to the corresponding contents disclosed in the aforementioned embodiments, and will not be repeated here.
[0067] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0068] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented with electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application. The steps of the method or algorithm described in conjunction with the embodiments disclosed herein can be implemented directly with hardware, a software module executed by a processor, or a combination of the two. The software module can be placed in a random access memory RAM (Random Access Memory), memory, read-only memory ROM (Read Only Memory), electrically programmable EPROM (Electrically Programmable Read Only Memory), electrically erasable programmable EEPROM (ElectricErasable Programmable Read Only Memory), register, hard disk, removable disk, CD-ROM (CompactDisc-Read Only Memory), or any other form of storage medium known in the technical field.
[0069] 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 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 sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0070] The Java WebSocket disconnection reconnection method, device, equipment and medium provided by the present invention are introduced in detail above. 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 those skilled in the art, 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 limiting the present invention.
Claims
1. A Java WebSocket disconnection reconnection method, characterized in that: Applicable to WebSocket clients, including: Monitor whether the WebSocket endpoint generates a target event, and if so, obtain the event type of the target event; If the event type is the first event type indicating that the target event is an open event, executing a timer scheduling thread initiated to the WebSocket server, and receiving a heartbeat reply fed back by the WebSocket server, so as to realize the connection between the WebSocket client and the WebSocket server; If the event type is a second event type indicating that the target event is an error event and / or a closing event, a disconnection and reconnection thread initiated to the WebSocket server is executed to reconnect the WebSocket to the WebSocket server.
2. The Java WebSocket disconnection reconnection method according to claim 1, characterized in that: Before monitoring whether the WebSocket endpoint generates a target event and obtaining the event type of the target event if the target event is generated, the method further includes: The WebSocket client and the corresponding WebSocket server are initialized and connected based on a preset initialization method, a connection address and connection parameter information.
3. The Java WebSocket disconnection reconnection method according to claim 2, characterized in that: Before the WebSocket client and the corresponding WebSocket server are initialized to connect based on the preset initialization method, connection address and connection parameter information, the method further includes: Retrieve the Java package of classes and interfaces used to perform WebSocket communication connections; The connection address and connection parameter information of the WebSocket client and the WebSocket server are set, and the initialization method of the WebSocket client to connect to the WebSocket server based on the Java package is defined.
4. The Java WebSocket disconnection reconnection method according to claim 1, characterized in that: If the event type is the first event type indicating that the target event is an open event, executing a timer scheduling thread initiated to the WebSocket server, and receiving a heartbeat reply fed back by the WebSocket server to realize the connection between the WebSocket client and the WebSocket server, including: If the event type is the first event type that characterizes that the target event is an open event, a heartbeat request is periodically sent to the WebSocket server through the configured scheduling frequency and time unit, and a heartbeat reply fed back by the WebSocket server is received to realize the connection between the WebSocket client and the WebSocket server.
5. The Java WebSocket disconnection reconnection method according to claim 4, characterized in that: The step of sending a heartbeat request to the WebSocket server at a scheduled frequency and time unit configured by the scheduled scheduling, and receiving a heartbeat reply fed back by the WebSocket server to achieve a connection between the WebSocket client and the WebSocket server includes: Send a zero-byte message body to the WebSocket server periodically through the configured scheduling frequency and time unit, receive the message body fed back by the WebSocket server, and then update the first type volatile variable value corresponding to the first event type, so as to determine whether to reconnect based on the first type volatile variable value, so as to realize the connection between the WebSocket client and the WebSocket server.
6. The Java WebSocket disconnection reconnection method according to claim 1, characterized in that: If the event type is a second event type indicating that the target event is an error event and / or a closing event, executing a disconnection and reconnection thread initiated to the WebSocket server to reconnect the WebSocket to the WebSocket server, including: If the event type is a second event type that characterizes that the target event is an error event and / or a shutdown event, a second type volatile variable value corresponding to the second event type is read, and whether to reconnect is determined based on the second type volatile variable value. If reconnected, the connection frequency of the WebSocket client is controlled based on a preset waiting time to achieve reconnection with the WebSocket server.
7. The Java WebSocket disconnection reconnection method according to claim 6, characterized in that: Before the method controls the connection frequency of the WebSocket client based on the preset waiting time to achieve reconnection with the WebSocket server, the method further includes: pass Determine the waiting time; wherein t represents the waiting time, n represents the exponential power, and random represents a random number in the waiting factor interval.
8. A Java WebSocket-based disconnection reconnection device, characterized in that: Applicable to WebSocket clients, including: An event monitoring module is used to monitor whether a target event is generated by a WebSocket endpoint, and if the target event is generated, obtain the event type of the target event; A first connection module, configured to execute a timed scheduling thread initiated to a WebSocket server if the event type is a first event type indicating that the target event is an open event, and receive a heartbeat reply fed back by the WebSocket server to achieve connection between the WebSocket client and the WebSocket server; The second connection module is used to execute a disconnection and reconnection thread initiated to the WebSocket server if the event type is a second event type that indicates that the target event is an error event and / or a closing event, so as to reconnect the WebSocket to the WebSocket server.
9. An electronic device, characterized in that: include: Memory, used to store computer programs; A processor is used to execute the computer program to implement the steps of the Java Web Socket disconnection reconnection method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: Used to store computer programs; wherein, when the computer program is executed by a processor, the steps of the Java WebSocket disconnection reconnection method according to any one of claims 1 to 7 are implemented.