Article source: blog.csdn.net/clh604/arti…
It is very common to push messages on the Internet, especially on the mobile Internet. Mobile phones can receive a lot of push messages every day. Through research, it is found that the principle of these push services is to maintain a long connection (or it is impossible to achieve real-time effect), but ordinary socket connection consumes too much on the server. Hence the emergence of lightweight, low-consumption protocols like MQTT to maintain long connections. How to maintain long connections:
\
Before we write, why does Android need a heartbeat mechanism to maintain a long connection? First, we know that the heartbeat mechanism is needed to maintain any long connection
The server sends a heartbeat reply to the client, thus forming a complete handshake for the client server. This handshake is to let both parties know that the connection between them is not broken and the client is online
. If a time threshold is exceeded and the client does not receive a response from the server, or the server does not receive a heartbeat from the client, then it is for the client to disconnect from the server and re-establish a connection
Connection, just disconnect the connection for the server. So what is the different purpose of a long connection heartbeat on a smartphone from a long connection heartbeat on the Internet? The reason lies in the use of smart phones
Mobile wireless networks, so before we talk about long connections we need to understand the characteristics of wireless mobile networks.
1. Features of wireless mobile network:
When a smart phone is connected to the mobile network, it is not really connected to the Internet. The IP assigned to the mobile phone by the carrier is actually the internal IP of the carrier. To connect to the Internet, the mobile phone terminal must pass the operation
The gateway of the business translates IP addresses, which is referred to as NetWork Address Translation (NAT). Simply speaking, the connection between mobile terminals and the Internet is actually between mobile Intranet IP, port and external IP
Map to each other. It’s like making a hole in the wall of the mobile wireless network and connecting it to the Internet. Schematic diagram is as follows :(source network)
\
\
GGSN(GateWay GPRS Support Note GateWay GPRS Support Node) module realizes the NAT function, because most mobile wireless network operators in order to reduce the load of GateWay NAT mapping table, such as
If a link has no communication for a period of time, it will delete its corresponding table, resulting in link interruption, it is this deliberately shortened idle connection release timeout, originally wanted to save channel resources, did not expect to let the Internet
Applications must not send heartbeats much higher than normal to maintain pushed long connections. This is also why there was a previous signaling storm, wechat lottery rumors, because such applications send heartbeat frequency is very short,
It not only causes the waste of channel resources, but also causes the rapid consumption of mobile phone electricity.
2. What are the differences between android system push and IOS push:
First we must know that all push functions must have a long connection between the client and the server, because push is sent by the server to the client actively, if there is no one between the client and the server
A long connection then the server is unable to actively connect to the client. Therefore, the push function is based on the long connection.
IOS long links are maintained by the system, that is, Apple’S IOS system maintains a long link between the client and the Apple server at the system level. All apps on IOS push messages first
Go to Apple’s server and push apple’s server to the mobile terminal through this system-level long link, which has the following advantages: 1. Always maintain a long connection in the mobile terminal, and because
The long link is system level and will not be killed and cannot be pushed. 2. Save power, no longer each application maintains its own long connection. 3. Security, only registered with Apple developers
Enough to push, and so on.
Android’s long connection is maintained by each application, but Google also launched a push framework similar to Apple’s technical architecture, C2DM, cloud push function, but because Google’s server is not in it
Inside the country. You know the rest. As a result, the push doesn’t work, and Android developers have to maintain a long link themselves, so each app, if available 24 hours a day, has its own version
Protecting a long connection, this kind of power and flow consumption is imaginable. Although various push platforms have emerged in China, none of them can reach the level of consumption of maintaining only a long connection.
3. Implementation of push:
One: the client constantly queries the server to retrieve new content, which is called pull or polling
Two: A TCP/IP connection is maintained between the client and the server, and the server pushes the client
3. When the server receives new content, it sends a signaling message similar to a short message to the client. After receiving the message, the client downloads the new content from the server, that is, SMS push
Apple’s push system and googleC2DM push system actually maintain a TCP/IP long connection at the system level, which is based on the second way to push. The third way because the operator did not open for free
This signaling makes this push cost unacceptable, even though it is very stable, efficient and timely.
If you want to learn about the various push methods in Android, please refer to this link: Android implementation push method solution this blog has been introduced very well.
\