Connect to the following article:
(a) Client and server connection established
Previous write to Clientsession createclientsession here, create a client session. After the session is created in the SessionManager class, the text of the two XML content is stitched together. One is build the start packet response, creating a headline package as a response. The other is: XMPP 1.0 needs stream features, which is the file structure required by xmpp1.0. The format of the two messages is as follows:
<? XML version= ' 1.0 ' encoding= ' UTF-8 ' ?> < xmlns:stream= "Http://etherx.jabber.org/streams" xmlns= "Jabber:client" from= "127.0.0.1"
ID= "bdef9c6a" xml:lang= "en" version= "1.0">
<Stream:features><STARTTLSxmlns= "Urn:ietf:params:xml:ns:xmpp-tls"></STARTTLS><Authxmlns= "Http://jabber.org/features/iq-auth"/><Registerxmlns= "Http://jabber.org/features/iq-register"/></Stream:features>
Then, call connection's Deliverrawtext method, transfer these two XML content through Iosession writer method, to Mina inside. Specific Mina how to deal with, I have not studied.
By this, this message has been published according to the log log of the record. The process of establishing a connection between the client and the server has been completed. This is only part of the message about the client to send, this part of the content should be a lot.
(ii) Server push message
When a message is pushed from the server, the methods inside the Notificationmanager class are called, broadcast and sent to individual users, respectively. The main view here is radio.
The first is to construct the IQ message body, Createnotificationiq method to construct.
/*** Creates a new notification IQ and returns it. * Construct message formatting*/ PrivateIQ Createnotificationiq (String ApiKey, string title, String message, string uri) {random random
=NewRandom (); String ID=integer.tohexstring (Random.nextint ()); //String id = string.valueof (system.currenttimemillis ());Element Notification=documenthelper.createelement (Qname.get ("Notification", Notification_namespace)); Notification.addelement ("id"). SetText (ID); Notification.addelement ("ApiKey"). SetText (ApiKey); Notification.addelement ("Title"). SetText (title); Notification.addelement ("Message"). SetText (message); Notification.addelement ("uri"). SetText (URI); IQ IQ=NewIQ (); Iq.settype (IQ. Type.set); Iq.setchildelement (notification); returnIQ; }
Post-constructed XML:
<IQtype= "Set"ID= "860-0" to= "[Email protected]/androidpnclient"> <Notificationxmlns= "Androidpn:iq:notification"> <ID>11218d6c</ID> <ApiKey>1234567890</ApiKey> <title>How are you doing</title> <message>Oh, hello.</message> <URI></URI> </Notification></IQ>
Finds the session of the specified user name in Clientsession, and sends this IQ message if one exists. Call Connectin's deliver method, transfer the XML information to Mina through Iosession.write (buffer), and send the record plus 1.
Client Source Analysis:
The client code is simple and relies on Xmppmanager to maintain the connection. Let's talk about the process here.
When the client pushes the message over, the OnReceive method of the Notificationreceiver class receives the message, and here, at this time, has obtained all the data sent. Here, you can already do your own thing, because you already have the data you need. But it seems that a lot of people like to Notifier in the Notify method to deal with.
The rest is your business.
There is also a user name and password for the client, which is automatically generated by default, and can be specified automatically. Can be modified in Xmppmanager's Run method. After the modification, there will be some problems, that is, the server-side registration, there will be a user name already exists, the problem of repeated insertions. This need to insert data in the server side of the time to modify the code, modify in Userserviceimpl, in order to business needs, I have hibernate modified in order to MyBatis, so the method slightly different:
PublicUser saveuser (user user)throwsuserexistsexception {Try { //Modify the login for your own users Try{User=Getuserbyusername (User.getusername ()); } Catch(usernotfoundexception e) {//TODO auto-generated Catch blockLog.info (User.getusername () + "isn't exist in db ...."); Userdao.saveuser (user); E.printstacktrace (); } } Catch(dataintegrityviolationexception e) {e.printstacktrace (); Log.warn (E.getmessage ()); Throw NewUserexistsexception ("User" +User.getusername ()+ "' Already exists!"); } Catch(Entityexistsexception e) {//needed for JPAE.printstacktrace (); Log.warn (E.getmessage ()); Throw NewUserexistsexception ("User" +User.getusername ()+ "' Already exists!"); } returnuser; }
That's all you can do.
On the short-term re-connected, there are many solutions on the Internet, the client plus a line of code:
Private voidAddTask (Runnable Runnable) {log.d (LogTag,"AddTask (runnable) ..."); Tasktracker.increase (); synchronized(taskList) {if(Tasklist.isempty () &&!running) {Running=true; Futuretask=Tasksubmitter.submit (runnable); if(Futuretask = =NULL) {tasktracker.decrease (); } } Else { //client cannot successfully connect to ANDROIDPN server after resolving server end multiplicityRunTask (); Tasklist.add (runnable); }} log.d (LogTag,"AddTask (runnable) ... done"); }
Of course, there are many other details, first here, thank the online so many bloggers write some information.