Detailed Description
In order to explain technical contents, achieved objects, and effects of the present invention in detail, the following description is made with reference to the accompanying drawings in combination with the embodiments.
Referring to fig. 1, an embodiment of the present invention provides a message sending method, including:
acquiring a message sending request, and acquiring a corresponding group identifier and a corresponding message according to the message sending request;
acquiring access instances of all corresponding online users through the group identification, and sending the message to all the access instances;
inquiring long links of users to be sent in each access instance according to the group identification, and sending the message through the long links of the users to be sent;
and receiving confirmation messages sent after all the access instances successfully send the messages, and generating offline messages for the offline users in the group according to the confirmation messages.
From the above description, the beneficial effects of the present invention are: acquiring the content of the message and the group identification of the group according to the message sending request, sending the message to access instances of all online users corresponding to the group identification, confirming the long link of the user to be sent in the access instances according to the group identification, and sending the message through the long link of the user to be sent; after the completion message is sent to all the access instances of the online users, receiving the confirmation message of the completion of the sending, and generating the offline message for the offline users according to the confirmation message; therefore, the method and the device can directly send the message according to the long chain connection by establishing the relationship between the group identification and the access example and the relationship between the group identification and the long chain connection, and are different from the method of splitting one message into a plurality of message details and sequentially sending the message details to the corresponding user in the prior art.
Further, the obtaining the message sending request comprises:
establishing a relation between a group identifier and an access instance in a push cluster;
and establishing a relationship between the group identification and the long link of the user to be sent corresponding to the access instance in the storage space of the access instance.
As can be seen from the above description, before sending a message, the relationship between the group identifier and the access instance and the relationship between the group identifier and the long link of the user to be sent corresponding to the access instance are established, and by establishing the above relationship, the corresponding long link can be directly obtained through the group identifier, so that the message sending can be quickly implemented.
Further, the receiving the acknowledgement message sent after all the access instances successfully send the message includes:
receiving all message records sent after the access instance successfully sends the messages;
and acquiring the corresponding sent message detail according to the message record to obtain the confirmation message.
As can be seen from the above description, after a message is successfully sent to each access instance, the corresponding message record is sent, and the corresponding sent message details are obtained according to the message record, so that the specific condition of message sending is obtained through the sent message details.
Further, the generating an offline message for the offline users in the group according to the confirmation message includes:
acquiring the confirmation information, and obtaining unsent message details through asynchronous operation according to the sent message details;
and determining the offline users in the group according to the unsent message details, and generating offline messages for the offline users.
According to the above description, after the message is successfully sent to the online user, the unsent message detail is obtained according to the sent message detail, the offline user is further obtained, and the offline message is generated, so that the message sending process can be asynchronously processed, and the message sending delay is reduced.
Further, still include:
and judging whether the long-chain connection of the user to be sent keeps connected within a preset time through a timing task, if so, judging that the state of the user to be sent is on-line, otherwise, judging that the state of the user to be sent is off-line, and deleting the long-chain connection data of the user to be sent.
According to the description, whether the long-chain connection of the user to be sent keeps connection within the preset time or not is judged in real time, and the long-chain connection data of the offline user is deleted, so that the long-chain connection data indicate that the user is online, the message is directly sent, and a large amount of work of user data query is saved.
Referring to fig. 2, another embodiment of the present invention provides a message sending terminal, including a memory, a processor, and a computer program stored in the memory and running on the processor, where the processor executes the computer program to implement the following steps:
acquiring a message sending request, and acquiring a corresponding group identifier and a corresponding message according to the message sending request;
acquiring access instances of all corresponding online users through the group identification, and sending the message to all the access instances;
inquiring long links of users to be sent in each access instance according to the group identification, and sending the message through the long links of the users to be sent;
and receiving confirmation messages sent after all the access instances successfully send the messages, and generating offline messages for the offline users in the group according to the confirmation messages.
As can be seen from the above description, the content of the message and the group identifier of the group where the message is located are obtained according to the message sending request, the message is sent to the access instances of all online users corresponding to the group identifier, the long link of the user to be sent is confirmed in the access instances according to the group identifier, and the message is sent through the long link of the user to be sent; after the completion message is sent to all the access instances of the online users, receiving the confirmation message of the completion of the sending, and generating the offline message for the offline users according to the confirmation message; therefore, the method and the device can directly send the message according to the long chain connection by establishing the relationship between the group identification and the access example and the relationship between the group identification and the long chain connection, and are different from the method of splitting one message into a plurality of message details and sequentially sending the message details to the corresponding user in the prior art.
Further, the obtaining the message sending request comprises:
establishing a relation between a group identifier and an access instance in a push cluster;
and establishing a relationship between the group identification and the long link of the user to be sent corresponding to the access instance in the storage space of the access instance.
As can be seen from the above description, before sending a message, the relationship between the group identifier and the access instance and the relationship between the group identifier and the long link of the user to be sent corresponding to the access instance are established, and by establishing the above relationship, the corresponding long link can be directly obtained through the group identifier, so that the message sending can be quickly implemented.
Further, the receiving the acknowledgement message sent after all the access instances successfully send the message includes:
receiving all message records sent after the access instance successfully sends the messages;
and acquiring the corresponding sent message detail according to the message record to obtain the confirmation message.
As can be seen from the above description, after a message is successfully sent to each access instance, the corresponding message record is sent, and the corresponding sent message details are obtained according to the message record, so that the specific condition of message sending is obtained through the sent message details.
Further, the generating an offline message for the offline users in the group according to the confirmation message includes:
acquiring the confirmation information, and obtaining unsent message details through asynchronous operation according to the sent message details;
and determining the offline users in the group according to the unsent message details, and generating offline messages for the offline users.
According to the above description, after the message is successfully sent to the online user, the unsent message detail is obtained according to the sent message detail, the offline user is further obtained, and the offline message is generated, so that the message sending process can be asynchronously processed, and the message sending delay is reduced.
Further, still include:
and judging whether the long-chain connection of the user to be sent keeps connected within a preset time through a timing task, if so, judging that the state of the user to be sent is on-line, otherwise, judging that the state of the user to be sent is off-line, and deleting the long-chain connection data of the user to be sent.
According to the description, whether the long-chain connection of the user to be sent keeps connection within the preset time or not is judged in real time, and the long-chain connection data of the offline user is deleted, so that the long-chain connection data indicate that the user is online, the message is directly sent, and a large amount of work of user data query is saved.
The message sending method and the terminal are suitable for message pushing in a live scene or before a client, can reduce the message sending delay when sending messages to a large number of users, and are described in the following through specific implementation modes:
example one
Referring to fig. 1, a message sending method includes the steps of:
s1, obtaining the message sending request, and obtaining the corresponding group mark and the message according to the message sending request.
Wherein, before the acquiring the message sending request, the method comprises:
establishing a relation between a group identifier and an access instance in a push cluster;
and establishing a relationship between the group identification and the long link of the user to be sent corresponding to the access instance in the storage space of the access instance.
Specifically, the basic principle of message pushing is as follows: the long link is established between the client and one of the access instances in the access cluster of the server, and the message communication between the client and the server and between the client and the client is realized by using the long link as a communication channel.
In this embodiment, the push software development kit of the client has a function of adding groups, and a user can add a long link of the user into a required group by self-definition, wherein the group id of each group is unique;
for the users of the temporary group, the association relationship between the group and the long link needs to be established, the association relationship between the group and the long link is stored in the corresponding access instance, and the association relationship between the group and the access instance is established.
Specifically, in this embodiment, the client _ a and the access instance access _ a in the server access cluster have established a long link _ a, and at this time, after the client selects its own long link to join the group _ a, the server will implement the following operations:
the method comprises the steps that an incidence relation between a group and a long link is newly built in a memory of an access _ a instance, a data structure is map < key, value >, wherein key is group _ a, value is list < String >, each record in the list represents a long link, and a record link _ a is added;
the push cluster of the server generally uses a redis cluster as a data cache, at this time, an association relationship between a group and an access instance is added to the redis cluster, and the data structure is map < key, field, value >, where key is group _ a, field is instance id, and value is the number of long links of the group on this instance, where each record represents an access instance, and this edge is to add a record access _ a.
And acquiring a message sending request, acquiring a corresponding group identifier and a message according to the message sending request, and searching a corresponding access instance and a long link according to the group identifier.
S2, obtaining the corresponding access examples of all online users through the group identification, and sending the message to all the access examples.
Specifically, in this embodiment, when the user a sends a message to all users in the group _ a, after receiving the message, the server acquires, by the push cluster, access instances of all online long-link users of the group _ a from the redis cluster in advance, and sends the message to the access instances respectively.
S3, inquiring the long link of the user to be sent in each access instance according to the group identification, and sending the message through the long link of each user to be sent.
Specifically, in this embodiment, after the access instance receives the message, the long links of all the groups are obtained from the local memory according to the group _ a, and the message is directly sent to the client through the long link.
S4, receiving the confirmation message sent after all the access instances successfully send the message, and generating the offline message for the offline users in the group according to the confirmation message.
Wherein, the receiving the acknowledgement message sent after all the access instances successfully send the message includes:
receiving all message records sent after the access instance successfully sends the messages;
and acquiring the corresponding sent message detail according to the message record to obtain the confirmation message.
Wherein the generating of the offline message for the offline users in the group according to the confirmation message comprises:
acquiring the confirmation information, and obtaining unsent message details through asynchronous operation according to the sent message details;
and determining the offline users in the group according to the unsent message details, and generating offline messages for the offline users.
Specifically, after each access instance successfully sends a message, all online users send the completed message to the push service, and record the message record of each online user successfully sending in redis;
the data format of the message record is map < key, field, value >, wherein key is groupId, and field represents the userId which has been successfully sent, so that after a certain group of messages is sent, the message detail of the message which has been sent by the online user is recorded in redis, so that the subsequent asynchronous task can arrange the corresponding unsent message detail according to the message record, and the specific message generated by the unsent message detail is recorded in the database, thereby facilitating the user to obtain the offline message after the online.
Therefore, in this embodiment, it is not necessary to disassemble a plurality of message details from the message to be sent, the message is directly sent by the long link for the online user, and the offline information is stored in the redis after all the messages of the online user are sent, so that the message sending efficiency is greatly improved.
Example two
The difference between this embodiment and the first embodiment is that how to process the long link data after the user goes offline is further defined:
and judging whether the long-chain connection of the user to be sent keeps connected within a preset time through a timing task, if so, judging that the state of the user to be sent is on-line, otherwise, judging that the state of the user to be sent is off-line, and deleting the long-chain connection data of the user to be sent.
Specifically, if the user leaves the group, when the user actively goes offline, the server may sense the behavior, and at this time, the server has a cache for recording the relationship between the user and the group, that is, userId and List < groupId >. And then, deleting cache records of active offline users in the group and long link relation in advance according to the groupId. At this time, it is able to obtain which access instance the long link is on, that is, obtain the id of the access instance, and may simultaneously perform processing aiming at the association relationship cache of the group and the access instance, query the access number of the access instance of a certain group, and compare the access number with-1, when the value is 0, it indicates that there is no long link of the group on the instance, and at this time, may delete the cache record.
When the user is passively offline, such as in a broken network or bad network scene, a special timing task is used for judging whether long link is continuously maintained, and if the long link is disconnected for a long time, the user is considered to be actively offline.
EXAMPLE III
Referring to fig. 2, a message sending terminal includes a memory, a processor, and a computer program stored in the memory and executable on the processor, and the processor implements the steps of the message sending method according to the first embodiment or the second embodiment when executing the computer program.
In summary, the message sending method and the terminal provided by the present invention establish a relationship between a group and a long link and a relationship between a group and an access instance; acquiring the content of the message and the group identification of the group according to the message sending request, sending the message to access instances of all online users corresponding to the group identification, confirming the long link of the user to be sent in the access instances according to the group identification, and sending the message through the long link of the user to be sent; after the completion message is sent to all the access instances of the online users, receiving the confirmation message of the completion of the sending, and generating the offline message for the offline users according to the confirmation message; for the offline condition of the user, judging whether the user is actively offline or passively offline, if the user is actively offline, correspondingly deleting the relation between the group and the long link and the relation between the group and the access example, wherein the relation comprises the data of the actively offline user, if the user is passively offline, judging whether the offline duration of the user exceeds the preset time, if the offline duration exceeds the preset time, processing the user according to the active offline, ensuring the reliability of the data, and avoiding the condition that the user cannot receive the data due to temporary network disconnection; therefore, the method and the device can directly send the message according to the long chain connection by establishing the relationship between the group identification and the access example and the relationship between the group identification and the long chain connection, and are different from the method of splitting one message into a plurality of message details and sequentially sending the message details to the corresponding user in the prior art.
The above description is only an embodiment of the present invention, and not intended to limit the scope of the present invention, and all equivalent changes made by using the contents of the present specification and the drawings, or applied directly or indirectly to the related technical fields, are included in the scope of the present invention.