Summary of IM practices on the Android platform and im summary on the android platform
Preface
The development of IM communication on the Internet has become a well-known technology in the world of mobile phones, especially in the era of rapid development of mobile Internet, among them, the old representatives include QQ and MSN, and the recent rise of the new, silent, easy-to-believe, and exchange applications are all dazzled by the Application of IM technology. I am an Android developer. The main reason for writing this article is that several apps I have developed include IM communication, in the process of constantly crawling and solving problems, I have accumulated some experience records and recorded them in my blog as my own summary. I can also share with other developers who need them, as a reference to practice solution, of course, I am not a hero of the gods and horses. If there is an error in the blog or the readers feel that they have doubts after reading it, you are welcome to leave your suggestion or question below the article as a comment.
A simple IM communication process from socket to XMPP is shown in steps 1-2-3-4 in the following model. messages sent and received between two mobile devices must be forwarded by the server.
Throughout the IM process, the server receives the request from the message sender and checks whether the receiver is online. If the server is online, the server directly sends the message to the receiver, if the message is not online, it is stored as an offline message in the database, waiting for the message to be sent when the receiver goes online.
As a network communication technology, IM will inevitably involve communication protocols. I am developing IM on the Android platform. Currently, I have two types of Protocols: socket and XMPP. Of course, the two are not in a parallel relationship. in a strict sense, socket is the cornerstone of all Java networks for communication. In Java, all network protocols are used for communication, in the end, it depends on socket. XMPP is used for communication on the Android platform, and the underlying layer is also dependent on socket implementation. The most typical reference example is smack, it is estimated that most small enterprises will use the XMPP protocol to encapsulate smack when developing IM to save development time. smack itself is developed in Java, and uses mina as the Core Implementation Framework (mina itself is a Java NIO communication framework, but it has not been updated for a long time, the author of mina wrote a new NIO communication framework called netty, which should be a replacement for mina ). The main advantages and disadvantages of IM communication as socket and XMPP protocols are as follows:
Therefore, in actual IM development, XMPP protocol and socket can be used for implementation, which can be determined based on the development application scenario and their advantages and disadvantages. Of course, it is not limited to IM development. The server can push messages to clients as a reference.
In addition to basic message sending and receiving and protocol usage selection, persistent connections and packet loss IM development also need to solve two common problems faced by all IM communication technologies.
1. Maintain a persistent connection between the client and the server. This is required in business scenarios. The basis of IM communication is to keep the client online on the server at all times so that the sender's messages can be sent to the receiver in a timely manner, especially after the design concept comes out, there is no difference between the online and offline status of IM clients, which is the biggest difference with QQ, in this case, the requirement for real-time online client is higher. How can I keep the client online in real time? Because the bottom layer of Java Network Communication depends on socket, and socket is divided into UDP and TCP, theoretically speaking, we can use a TCP socket for communication to ensure the implementation of persistent connections. However, this is only theoretical. Why can't we use TCP alone, this has something to do with the network environment and the operating system. We can see that the network environment and the operating system are short.
First, the above simple IM communication diagram only lists the servers and clients for IM communication, but it is very complex in the actual network environment (as shown in, the yellow lightning figure represents a connection.) The message we send is sent through many different carrier lines, and the switches and routers can finally reach the receiver's device, it is a simple network message transmission diagram, and the yellow lightning line is the persistent connection maintained by TCP. In theory, this link is always smooth, therefore, the client only needs to establish a connection before it can leave it alone.
However, from the perspective of performance and resource consumption, the use of socket is very resource-consuming, especially the TCP socket that maintains a persistent connection, so on the actual carrier device, this persistent connection will be disconnected after a short period of time. In this way, the operator wants to save and make full use of the device's resources. However, after a link is closed, the two sides will no longer be able to maintain persistent connections and communicate with each other, because if the connection is only dependent on TCP persistent connections, the server and the client do not know that the connection has been disconnected, so it will cause the data packet loss problem that will be discussed below. In addition to the long connection that the carrier will disconnect the device, the connection may also be broken for other reasons, for example, complicated factors such as the power failure or failure of the carrier's device, and the disconnection of device breakpoints on the mobile device receiver may make the TCP-based persistent connection solution unavailable, for example, the Android operating system will also process TCP persistent connections. As mentioned earlier, TCP socket persistent connections are very resource-consuming, this kind of resource is more valuable than mobile devices. Therefore, after a TCP persistent connection is idle for a certain period of time, the system will automatically clear the persistent connection on the device to save energy consumption on the mobile device. In order to completely solve the problem of persistent connections, IM technology actually introduces the heartbeat packet solution, which is not complicated to say, the client sends a request packet to the server every moment (this packet can be blank and agreed with the server) to tell the server that "I am online ", after receiving heartbeat request packets, the server also needs to send a message to the client to "I know" and maintain the existence of persistent connections through continuous message sending and receiving at the application layer, when the client does not receive any feedback from the server, the server is disconnected by default. After the service logic is processed, the server will be re-connected. Similarly, the server does not receive the heartbeat packet sent by the client, by default, the client is disconnected, and the communication socket on the server is disabled to recycle resources.
Second, packet loss occurs when the client sends data. In the first reason, dropping to the carrier and various network conditions will cause packet loss, in the following scenario, messages sent and received between A and B are also 1-2-4-4, but unfortunately, for various reasons, messages returned by B are lost when the IM Server forwards them to, at this time, A does not know that B has replied to his message, and the server does not know that A has not received B's reply. In this case, the user experience is poor.
So how should we deal with this packet loss problem? Currently, a simple and crude method is used to mark the message as an id every time the server sends a message to the client, after receiving the message, the client must return a receipt to the server as a proof that the current client has received the message. If the server sends the message but has not received the receipt, these messages are stored as offline messages in the database, and the offline messages are pushed to the client when the client is released again. This method can solve the problem of packet loss while the client and server are unaware of the packet loss. All of a sudden, the experience went up.
Due to the high real-time requirements of IM communication, the optimization performance consumes a lot of resources along with persistent connections, therefore, performance optimization is essential for the IM communication client on the mobile platform to achieve performance and experience. This makes a small Summary of IM Optimization on the Android platform, of course it may not be comprehensive. If you have better suggestions, please add them in the comments.
Summary The above content is only a summary of some of the knowledge and experience accumulated during the development process. I hope he can help other developers who are looking for information. At the same time, I would also like to thank many contributors to open-source projects and blog writers who love sharing. This article was born only after their contributions and sharing, and I made progress in the development process. The Word documents in this article will also be synchronized to my github repository,
Please refer to the source. Do not use it for commercial purposes..