XMPPIntroduction to the agreement
The XMPP protocol (extensible Messaging and PRESENCEPROTOCOL, Extensible Message Processing field protocol) is an XML-based protocol that is designed to address the standards of timely communication and is first implemented in Jabber. It inherits the flexibility of development in the XML environment. As a result, XMPP-based applications have super-scalable capability. and XML is easy to pass through firewalls, so applications built with XMPP are not susceptible to firewalls. Using XMPP as a universal transmission mechanism, different applications in different organizations can communicate effectively.
This article has a basic introduction, http://blog.csdn.net/xutaozero21/article/details/4873439
IM
Instant Messenger, timely communication software, is the use of QQ, MSN Messenger and Gtalk and so on. Where Gtalk is based on an implementation of the XMPP protocol, others are not. Im almost always used as a tool for every surfer, there are some enterprise-level IM applications in large enterprises abroad, but their commercial value has not been fully utilized. Imagine that since the XMPP protocol is an open protocol, each enterprise can use it to develop the IM that suits its business, improve its productivity, and even, you can integrate this communication software in the network game, not only lets you chat while playing the game, but also can develop an IM application suitable for the game itself. For example, some game key scene alert function, team voice communication and so on can be based on IM.
Using OpenFire, smack, and spark to complete the XMPP IM implementation, here's a brief introduction to these 3 roles:
OpenFire is primarily a server that manages communication connections to clients and provides some communication and connection information for clients.
Smack is mainly the implementation of the XMPP protocol, provides a good set of APIs, so the following operation XMPP is done by using the Smack API, of course, because it is on Android, so the use of asmack this package, the method is similar to the smack package.
Spark is an IM client implementation, which is actually implemented using the Smack API.
Asmack use
First, connect the server
Connectionconfiguration configuration = new Connectionconfiguration ( , 5222 Span style= "COLOR:RGB (106, 135, 89); > "" ) //settings do not require SAS validation
configuration.setsaslauthenticationenabled (;
disabled )
second, the registration module
There are two ways to register:
One is to use CreateAccount, but I tested the discovery cannot create the user, the specific reason is unknown, the following describes the second.
accountmanager ? ?Accountmanager? ? = conn. Getaccountmanager ();
Try {
??Accountmanager??. CreateAccount ("Saliya", "123456");
} catch (Xmppexception e) {
E.printstacktrace ();
}
One is through the registration class
??Registration? ? reg = new ? ?Registration??();
//set type
Reg.settype (IQ. Type.SET);
Send to where
Reg.setto (Conn. Getservicename ());
//Set user name
Reg.setusername ("Keven1");
//Set Password
Reg.setpassword ("123456");
//Set the remaining properties (optional)
Reg.addattribute ("Android", "Geolo_createuser_android");
//Create a packet filter
Packetfilter filter =NewAndfilter (NewPacketidfilter (Reg.getpacketid ()), NewPackettypefilter (IQ.class));
//Create a package collector
Packetcollector collector =Conn. Createpacketcollector (Filter);
//Send Package
Conn. Sendpacket (REG);
//Get return information
IQ result = (IQ) collector.nextresult (smackconfiguration.Getpacketreplytimeout());
//Cancel Collection
Collector.cancel ();
//Judging by return information
if(Result = =NULL) {
Toast.Maketext(Getapplicationcontext (),"Server Exception",Toast.Length_short). Show ();
}else if(Result.gettype () = = IQ. Type.ERROR) {
if(Result.geterror (). toString (). Equalsignorecase ("Conflict (409)")) {
Toast.Maketext(Getapplicationcontext (),"Registration failed, user already exists", Toast.Length_short). Show ();
}Else{
Toast.Maketext(Getapplicationcontext (), "Registration Failed", Toast.Length_short). Show ();
}
}else if(Result.gettype () = = IQ. Type.RESULT) {
Toast.Maketext(Getapplicationcontext (), "Registered successfully", Toast.Length_short). Show ();
}
Third, login
1. Login
Conn.login ("Kooeasy", "********");
2. Get Contacts
Conn. Getroster (); Access to the Roster
collection<rosterentry> entries = roster.getentries (); Get Contact person
third, send the message
//Create Chat manager
Chatmanager Chatmanager = Conn.getchatmanager ();
//Create a chat: (user, chat listening) and then this Add chat listener can not listen to the bug?
Chat msg = chatmanager.createchat (user, NULL);
//Added chat listener to chat manager
Chatmanager.addchatlistener (New Chatmanagerlistener () {
@Override
public void chatcreated (chat chat, Boolean b) {
//To chat, add monitoring
Chat.addmessagelistener (New MessageListener () {
@Override
public void ProcessMessage (chat chat, message message) {
LOG.I ("MSG", Message.getbody ());
}
});
}
});
try {
//start sending messages
Msg.sendmessage ("Hello, can you get it?" ");
} catch (Xmppexception e) {
E.printstacktrace ();
}
More information about the introduction will be later modified with smack 4.0
Android Asmark Usage Introduction