WebSocket-based message pushing method, system and device and storage medium

Through the WebSocket connection and Redis message routing between the client and the server, the problem of low efficiency of message push across service instances is solved, and the availability and stability of the server are improved.

CN118694816AInactive Publication Date: 2024-09-24GUANGZHOU JINGQI NETWORK TECH CO LTD

Patent Information

Application Number
CN202410845121.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-27
Publication Date
2024-09-24
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

The existing message push method based on WebSocket is inefficient in cross-service instance scenarios, affecting the availability and stability of the server.

Method used

The client initiates a WebSocket connection request to the server, establishes a connection between the service instance and the client, and determines the specific client for message push based on the target user's connection information. Redis is used for message routing to implement unicast, multicast and broadcast messages. push.

Benefits of technology

It improves the efficiency of cross-service instance message push and improves the availability and stability of the server.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118694816A
    Figure CN118694816A
Patent Text Reader

Abstract

The invention discloses a WebSocket-based message pushing method, system and device and a storage medium. The method comprises the following steps: establishing WebSocket connection between a server and a client; in response to the single-person message push request, determining a first client according to first connection information of the target user, and pushing a single-person message to the first client through the server; in response to the multicast message push request, determining a second client and a third client according to second connection information of the target user group, and pushing a multicast message to the second client and the third client through the server; and in response to the broadcast message pushing request, determining a fourth client according to the third connection information of the current service instance, determining a fifth client according to the fourth connection information of other service instances, and pushing a broadcast message to the fourth client and the fifth client through the server. According to the invention, the message pushing efficiency in a cross-service instance message pushing field is improved, and the method can be applied to the technical field of message pushing.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of message push, and in particular to a message push method, system, device and storage medium based on WebSocket. Background Art

[0002] The service capacity of a single WebSocket server is limited. If a service instance goes down, other service instances need to take over its work. When running multiple WebSocket service instances at the same time, different users may connect to different service instances. When one of the service instances needs to push a message to a client after processing the data, the client may not be connected to the service instance. This involves the problem of message routing. The following is a scenario for cross-service instance message push:

[0003] 1) For broadcast messages, service instance A needs to push messages to all clients on all service instances;

[0004] 2) For multicast messages, service instance A needs to push messages to other clients that belong to the group and are connected to other servers;

[0005] 3) For unicast messages, service instance A needs to push to a client of service instance B.

[0006] Conventional WebSocket is a one-way message push method, that is, the client sends messages to the server, or the server pushes messages to the client. When faced with the scenario of cross-service instance message push, the existing WebSocket-based message push method has the problem of low efficiency, which affects the availability and stability of the server. Summary of the invention

[0007] The purpose of the present invention is to solve one of the technical problems existing in the prior art to at least a certain extent.

[0008] To this end, an object of an embodiment of the present invention is to provide a message push method based on WebSocket, which improves the message push efficiency in a cross-service instance message push scenario, thereby improving the availability and stability of the server.

[0009] Another object of an embodiment of the present invention is to provide a message push system based on WebSocket.

[0010] In order to achieve the above technical objectives, the technical solutions adopted by the embodiments of the present invention include:

[0011] In a first aspect, an embodiment of the present invention provides a message push method based on WebSocket, comprising the following steps:

[0012] The client initiates a WebSocket connection request to the server, so that the server performs a WebSocket handshake process and establishes a WebSocket connection between the service instance and the client;

[0013] In response to a single-person message push request of the current service instance, determining a first client according to first connection information of a target user, and then pushing the single-person message to the first client through the server;

[0014] In response to the multicast message push request of the current service instance, determine the second client connected to the current service instance and the third client connected to other service instances according to the second connection information of the target user group, and then push the multicast message to the second client and the third client through the server;

[0015] In response to the broadcast message push request of the current service instance, the fourth client connected to the current service instance is determined according to the third connection information of the current service instance, and the fifth client connected to the other service instance is determined according to the fourth connection information of the other service instance, and then the broadcast message is pushed to the fourth client and the fifth client through the server.

[0016] Furthermore, in one embodiment of the present invention, the message push method further includes the following steps:

[0017] The client sends a heartbeat packet to the service instance, so that the service instance generates a heartbeat response packet according to the heartbeat packet, and then returns the heartbeat response packet to the client.

[0018] Furthermore, in one embodiment of the present invention, the message push method further includes the following steps:

[0019] When the service instance does not receive the heartbeat packet sent by the client within a preset time period, it is determined that the corresponding WebSocket connection is an idle WebSocket connection, and then the idle WebSocket connection is cleared.

[0020] Furthermore, in one embodiment of the present invention, the message push method further includes the following steps:

[0021] The client sends a business message to the service instance, so that the service instance parses and processes the business message, generates a message response packet according to the business processing result, and returns the message response packet to the client.

[0022] Further, in an embodiment of the present invention, determining the first client according to the first connection information of the target user, and then pushing the single-person message to the first client through the server specifically includes:

[0023] Calling the getWeb SocketOnlineUser method of WebSocketConnectionService through the WebSocketHolder of the current service instance to obtain the first connection information;

[0024] Determine the first client and several target service instances for establishing a WebSocket connection with the first client according to the first connection information;

[0025] When the target service instance is the current service instance, calling the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the current service instance to push the single-person message to the first client;

[0026] When the target service instance is the other service instance, a single-person message push event is sent to the target service instance through Redis, so that the WebSocketHolder of the target service instance calls the message push strategy of WebSocketConnectionActor to push the single-person message to the first client.

[0027] Further, in one embodiment of the present invention, the second connection information includes fifth connection information between the target user group and the current service instance and sixth connection information between the target user group and the other service instances, and the second client connected to the current service instance and the third client connected to the other service instances are determined according to the second connection information of the target user group, and then the multicast message is pushed to the second client and the third client through the server, which specifically includes:

[0028] Call tagsToConnectionIds through the WebSocketHolder of the current service instance to obtain the fifth connection information;

[0029] Determine a number of second clients connected to the current service instance according to the fifth connection information, and then push the multicast message to the second clients by calling the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the current service instance;

[0030] Sending a multicast message push event to the other service instance through Redis, so that the Web SocketHolder of the other service instance calls tagsToConnectionIds to obtain the sixth connection information;

[0031] A number of third clients connected to the other service instances are determined according to the sixth connection information, and then the message push strategy of WebSocketConnectionActor is called through the WebSocketHolder of the other service instances to push the multicast message to the third clients.

[0032] Further, in one embodiment of the present invention, determining a fourth client connected to the current service instance according to the third connection information of the current service instance, and determining a fifth client connected to the other service instance according to the fourth connection information of the other service instance, and then pushing a broadcast message to the fourth client and the fifth client through the server, specifically includes:

[0033] Calling webSocketConnectionActorMap through the WebSocketHolder of the current service instance to obtain the third connection information;

[0034] Determine a number of fourth clients connected to the current service instance according to the third connection information, and then push the broadcast message to the fourth clients by calling the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the current service instance;

[0035] Sending a broadcast message push event to the other service instance through Redis, so that the Web SocketHolder of the other service instance calls webSocketConnectionActorMap to obtain the fourth connection information;

[0036] A number of fifth clients connected to the other service instances are determined according to the fourth connection information, and then the message push strategy of WebSocketConnectionActor is called through the WebSocketHolder of the other service instances to push the broadcast message to the fifth clients.

[0037] In a second aspect, an embodiment of the present invention provides a message push system based on WebSocket, including:

[0038] A connection establishment module, used to initiate a WebSocket connection request to a server through a client, so that the server performs a WebSocket handshake process and establishes a WebSocket connection between a service instance and the client;

[0039] A single-person message push module, configured to respond to a single-person message push request of the current service instance, determine a first client according to the first connection information of a target user, and then push the single-person message to the first client through the server;

[0040] A multicast message push module, configured to respond to a multicast message push request of the current service instance, determine a second client connected to the current service instance and a third client connected to other service instances according to the second connection information of the target user group, and then push a multicast message to the second client and the third client through the server;

[0041] A broadcast message push module is used to respond to the broadcast message push request of the current service instance, determine the fourth client connected to the current service instance according to the third connection information of the current service instance, and determine the fifth client connected to the other service instance according to the fourth connection information of the other service instance, and then push the broadcast message to the fourth client and the fifth client through the server.

[0042] In a third aspect, an embodiment of the present invention provides a message push device based on WebSocket, comprising:

[0043] at least one processor;

[0044] at least one memory for storing at least one program;

[0045] When the at least one program is executed by the at least one processor, the at least one processor implements the above-mentioned WebSocket-based message push method.

[0046] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, which stores a program executable by a processor, and the program executable by the processor is used to execute the above-mentioned WebSocket-based message push method when executed by the processor.

[0047] The advantages and beneficial effects of the present invention will be partly given in the following description, partly become apparent from the following description, or be understood through the practice of the present invention:

[0048] In the embodiment of the present invention, a WebSocket connection request is initiated from the client to the server, so that the server performs a WebSocket handshake process and establishes a WebSocket connection between the service instance and the client, responds to a single-person message push request of the current service instance, determines a first client according to the first connection information of the target user, and then pushes a single-person message to the first client through the server, responds to a multicast message push request of the current service instance, determines a second client connected to the current service instance and a third client connected to other service instances according to the second connection information of the target user group, and then pushes a multicast message to the second client and the third client through the server, responds to a broadcast message push request of the current service instance, determines a fourth client connected to the current service instance according to the third connection information of the current service instance, and determines a fifth client connected to other service instances according to the fourth connection information of other service instances, and then pushes a broadcast message to the fourth client and the fifth client through the server. The embodiment of the present invention implements unicast, multicast and broadcast message push in a cross-service instance message push scenario based on WebSocket, improves the message push efficiency in a cross-service instance message push scenario, and thus improves the availability and stability of the server. BRIEF DESCRIPTION OF THE DRAWINGS

[0049] In order to more clearly illustrate the technical solution in the embodiments of the present invention, the following introduction is made to the drawings required for use in the embodiments of the present invention. It should be understood that the drawings introduced below are only for the convenience of clearly describing some embodiments of the technical solution of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.

[0050] Figure 1 A flowchart of a method for pushing messages based on WebSocket provided by an embodiment of the present invention;

[0051] Figure 2 An interactive schematic diagram of establishing a WebSocket connection provided by an embodiment of the present invention;

[0052] Figure 3 A schematic diagram of an interaction of responding to a heartbeat packet provided by an embodiment of the present invention;

[0053] Figure 4 An interactive schematic diagram of cleaning up idle WebSocket connections provided by an embodiment of the present invention;

[0054] Figure 5 A schematic diagram of an interaction of a response service message provided in an embodiment of the present invention;

[0055] Figure 6A schematic diagram of the interaction of a single-person message push provided by an embodiment of the present invention;

[0056] Figure 7 A schematic diagram of interaction of multicast message push provided by an embodiment of the present invention;

[0057] Figure 8 A schematic diagram of the interaction of broadcast message push provided by an embodiment of the present invention;

[0058] Fig. 9 A structural block diagram of a message push system based on WebSocket provided by an embodiment of the present invention;

[0059] Fig.10 A structural block diagram of a message push device based on WebSocket provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0060] The embodiments of the present invention are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and are not to be construed as limitations of the present invention. For the step numbers in the following embodiments, they are only provided for the convenience of explanation, and the order between the steps is not limited in any way, and the execution order of each step in the embodiment can be adaptively adjusted according to the understanding of those skilled in the art.

[0061] In the description of the present invention, the meaning of "a plurality" is two or more than two. If there is a description of "a first" or "a second", it is only used for the purpose of distinguishing technical features, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features or implicitly indicating the order of the indicated technical features. In addition, unless otherwise defined, all technical and scientific terms used in this document have the same meaning as those commonly understood by those skilled in the art.

[0062] Reference Figure 1 The embodiment of the present invention provides a message push method based on WebSocket, which specifically includes the following steps:

[0063] S101. The client initiates a WebSocket connection request to the server, so that the server performs a WebSocket handshake process and establishes a WebSocket connection between the service instance and the client.

[0064] Specifically, the embodiment of the present invention uses Spring WebFlux to fully utilize the asynchronization capability to improve thread reuse, reduce the number of service worker threads, and achieve the purpose of using a small number of threads to serve a large number of users. Spring WebFlux provides the following services:

[0065] The WebSocketHandlerAdapter class uses HandshakeWebSocketService internally to process WebSocket handshake requests;

[0066] The WebSocketHandler interface provides the developer with a WebSocketSession object after the Adapter completes the handshake and establishes the connection, providing the ability to send and receive messages with the client.

[0067] When Spring WebFlux starts, the unique identifier (instance ID) of the current service instance is read through system configuration; the HandlerMapping Bean is registered mainly to inform the Spring WebFlux framework that the processor provided by the specific URL address is a specific WebSocketHandler instance; the program writes an implementation class to implement the handle method of the WebSocketHandler interface and take over the specific operations after the WebSocket connection.

[0068] The program provides the WebSocketHolder class, which is used to manage all connection information currently connected to the service and manage it. The WebSocketHolder class contains the following properties:

[0069] 1. The program implements the WebSocketConnectionService class and provides the following methods:

[0070] 1) upsertUserConnection: insert or update user connection information into Redis;

[0071] 2)removeUserConnection: delete user connection information from Redis;

[0072] 3)getIdleWebSocketConnections: Get idle WebSocket connection information;

[0073] 4) getWebSocketOnlineUser: Get WebSocket connection information based on user ID;

[0074] 5)getWebSocketOnlineUsers: Get all user WebSocket connection information.

[0075] 2. The ServiceInvoker class implemented by the program, when the program receives a WebSocket message from a user, triggers the caller of a specific business logic method of a WebSocket service.

[0076] 3. Redis-related object instances mainly synchronize messages to other instances through the Redis Pub / Sub mechanism to implement message routing.

[0077] 4.WebSocketInstanceConfig configuration class, mainly used to obtain the identifier of the current service instance and some other configurations.

[0078] 5.webSocketConnectionActorMap: A Has h mapping of ConnectionId->WebSocketConnectionActor, which mainly stores the mapping of connection identifier to WebSocketConnectionActor. WebSocketConnectionActor is a class implemented by the program, with the following properties: record connection ID, WebSocketSession, user identity information, tag collection, and mainly provide the following functions:

[0079] 1) Record the above attributes;

[0080] 2) Obtain the message sent by the user through WebSocketSession. When the user sends a message, the caller callback is triggered and the message is passed upward;

[0081] 3) Obtain the transmitter that pushes messages to users from WebSocketSession, and provide the sendMessage method for external calls to push messages to users;

[0082] 4) Provide a close method for external calls to disconnect the WebSocket connection from the server.

[0083] 6.tagsToConnectionIds:Tag->Set <conectionid>A Hash map that mainly stores the mapping from a label to a set of connection IDs.

[0084] When WebSocketHolder is registered with the Spring container, it subscribes to the following Redis events:

[0085] 1. Idle connection release event;

[0086] 2. Unicast message sending event;

[0087] 3. Broadcast message sending event;

[0088] 4. Multicast (tag) message sending event.

[0089] like Figure 2 The following is an interactive diagram of establishing a WebSocket connection provided by an embodiment of the present invention. The client initiates a WebSocket connection request to the server, and the server forwards the request to the WebSocketHandler class implemented by the program according to the Url Mapping, calls the handle method, and performs the following operations:

[0090] 1. Get the WebSocketSession object (which can be understood as a session object or connection object) and obtain the server-side active closing, message sending and receiving capabilities of the connection. When WebFlux calls the handle method of WebSocketHandler, the WebS ocketSession object is passed to the method as a parameter.

[0091] 2. Obtain user identity through user authentication. Obtain user authentication information of the WebSocket connection request through Http Cookie, verify the legitimacy of the connection request, and close the connection through the WebSocketSession.close method if it is not legal; obtain user identity information if it is legal.

[0092] 3. Extract the user's tag information (for multicast) based on the user's identity information. Tags are used to group users. Services can add several tags to users according to business needs, and then perform batch multicast based on user tags.

[0093] 4. Call the put method of the WebSocketHolder class:

[0094] 1) Create an object instance of the WebSocketConnectionActor class, assign a connection identifier (Id) to the session, and record the connection identifier and session object, authenticated user, and tag information within the instance;

[0095] 2) Recorded in webSocketConnectionActorMap and tagsToConnectionIds;

[0096] 3) Call the upsertUserConnection method of WebSocketConnectionService to record the connection information to Redis. The Redis data is as follows:

[0097] (1)Key variable:

[0098] a. WEBSOCKET_SERVICE_NAME: WebSocket service name, one WebSocket service URL and one WebSocket service name;

[0099] b. USER_ID_MOD_NUM: To prevent a single key from being too large, the user heartbeat information is divided into several keys for storage. Currently, the program uses the last digit of the user ID as the shard.

[0100] (2) Connection heartbeat Key:

[0101] a. WS_HB: {WEBSOCKET_SERVICE_NAME:USER_ID_MOD_NUM};

[0102] b. Data type: SortSet;

[0103] c. Value: User Id: Connection Id;

[0104] d. Score: the timestamp of the user's last heartbeat;

[0105] e. Used to store the last heartbeat time of all connections of all users.

[0106] (3) User connection key:

[0107] a. WS_CONN:{WEBSOCKET_SERVICE_NAME:USER_ID_MOD_NUM};

[0108] b. Data type: Hash;

[0109] c. SubKey: User ID;

[0110] d. Value: connection information Json string, including the user ID and all the connection information Map of the user (connection ID->connection information mapping). The connection information includes the server instance ID (instanceId) where the connection is located.

[0111] (4) The contents of the curly brackets of the two data keys are the same. In the Redis cluster mode, the key slots are calculated as the same slot, which makes it easier to operate the two keys at the same time through Lua scripts.

[0112] 4) Register the disconnection callback of WebSocketSession, triggering the onClose method of this class to close the connection;

[0113] 5) Register the callback for WebSocketSession to receive user messages, triggering the onMessage method of this class to receive messages.

[0114] As an optional implementation, the message push method further includes the following steps:

[0115] S105. Send a heartbeat packet to the service instance through the client, so that the service instance generates a heartbeat response packet according to the heartbeat packet, and then returns the heartbeat response packet to the client.

[0116] Specifically, after the WebSocket connection is established, the client needs to trigger a heartbeat periodically (for example, every 30 seconds), mainly to solve the following problems:

[0117] 1. Connections that are connected for a long time without data interaction will be disconnected by the operator at the intermediate node of the connection link. Although the standard WebSocket connection protocol has designed ping and pong heartbeat packets, the heartbeat packets are not triggered in the actual usage scenario of the Chrome browser, and the business test needs to implement the heartbeat separately;

[0118] 2. Due to non-standard client implementation, WebSocket may remain connected, but there is actually no data interaction. This invalid connection needs to be actively disconnected by the server to save server resources.

[0119] like Figure 3 The figure shows an interactive schematic diagram of responding to a heartbeat packet provided in an embodiment of the present invention. The client sends a heartbeat packet periodically, the backend receives the heartbeat packet, calls upsertUserConnection of WebSocketConnectionService to refresh the heartbeat data timestamp to the current system timestamp, and the backend pushes the heartbeat response data packet to the client. The client receives the heartbeat response data packet and considers that the connection is available, otherwise it considers that the connection is unavailable, disconnects the connection and re-establishes a new connection.

[0120] As an optional implementation, the message push method further includes the following steps:

[0121] S106: When the service instance does not receive a heartbeat packet sent by the client within a preset time period, it determines that the corresponding Web Socket connection is an idle WebSocket connection, and then clears the idle WebSocket connection.

[0122] Specifically, after implementing the connection heartbeat, the backend is able to disconnect some idle connections without heartbeats in order to release unnecessary wasted resources. Idle connections are triggered regularly in WebSocketHolder (for example, 3 times the heartbeat interval, 90 seconds), and are triggered and controlled by @Scheduled and @SchedulerLock to have only one service instance triggered at the same trigger time. At the same time, since only a single instance is triggered, when triggered, the connections that need to be cleaned up by each instance will be delivered to other service instances through the Redis Pub / Sub mechanism.

[0123] like Figure 4 The figure shows an interactive schematic diagram of cleaning up idle WebSocket connections provided by an embodiment of the present invention. The specific process is as follows:

[0124] 1. The scheduled task triggers the clearIdleWebSocketConnect method of WebSocketHolder of instance A, calls WebSocketconnectionService to obtain idle WebSocket connection information, obtains all connection IDs 90 seconds before the current system time in the WS_HB Key of Redis through Lua, and obtains the information of these connections.

[0125] 2. Traverse all idle connection data and check whether it is the connection data of the current instance. If so:

[0126] 1) Call WebSocketConnectionActor to close the WebSocket connection;

[0127] 2) Call the WebSocketconnectionService method to delete the connection data from Redis;

[0128] 3) Delete the connection information from the local variable webSocketConnectionActorMap;

[0129] 4) Remove the connection information from the local variable tagsToConnectionIds.

[0130] otherwise:

[0131] 1) Publish an idle connection clearing event to Redis;

[0132] 2) Other instances receive the idle connection clearing event and confirm whether it is the event of this instance. If it is:

[0133] (1) Call WebSocketConnectionActor to close the WebSocket connection;

[0134] (2) Call the WebSocketconnectionService method to delete the connection data from Redis;

[0135] (3) Delete the connection information from the local variable webSocketConnectionActorMap;

[0136] (4) Remove the connection information from the local variable tagsToConnectionIds;

[0137] Otherwise, no processing is performed.

[0138] As an optional implementation, the message push method further includes the following steps:

[0139] S107: Send a business message to the service instance through the client, so that the service instance parses and processes the business message, generates a message response package according to the business processing result, and returns the message response package to the client.

[0140] Specifically, Figure 5 The figure shows an interaction diagram of a response service message provided by an embodiment of the present invention. The specific process is as follows:

[0141] 1. The client sends a business message to the server, which is triggered by the WebSocketSession's receive message callback.

[0142] 2. The WebSocketConnectionActor's receive message callback is triggered.

[0143] 3. WebSocketHolder performs the following:

[0144] 1) onMessage callback is triggered;

[0145] 2) Parse the data packet;

[0146] 3) Confirmed as business message;

[0147] 4) Call ServiceInvoker to trigger the calling of business methods;

[0148] 5) ServiceInvoker calls the business method;

[0149] 6) Business service processing business;

[0150] 7) The business processing result is returned to ServiceInvoker->WebSocketHolder->WebSocketConnectionActor;

[0151] 8) WebSocketConnectionActor pushes the business processing results to the client through WebSocketSession.

[0152] S102: In response to a single-person message push request of the current service instance, determine a first client according to first connection information of a target user, and then push the single-person message to the first client through the server.

[0153] Specifically, in addition to receiving client messages and pushing them after processing business, the system's business services will also push messages to active users after processing the business.

[0154] As an optional implementation, determining the first client according to the first connection information of the target user, and then pushing the single-person message to the first client through the server specifically includes:

[0155] S1021. Call the get WebSocketOnlineUser method of WebSocketConnectionService through the WebSocketHolder of the current service instance to obtain the first connection information;

[0156] S1022, determining the first client and several target service instances for establishing a WebSocket connection with the first client according to the first connection information;

[0157] S1023: When the target service instance is the current service instance, the message push strategy of WebSocketConnectionActor is called through the WebSocketHolder of the current service instance to push the single-person message to the first client;

[0158] S1024: When the target service instance is another service instance, a single-person message push event is sent to the target service instance through Redis, so that the WebSocketHolder of the target service instance calls the message push strategy of WebSocketConnectionActor to push the single-person message to the first client.

[0159] like Figure 6 The figure shows an interactive schematic diagram of a single-person message push provided by an embodiment of the present invention. The specific process is as follows:

[0160] 1. Business service triggers single-person message push.

[0161] 2. The business calls the single-person message push method of WebSocketHolder.

[0162] 3. WebSocketHolder calls the getWebSocketOnlineUser method of WebSocketConnectionService to obtain all connection information of the user. The user may be connected to instance A or to other instances at the same time.

[0163] 4. Get the connection connected to the current business process and call the push message method of WebSocketConnectionActor to push the message.

[0164] 5. Get a connection to a non-current instance and publish the single message push time to Redis.

[0165] 6. Other instances Subscribe to the event, confirm that it is a message push for the connection connected to the current instance, and call the push message method of WebSoc ketConnectionActor to push the message.

[0166] S103, in response to the multicast message push request of the current service instance, determine the second client connected to the current service instance and the third client connected to other service instances according to the second connection information of the target user group, and then push the multicast message to the second client and the third client through the server.

[0167] Further as an optional implementation manner, the second connection information includes fifth connection information between the target user group and the current service instance and sixth connection information between the target user group and other service instances, and the second client connected to the current service instance and the third client connected to other service instances are determined according to the second connection information of the target user group, and then the multicast message is pushed to the second client and the third client through the server, which specifically includes:

[0168] S1031, calling tagsToConnectionIds through the WebSocketHolder of the current service instance to obtain the fifth connection information;

[0169] S1032, determining a number of second clients connected to the current service instance according to the fifth connection information, and then calling the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the current service instance to push the multicast message to the second clients;

[0170] S1033, sending a multicast message push event to other service instances through Redis, so that the WebSocketHolder of the other service instances calls tagsToConnectionIds to obtain the sixth connection information;

[0171] S1034. Determine a number of third clients connected to other service instances according to the sixth connection information, and then call the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the other service instances to push multicast messages to the third clients.

[0172] like Figure 7 The figure shows an interactive schematic diagram of multicast message push provided by an embodiment of the present invention, and the specific process is as follows:

[0173] 1. Business services trigger multicast message push.

[0174] 2. The business calls the multicast push method of WebSocketHolder.

[0175] 3. The WebSocketHolder of the current instance obtains all connection IDs of the group through tagsToConnectionIds.

[0176] 4. The WebSocketHolder of the current instance traverses and calls the push message method of WebSocketConnectionActor to push messages.

[0177] 5. The current instance WebSocketHolder publishes a multicast message push event.

[0178] 6. Other instances Subscribe to events, obtain all connection IDs of the group through tagsToConnectionIds of their WebSocketHolder, and traverse and call the push message method of WebSocketConnectionActor to push messages.

[0179] S104. In response to the broadcast message push request of the current service instance, determine the fourth client connected to the current service instance according to the third connection information of the current service instance, and determine the fifth client connected to other service instances according to the fourth connection information of other service instances, and then push the broadcast message to the fourth client and the fifth client through the server.

[0180] Further as an optional implementation, determining a fourth client connected to the current service instance according to the third connection information of the current service instance, and determining a fifth client connected to other service instances according to the fourth connection information of other service instances, and then pushing a broadcast message to the fourth client and the fifth client through the server, specifically includes:

[0181] S1041. Call webSocketConnectionActorMap through the WebSocketHolder of the current service instance to obtain third connection information;

[0182] S1042, determining a number of fourth clients connected to the current service instance according to the third connection information, and then calling the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the current service instance to push the broadcast message to the fourth clients;

[0183] S1043, sending a broadcast message push event to other service instances through Redis, so that the WebSocketHolder of other service instances calls webSocketConnectionActorMap to obtain the fourth connection information;

[0184] S1044. Determine a number of fifth clients connected to other service instances according to the fourth connection information, and then push broadcast messages to the fifth clients by calling the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the other service instances.

[0185] Specifically, Figure 8 The figure shows an interactive schematic diagram of broadcast message push provided by an embodiment of the present invention, and the specific process is as follows:

[0186] 1. Business services trigger broadcast message push.

[0187] 2. The business calls the broadcast push method of WebSocketHolder.

[0188] 3. The WebSocketHolder of the current instance obtains all connection IDs connected to the current instance through webSocketConnectionActorMap.

[0189] 4. The WebSocketHolder of the current instance traverses and calls the push message method of WebSocketConnectionActor to push messages.

[0190] 5. The current instance WebSocketHolder publishes a broadcast message push event.

[0191] 6. Other instances Subscribe to events and obtain all connection IDs connected to the current instance through the webSocketConnectionActorMap of its WebSocketHolder.

[0192] a. Traverse and call the push message method of WebSocketConnectionActor to push messages.

[0193] The above is a description of the method steps of the embodiment of the present invention. It can be recognized that the embodiment of the present invention implements unicast, multicast and broadcast message push in the cross-service instance message push scenario based on WebSocket, improves the message push efficiency in the cross-service instance message push scenario, and thus improves the availability and stability of the server.

[0194] Reference Fig. 9 , an embodiment of the present invention provides a message push system based on WebSocket, including:

[0195] The connection establishment module is used to initiate a WebSocket connection request from the client to the server, so that the server performs a WebSocket handshake process and establishes a WebSocket connection between the service instance and the client;

[0196] A single-person message push module, configured to respond to a single-person message push request of the current service instance, determine a first client according to the first connection information of a target user, and then push the single-person message to the first client through the server;

[0197] A multicast message push module, for responding to a multicast message push request of the current service instance, determining a second client connected to the current service instance and a third client connected to other service instances according to the second connection information of the target user group, and then pushing a multicast message to the second client and the third client through the server;

[0198] A broadcast message push module is used to respond to the broadcast message push request of the current service instance, determine the fourth client connected to the current service instance according to the third connection information of the current service instance, and determine the fifth client connected to other service instances according to the fourth connection information of other service instances, and then push the broadcast message to the fourth client and the fifth client through the server.

[0199] The contents of the above method embodiments are all applicable to the present system embodiments. The functions specifically implemented by the present system embodiments are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.

[0200] Reference Fig.10 The embodiment of the present invention provides a message push device based on WebSocket, including:

[0201] at least one processor;

[0202] at least one memory for storing at least one program;

[0203] When the at least one program is executed by the at least one processor, the at least one processor implements the WebSocket-based message push method.

[0204] The contents of the above method embodiments are all applicable to the present device embodiments. The functions specifically implemented by the present device embodiments are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.

[0205] An embodiment of the present invention further provides a computer-readable storage medium, in which a program executable by a processor is stored. When the program executable by the processor is executed by the processor, it is used to execute the above-mentioned message push method based on WebSocket.

[0206] A computer-readable storage medium according to an embodiment of the present invention can execute a message push method based on WebSocket provided by an embodiment of the method of the present invention, can execute any combination of implementation steps of the method embodiment, and has the corresponding functions and beneficial effects of the method.

[0207] The embodiment of the present invention also discloses a computer program product or a computer program, wherein the computer program product or the computer program includes computer instructions, and the computer instructions are stored in a computer-readable storage medium. A processor of a computer device can read the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes Figure 1 The method shown.

[0208] In some selectable embodiments, the function / operation mentioned in the block diagram may not occur in the order mentioned in the operation diagram. For example, depending on the function / operation involved, the two boxes shown in succession can actually be executed substantially simultaneously or the above-mentioned boxes can sometimes be executed in reverse order. In addition, the embodiment presented and described in the flow chart of the present invention is provided by way of example, for the purpose of providing a more comprehensive understanding of technology. The disclosed method is not limited to the operation and logic flow presented herein. Selectable embodiments are expected, wherein the order of various operations is changed and the sub-operation of a part for which is described as a larger operation is performed independently.

[0209] In addition, although the present invention is described in the context of functional modules, it should be understood that, unless otherwise specified to the contrary, one or more of the above-mentioned functions and / or features can be integrated into a single physical device and / or software module, or one or more functions and / or features can be implemented in a separate physical device or software module. It is also understood that a detailed discussion of the actual implementation of each module is unnecessary for understanding the present invention. More specifically, in view of the properties, functions and internal relationships of the various functional modules in the device disclosed herein, the actual implementation of the module will be understood within the conventional skills of the engineer. Therefore, those skilled in the art can implement the present invention set forth in the claims without excessive experimentation using ordinary techniques. It is also understood that the specific concepts disclosed are merely illustrative and are not intended to limit the scope of the present invention, which is determined by the full scope of the appended claims and their equivalents.

[0210] If the above functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the above methods of each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.

[0211] The logic and / or steps represented in the flowchart or otherwise described herein, for example, may be considered as an ordered list of executable instructions for implementing logical functions, and may be embodied in any computer-readable medium for use by an instruction execution system, apparatus or device (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, apparatus or device and execute the instructions), or in conjunction with such instruction execution system, apparatus or device. For purposes of this specification, "computer-readable medium" may be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, apparatus or device, or in conjunction with such instruction execution system, apparatus or device.

[0212] More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection with one or more wires (electronic device), a portable computer disk case (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and editable read-only memory (EPROM or flash memory), an optical fiber device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be a paper or other suitable medium on which the above-mentioned program is printed, since the above-mentioned program can be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, deciphering or processing in other suitable ways as necessary, and then stored in a computer memory.

[0213] It should be understood that the various parts of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, a plurality of steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.

[0214] In the above description of this specification, the description with reference to the terms "one embodiment / example", "another embodiment / example" or "certain embodiments / examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner.

[0215] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the claims and their equivalents.

[0216] The above is a specific description of the preferred implementation of the present invention, but the present invention is not limited to the above embodiments. Those skilled in the art may make various equivalent modifications or substitutions without violating the spirit of the present invention. These equivalent modifications or substitutions are all included in the scope defined by the claims of this application.< / conectionid>

Claims

1. A message push method based on WebSocket, characterized in that: The following steps are involved: The client initiates a WebSocket connection request to the server, so that the server performs a WebSocket handshake process and establishes a WebSocket connection between the service instance and the client; In response to a single-person message push request of the current service instance, determining a first client according to first connection information of a target user, and then pushing the single-person message to the first client through the server; In response to the multicast message push request of the current service instance, determine the second client connected to the current service instance and the third client connected to other service instances according to the second connection information of the target user group, and then push the multicast message to the second client and the third client through the server; In response to the broadcast message push request of the current service instance, the fourth client connected to the current service instance is determined according to the third connection information of the current service instance, and the fifth client connected to the other service instance is determined according to the fourth connection information of the other service instance, and then the broadcast message is pushed to the fourth client and the fifth client through the server.

2. A message push method based on WebSocket according to claim 1, characterized in that: The message push method also includes the following steps: The client sends a heartbeat packet to the service instance, so that the service instance generates a heartbeat response packet according to the heartbeat packet, and then returns the heartbeat response packet to the client.

3. A message push method based on WebSocket according to claim 2, characterized in that: The message push method also includes the following steps: When the service instance does not receive the heartbeat packet sent by the client within a preset time period, it is determined that the corresponding WebSocket connection is an idle WebSocket connection, and then the idle WebSocket connection is cleared.

4. A message push method based on WebSocket according to claim 1, characterized in that: The message push method also includes the following steps: The client sends a business message to the service instance, so that the service instance parses and processes the business message, generates a message response packet according to the business processing result, and returns the message response packet to the client.

5. A message push method based on WebSocket according to claim 1, characterized in that: The determining the first client according to the first connection information of the target user, and then pushing the single-person message to the first client through the server, specifically includes: Calling the getWebSocketOnlineUser method of WebSocketConnectionService through the WebSocketHolder of the current service instance to obtain the first connection information; Determine the first client and several target service instances for establishing a WebSocket connection with the first client according to the first connection information; When the target service instance is the current service instance, calling the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the current service instance to push the single-person message to the first client; When the target service instance is the other service instance, a single-person message push event is sent to the target service instance through Redis, so that the WebSocketHolder of the target service instance calls the message push strategy of WebSocketConnectionActor to push the single-person message to the first client.

6. A message push method based on WebSocket according to claim 1, characterized in that: The second connection information includes fifth connection information between the target user group and the current service instance and sixth connection information between the target user group and the other service instances, and determining the second client connected to the current service instance and the third client connected to the other service instances according to the second connection information of the target user group, and then pushing the multicast message to the second client and the third client through the server, specifically includes: Call tagsToConnectionIds through the WebSocketHolder of the current service instance to obtain the fifth connection information; Determine a number of second clients connected to the current service instance according to the fifth connection information, and then push the multicast message to the second clients by calling the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the current service instance; Send a multicast message push event to the other service instance through Redis, so that the WebSocketHolder of the other service instance calls tagsToConnectionIds to obtain the sixth connection information; A number of third clients connected to the other service instances are determined according to the sixth connection information, and then the message push strategy of WebSocketConnectionActor is called through the WebSocketHolder of the other service instances to push the multicast message to the third clients.

7. A message push method based on WebSocket according to claim 1, characterized in that: The determining, according to the third connection information of the current service instance, a fourth client connected to the current service instance, and determining, according to the fourth connection information of the other service instance, a fifth client connected to the other service instance, and then pushing a broadcast message to the fourth client and the fifth client through the server specifically includes: Calling webSocketConnectionActorMap through the WebSocketHolder of the current service instance to obtain the third connection information; Determine a number of fourth clients connected to the current service instance according to the third connection information, and then push the broadcast message to the fourth clients by calling the message push strategy of WebSocketConnectionActor through the WebSocketHolder of the current service instance; Send a broadcast message push event to the other service instance through Redis, so that the WebSocketHolder of the other service instance calls webSocketConnectionActorMap to obtain the fourth connection information; A number of fifth clients connected to the other service instances are determined according to the fourth connection information, and then the message push strategy of WebSocketConnectionActor is called through the WebSocketHolder of the other service instances to push the broadcast message to the fifth clients.

8. A message push system based on WebSocket, characterized in that: include: A connection establishment module, used to initiate a WebSocket connection request to a server through a client, so that the server performs a WebSocket handshake process and establishes a WebSocket connection between a service instance and the client; A single-person message push module, configured to respond to a single-person message push request of the current service instance, determine a first client according to the first connection information of the target user, and then push the single-person message to the first client through the server; A multicast message push module, configured to respond to a multicast message push request of the current service instance, determine a second client connected to the current service instance and a third client connected to other service instances according to the second connection information of the target user group, and then push a multicast message to the second client and the third client through the server; A broadcast message push module is used to respond to the broadcast message push request of the current service instance, determine the fourth client connected to the current service instance according to the third connection information of the current service instance, and determine the fifth client connected to the other service instance according to the fourth connection information of the other service instance, and then push the broadcast message to the fourth client and the fifth client through the server.

9. A message push device based on WebSocket, characterized in that: include: at least one processor; at least one memory for storing at least one program; When the at least one program is executed by the at least one processor, the at least one processor implements a WebSocket-based message push method as described in any one of claims 1 to 7.

10. A computer-readable storage medium storing a program executable by a processor, characterized in that: The program executable by the processor is used to execute a message push method based on WebSocket when executed by the processor.

Citation Information

Patent Citations

  • Data forwarding method, API gateway and message service system

    CN112202872A

  • Message processing method, device and system

    CN112769671A

  • Message pushing method and device, equipment and storage medium

    CN113452774A

  • Message processing method, device and equipment and computer readable storage medium

    CN113472846A

  • Conference message pushing method, conference server and electronic equipment

    CN115865874A

Cited By

  • Service cluster websocket connection and message distribution method under micro-service architecture

    CN120281805A