Simple implementation and analysis of androidpn

Source: Internet
Author: User

(Some content in this article comes from the Network)

XMPP protocol:

XMPP: The extensible messaging andpresence protocol.

Full name: extensible communication and representation protocols.

Introduction: scalable communication and express protocol (XMPP) can be used for XML data element stream transmission in real-time communication, presentation, and demand response services of service classes.

XMPP is a scalable markup language (XML)-based protocol for instant messaging (IM) and online detection.

It facilitates quasi-real-time operations between servers. This protocol may eventually allow Internet users to send instant messages to anyone else on the Internet, that is

Make the operating system different from the browser.

 

XMPP has the following advantages:

1. Distributed anyone can run their own XMPP server. It has no master server and the client is simple

2. High security. Reliable security using SASL, TLS, and other technologies

3. Open source, easy to learn and understand

4. There is no doubt that cross-platform data is transmitted in XML format.

 

Androidpn:

Androidpn: Android pushnotification (Chinese name: Android message push ).

Java Development Based on XMPP has an open-source framework, smack, which encapsulates some XMPP implementations. Based on the reference of smack

Achieve persistent connection with the server to push the server to the client, that is, androidpn. Androidpn integrates smack on the client.

In this way, you can easily establish a socket connection with the server based on the XMPP protocol. Therefore, androidpn uses the smack framework.

.

 

Androidpn benefits:

The advantages of using androidpn include the following: Using fully open XMPP protocol for data transmission (QQ, MSN, Gtalk, etc. are all using this Protocol ).

Good framework support (smack, a push framework dedicated for Android, and Mina, a framework for socket management, are mature products );

Fully open source code (we can modify it on the basis of androidpn to meet any of our needs), greatly reducing the client generation

Code, reducing the development difficulty of Android.

 

Androidpn technology implementation:

(I won't talk about how to download the source code here. There are many online tutorials)

The client establishes a socket connection with the server.

AndroidpnClientTo manage the connection information, such as XMPP port, IP (domain name), login username and password, and connection maintenance (timed

Send connection requests to keep the connection intact ). The communication between the server and the client is based on a session.

Specify the server port number, and then send the information to the server. Send a message to the server: <stream> the root node method is used for transmission.

When the server and client are closed, the end mark of the server is sent </stream>. The client only uses the XMPP protocol to receive messages, while all

Other operations are handed over to the server, such as management connections and message storage, which greatly reduces the burden on the client.

 

 

AndroidpnServerAndroidpn mainly uses spring and hibernate technologies. Saves user information to the database for verification.

Displays the user status and sent information. When the server is closed, a </stream> flag is sent to the client to notify the connection to be disconnected.

New connection. When managing socket connections, androidpn uses the Mina framework for management. The advantage of Mina is that it changes our traditional method of managing sockets. For example, each socket is set up to open a thread, while Mina

Multiple Threads can be implemented to manage n multiple users (this sentence is not too clear,

Who knows to leave a comment? Thank you very much ). It is undoubtedly of great benefit to handle highly concurrent pushing.

 

During data transmission, information processing uses XML nodes to transmit information. Message encapsulation typically uses IQ (info/query), message (Message ),

Presence (online status detection ).

However, it seems that the message (maybe I understand the error) is not used in androippn, and IQ encapsulation is used from server to client and client to server.

In addition, there are four types of XML Transfer Process: Set, get, result, and error. In my personal understanding, set indicates request Update (Change) processing.

(For example, pushing messages from the server to the client and registering users), get indicates getting data (for example, getting a friend list), and result indicates the response sent to the client after the request is updated.

(If the client is successfully logged on, see the createsession () method in clientsession.) The error indicates an error and indicates that the client is disconnected.

Connection.

Persistent connection and reconnection:

1. The first time the client connects to the server, a socket connection is established. If the server needs to be closed, the system returns </stream> or an error

The stream is returned to the client. The client starts the timed reconnection thread.

2. After the local connection to the server fails, the timed reconnection thread will be started.

3. Local listening network information. If the network is unavailable, the reconnection thread will also be started.

4. After the connection is successful, listen to the socket connection status. If disconnected, start the reconnection thread.

 

Note: 1) It is said that the firewall may close the inactive connection, so it is said that the heartbeat (keeplive) will be sent to the server after the connection is established ).

 

 

Main file functions:

Client:

Demoappactivity

Servicemanager Service Manager, which controls the start and stop of background services

Icationicationservice initializes the broadcast and registers the broadcast. The initialization thread sends and starts to connect to broadcast and other

Xmppmanager defines how to call the asmack library for connection and other operations.

Connectivityreceiver connects to the broadcast receiver and calls the connection method of xmppmanager.

Persistentconnectionlistener re-connection listener, called by asmack, registered during connection

Phonestatechangelistener listens on the mobile phone connection status and is registered when the service is started.

Icationicationiqprovider notifies the message provider, called by the asmack library, and receives the message (XML) returned by the server)

Icationicationpacketlistener notifies the message listener, which is registered after connection and called by the asmack library. When icationiqiqprovider receives

After a message arrives at the server, the listener's callback method processpacket will be called, and then broadcast the message to icationicationreceiver.

Icationicationreceiver notifies the broadcast receiver and calls the notification method in notifier.

Notifier calls the notify method interface notification.

 

Note: The parseiq () method in icationicationiqprovider parses XML data sent by the server and encapsulates the data into entities.

 

Server:

The Org. androidpn. server. XMPP package defines some exception types, mainly including the entry class xmppserver, which is used to start and stop the server program.

The Org. androidpn. server. XMPP. Auth package contains some Authentication Classes. Our own authentication module can be used with androidpn here.

Org. androidpn. server. XMPP. codec is an XML file parsing package of XMPP protocol. The server uses this package to encode and decode messages received and sent by XMPP.

Org. androidpn. server. XMPP. the handler package mainly processes messages. We can define our own handler for different message types. This package can process the data sent by the client in the class.

The org.androidpn.server.xmpp.net package maintains persistent connections with the client, and implements some transmission methods for sending XMPP messages.

Org. androidpn. server. XMPP. presence contains only the presencemanager class to maintain the online status of the client.

Org. androidpn. server. XMPP. Push contains only one icationicationmanager class that contains an interface for sending messages to the client. You can encapsulate the required data and send it to the client.

The Org. androidpn. server. XMPP. session package defines the session used to represent persistent links. Each session contains the status information of a connection. The user manages all sessions, such as disconnected connections, deleting sessions, establishing connections, and adding sessions.

Org. androidpn. server. XMPP. SSL is a toolkit for SSL authentication on connections.

 

Note: In the above red packet, icationicationmanager (Org. androidpn. server. XMPP. Push) carries username and message

Find the corresponding session in the Session (Org. androidpn. server. XMPP. Session) class, encapsulate the session into XML format, and then call

The deliver () method in connection (org.androidpn.server.xmpp.net) sends data to the corresponding user for communication.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.