Before I talk about XMPP and Asmck, I'm going to share some resource documentation, and if you're patient, you can just ignore what I've written below. The following introduction to XMPP is mostly excerpts from the online documentation, and later I will write some chat room development based on XMPP protocol and ASMCK Open Source Library, features include, text, voice, and current location address.
XMPP official website : http://xmpp.org
Smack Development Official Guidance Document : http://www.igniterealtime.org/builds/smack/dailybuilds/documentation/extensions/index.html
Smack Development API documentation : http://www.igniterealtime.org/builds/smack/dailybuilds/javadoc/
XMPP Chinese Translation Program : http://wiki.jabbercn.org/Home
One, what is XMPP
XMPP (extensible Messaging and Presence Protocol), extensible messaging and storage protocol, is one of the mainstream IM protocols, it is a protocol based on XML transmission, we all know that XML extensibility is relatively strong, XMPP uses XML for transmission, allowing us to use XMPP to send extended messages to meet more of our needs.
XMPP contains two parts of the definition: one is the core XML Stream transport protocol, which enables XMPP to be implemented on a platform that is more canonical than the previous network communication protocol, and the second is based on
An Instant Messaging extension application for XML stream transmission.
Three roles are defined in XMPP: client, server, gateway. Communication can occur bidirectional between any two of these three. The server also assumes the client information record, connection management and information routing function. The gateway undertakes the interconnection with the heterogeneous instant communication system, and the heterogeneous system can include SMS (SMS), MSN,ICQ and so on. The basic network form is that a single client connects to a single server over TCP/IP and then transmits the XML over it.
The basic network structure of XMPP is as follows:
C1----S1---S2---C3
|
C2----+--G1===FN1===FC1
Symbolic representation: C1,C2,C3=XMPP client; S1,S2=XMPP; server-side g1= a network that is converted between XMPP and protocols that use an external message network (non-XMPP); fn1= external Message network; fc1= the client for the external messaging network.
(1) Server
The service system is the Intelligent abstraction layer of XMPP communication, which is mainly responsible for managing the session connection from other individuals or the XML stream (streams) and the authentication sent from the client, server and other individuals to the appropriate XML address node in the XML stream entity. Most XMPP services allow for the storage of some client data (such as a contact list), in which case the service processes XML data directly to the client, rather than to other individuals.
(2) Client
Most clients are directly connected via TCP, and using XMPP to get the server provides all-12-2nd Chapter Jabber Protocol principles and their application functions and other services. Many resources that are authenticated by the client may be connected to the server at the same time, and each resource defined at the XMPP address is different (such as <[email protected]/home> and <[email protected]/work>) The recommended server and client side is 5222.
(3) Gateway
Its main function is to convert the XMPP protocol into the protocol used by the external message (NON-XMPP) system, and also to convert the returned data to XMPP. These communications are based on gateways and servers, based on gateways and external messaging systems.
XMPP Message Format
The JABBER/XMPP system uses XML streams to transfer data between different entities. During a two-entity connection, the XML stream is transferred from one entity to another. Between entities, there are three top-level XML elements:
<message/>,<presence/> and <iq/>. Each one contains attributes and child nodes. These elements are described separately below.
1. Message element:
One of the most basic functions of an instant messaging system is the ability to exchange messages in real time between two users,<message/> elements provide this functionality. Each message has one or more properties and child elements. The attributes "from" and "to" respectively represent the address of the sender and receiver of the message. <message/> can also contain a "type" attribute, which gives the receiver a hint as to what the message is. Table 3-1 shows the possible values for the "type" attribute. <message/> can also include the "id" attribute, which uniquely identifies a response to an output message.
2. Status (presence) elements:
The <presence/> element is used to convey the perceived information of a user's presence state. The user can be "available", or "unavailable", "Hide" and so on. When a user connects to an Instant Messaging server, the message sent to him by the friend is immediately delivered. If the user is not connected to the server, messages sent to him by the friend will be stored by the server until the user connects to the server. Users control availability through Instant Messaging clients themselves. However, if the user disconnects from the server, the server sends a message to the user who has subscribed to the user's presence to notify them that the user is unavailable. <presence/> also contains two child elements:<show/> and <status/>. <status> contains a text description of <show/>.
3. IQ (info<query) elements
The IQ element is the third top-level element of the JABBER/XMPP message protocol. IQ stands for "Info/query", which is used to send and retrieve information between entities. IQ messages are exchanged between entities through the request/response mechanism. IQ elements are used for different purposes, and they are differentiated by different namespaces. There are many namespaces in the JABBER/XMPP message protocol, but the most commonly used namespaces are: "Jabber:iq:register", "Jabber:iq:auth", "Jabber:iq:roster".
The three top-level nodes of the Jabber protocol are described above. In this format, Jabber messages can be not only simple text, but also can carry complex data and files in various formats, that is to say, Jabber can not only be used in communication between people, but also to achieve software and software or software and human communication. This feature of Jabber greatly expands the scope of application of instant communication.
XMPP working mechanism
For example: A terminal name is Remeo, connected to other users through the server Montague.lit, other users through the server [email protected] to establish a connection.
The first step Romeo began to contact the server Montague.lit by sending a stream packet, which included authentication, login, and so on.
Step Two Romeo obtained his contact list by sending the <iq/> field request to the server Montague.lit by following the protocol below.
<iq type= ' get ' from= ' [email protected]/pda ' >
<query xmlns= ' Jabber:iq:roster '/>
</iq>
<iq type= ' result ' to= ' [email protected]/pda ' >
<query xmlns= ' Jabber:iq:roster ' >
<item jid= ' [email protected] '/><item jid= ' [email protected] '/>
<item jid= ' [email protected] '/>
</query>
</iq>
Step three Romeo sends a presence request to the server Montague.lit, and then broadcasts this information to all of his friends.
<presence from= ' [email protected]/pda '/>
<presence from= ' [email protected]/pda ' to= ' [email protected] '/>
<presence from= ' [email protected]/pda ' to= ' [email protected] '
/>
<presence from= ' [email protected]/pda ' to= ' [email protected] '
/>
Fourth Step if Romeo's contacts are online, they will presence back to Romeo, informing them of their status.
<presence to= ' [email protected]/pda ' from= ' [email protected]/balcony '/>
<presence to= ' [email protected]/pda ' from= ' [email protected]/chamber '/>
<presence to= ' [email protected]/pda ' from= ' [email protected]/home '/>
Fifth Step Romeo and Juliet start exchanging information and chatting.
<message from= ' [email protected]/pda ' to= ' [email protected] ' >
<body>hi!</body>
</message>
<message from= ' [email protected]/balcony ' to= ' [email protected]/pda '
>
<body>hi yourself!</body>
</message>
From a document standpoint, all the XML text sent by the client or server concatenating together, from <stream> to </stream> to form a complete XML document. The stream tag is called the XML stream. The <message>...</message> such XML elements in <stream> and </stream> are called XML Stanza (XML stanzas). The basic mode of the XMPP core protocol communication is to build a stream first, then negotiate a bunch of security and other things, the intermediate communication process is the client sends XML Stanza, one by one. The server sends XML Stanza to the client based on the information sent by the client and the logic of the program. But this process is not a question and answer, and it is possible to send letters from one party to another at any time. The final phase of communication is the </stream> shutdown stream, which closes the TCP/IP connection.
Two, what is Asmack
Asmack is actually an implementation version of Smack on the Android platform, Smack is an open source API library that implements the XMPP protocol,
Asmack GitHub Source Address: Https://github.com/Flowdalic/asmack
Due to the company's project needs, I in the official version of the Asmack, based on the revision of some source code, so that it can be very quickly put into project use.
Modified Source Address:
https://github.com/UFreedom/asmack-android-8-source-4.0.2
The development of Android Instant Messaging XMPP (i) initial knowledge of XMPP protocol and Asmack