"Android Instant Messaging Development Summary " based on various common problems of IM andriod development, combined with the practice of instant communication technology of NetEase Cloud , I make a comprehensive summary of IM development.
Related recommended reading:,
Android Instant Messaging Development Summary (i)
Mobile IM Development Guide 1: How to choose a technology
Mobile IM Development Guide 2: Heart rate Instructions
Mobile IM Development Guide 3: How to optimize login modules
Establish a secure connection
Security is another hard requirement for IM software. If the communication data is intercepted by a third party when the message is delivered, it is guaranteed that no one else can get the real content. The process of secure connection can refer to the way of HTTPS, the server will send the certificate to the client, the client produces a symmetric key, and the server certificate is encrypted and handed over to the server, then the communication will all use this symmetric key to encrypt. Of course, there are two points need to be different from HTTPS, the first is the way to obtain the certificate, HTTPS is by the specialized agencies to verify the validity of the certificate, IM clients will certainly not do so, in order to prevent the process of obtaining a certificate is intercepted, and then tamper with the certificate, A possible way is to directly enter the certificate directly into the client installation package, which can be upgraded along with the client software upgrade, or through a protocol upgrade after the encrypted connection. The second problem is the choice of symmetric cryptographic algorithms, because the life cycle of the key is not long, and the mobile App is very sensitive to power consumption, so the encryption algorithm should try to choose a simpler type, such as RC4.
Heart Jump
The heartbeat can be divided into TCP protocol layer heartbeat and app layer heartbeat. In general, we use the application-level heartbeat, one to facilitate server expansion (such as the day we can switch to UDP), and the second is more flexible to control the heartbeat interval.
Heartbeat protocol is only used to connect the keepalive, its content should be as concise as possible, in addition to the necessary parts of Baotou, the package of the optional Baotou does not exist.
For different network environments, the heartbeat can take different time intervals. In different network environment, the choice of interval can refer to the intelligent Heartbeat scheme.
Wire Break re-connect
The reason why the client dropped the line is nothing more than two, the client network hangs, the server hangs. The client network is also divided into two types, one is the network can be perceived by the computer disconnected, the other is the local network is good, but the Internet connection is different, corresponding to the Android API, is Networkinfo isavailable and isconnected. Of course, the isconnected of this place is not necessarily reliable, because it is determined by a server, that server who knows there is no problem.
After you drop the line, choose a different re-connect interval depending on the state. If it is a local network error, do not need to periodically re-connect, then only need to listen to network status, wait until the network recovery after the re-connect. If the network changes very frequently, especially when the App is running in the background, for the re-connection can also be added to a certain frequency control, while guaranteeing a certain message real-time, while avoiding excessive power consumption.
If the line is dropped because the network is not connected to the Internet, or the server is hung, the choice of the re-connect interval is very important.
First, if the program is in the foreground, the user is using our App, the re-connect interval should be more frequent, so that the user feedback more timely, if the program is running in the background, in order to save power, you can properly extend the re-connect interval.
Secondly, with the increase of the number of re-connection, the possibility of the server recovering in a short time is gradually reduced, and the re-connection interval should be extended (multiple growth). However, you should set a maximum re-connect interval, which no longer increases when the maximum interval is reached.
Thirdly, the increase of the re-connect interval should not be fixed, but should be increased by a random backoff strategy. In case of a server outage caused by the drop line, all clients of the reconnection point of time is the same, when the server is restored, the same point in time all clients connect to the server, causing the server to be overwhelmed, and then down again. For a live example, please refer to the ring letter last year's outage time.
Summing up, the re-connect interval can be expressed as follows:
Multimedia Data Management
Another play in IM systems is multimedia data. Because the mobile network is slow and the traffic is expensive, there must be some processing on the mobile side for these problems. When uploading, minimize the upload time, when downloading, can let the user see the content as soon as possible. At the same time, as far as possible to save traffic, reduce unnecessary traffic consumption.
Text messages can be transmitted directly over long connections because they are small. However, for multimedia files, transmission through long connections is inappropriate, long-connected servers will not be targeted for large file transfer optimization, a large number of multimedia file data will directly preempt other signaling messages and text messages of the loan resources. As a result, multimedia messages are accessed through additional channels to a dedicated file server.
When downloading, for different network environment, can adopt different prefetching policy. In the WiFi environment, because there is no need to consider traffic problems, after receiving the message, we can immediately download the included multimedia files. In a mobile network, you should wait until the user actually sees the multimedia message before downloading it.
Image sheet
Upload, now mobile phone camera pixel at random, a picture casually on several m, however, through the IM software transmission of pictures, usually for quality requirements are not too high, if we directly upload several m pictures directly, often laborious and not pleasing. Before uploading, the image pixels are lowered and compressed, which can significantly reduce the time to upload to the chrysanthemum and reduce user traffic consumption. If the user does require a picture quality, an original image option is provided.
If using HTTP upload, the large file will be divided into multiple data block upload, the previous data block transmission succeeds, and then transfer the next. When the line is re-transmitted, the data block is also used as the minimum weight flyer element. Data block sizes are different for different network types. In a better network (WIFI/4G/3G), the data block can be relatively large, which can reduce the interaction time, speed up the transmission of familiar, and in the weak network environment, the data block should be set relatively small, in order to reduce the probability of transmission failure, reduce retransmission traffic consumption.
Another optimization technique for uploading using HTTP is using pipeline. When pipeline is not used, uploading a block of data requires that the previous data block be successfully transmitted, and the data channel is simplex. Using pipeline, you can change the data channel to duplex, and once the data block is transferred, it is not necessary to wait for the package to upload the next packet directly, which can save the data packet delay.
When downloading, in the message display area is usually only a small picture, this time only need to download the corresponding size of the thumbnail, no need to download the original image. Even, here can be smaller than the thumbnail image binary data directly into the message body issued, and display to the user A Gaussian blur, in the case of minimum availability, reduce user wait time, improve the user experience.
Pronunciation
For different network environments, the use of different quality of speech coding algorithm, in a better network, the use of high-quality voice, and in the weak network environment, using lower quality, priority to ensure the availability of.
In order to reduce user wait time, we can also adopt the strategy of edge recording and passing. Because the recording time is longer, in the process of recording, we can first upload the recorded parts to the server, until the recording is complete, only need to upload the last packet, and inform the server recording complete can be, basically to finish the recording is finished, without waiting.
Voice messages do not have thumbnails, so voice download is basically only real deal the original file to download.
The above is the NetEase cloud letter for Android Instant Messaging Development Summary, welcome you to actively ask questions and discuss together.
Summary of Android Instant Messaging development (II.)