How to implement the IM feature in the app four create a robust message sending module
Anim Chat is a heartbeat to keep the device and the server connection between the online, and sometimes because of network or other reasons cause the connection is interrupted, the user logged on the device on the server side will automatically offline. Then when the message is sent, the server returns the exception that the user has gone offline, causing the message to fail to send successfully.
In order to avoid this problem, we can use some techniques in the code to solve it.
1. Setting variables
Set a global variable isfromsendmsg to false. Private Boolean isfromsendmsg = false;
2. Determine if the user is online
When clicking the Send Message button on the interface, first determine if the user is online. If online, the message is sent directly. If you are not on the line, log in and send the message first.
//If the user is offline, log in again and send the message if (Anim.getcurrentstatus () == animstatus.offline) { refreshuiview (); // First refresh the interface //login Anim.connect (clientId); //sets the global variable to true, which means to send a message immediately after logging in, without going to the other process isFromSendMsg = true; } else { Anim.sendmessage (clientids,msg); } }
3. Login Judgment
Log in by the global variables to determine whether this login needs to send a message immediately. If so, change the global variables back before sending the message.
@Override public void Statusupdate (final animstatusupdatecallbackdata data) {if (isfromsendmsg && data). GetStatus () = = Animstatus.online) {isfromsendmsg = false; Anim.sendmessage (Clientids, msg); } else {//normal login process}}
4. Techniques for Interface Processing
Click the Send Message button, immediately put the data into the database, refresh the interface, with loading animation to express. When the message is successfully sent, the data is modified once, the interface is refreshed, and the loading animation is removed.
How to implement the IM feature series in the app article:
A common analysis of offline messages
The second fast realization of the offline message module
Three fast implementation of offline message push module
The four create a robust message sending module
The five rapid implementation of picture receiving and sending
The six fast realization of group chat advanced features
Seven fast implementation of chat list sorting module
This article from "Arrow Buckle technology Arrownock" blog, reproduced please contact the author!
How to implement the IM function in the app four create a robust message sending module-arrow Buckle technology Arrownock