Smack message extension, adding custom elements (tags) experience sharing

Source: Internet
Author: User

The smack framework encapsulates the XMPP protocol to facilitate interaction with the OpenFire Instant Messaging server. Plainly, the smack framework can construct an XML string that conforms to the XMPP protocol through objects, avoiding the manual concatenation of strings.

The basic XML structure of the XMPP protocol is as follows:

1 <message2      from= ' Sender Jid '3  to= ' Receiving Party Jid '4 type= ' message type (normal message/group chat) '>5   <Body>Message content</Body>6 </message>

In most cases, such a simple structure would not satisfy the requirements, and we might try to add child elements under the message element to describe more information.

For example, in addition to the sender's Jid, we would like to take the sender's nickname and Avatar URL directly, so as to avoid repeatedly querying these basic information from the database. But this seemingly simple process, implemented in smack, is rather cryptic, and is then directly explained by the code.

Send Message Basic flow

1 //Build Chat2Chat chat = chatmanager.createchat ("Opposite Party Jid");3 4 //Build Extension5Userinfo4xmpp USERINFO4XMPP =Newuserinfo4xmpp ();6Userinfo4xmpp.setnametext ("rookie"));7Userinfo4xmpp.seturltext ("http://www.liaoku.org/");8 9 //Build MessageTenMessage message =NewMessage (); OneMessage.setbody ("Hello hi");//Message Content AMessage.addextension (USERINFO4XMPP);//Add extension content -  - //Send theChat.sendmessage (message);

USERINFO4XMPP definition

1 Importorg.jivesoftware.smack.packet.ExtensionElement;2 3 /**4 * Extended User information for message5  * @authorYang Yuan (reprint does not specify the source shameful, the original search [Yang Yuan blog])6  * @versioncreated: May 11, 2015 PM 4:55:387  */8  Public classUserinfo4xmppImplementsextensionelement{9     Ten     //User Information element name One     PrivateString elementname = "UserInfo"; A     //User Nickname element name -     PrivateString nameelement = "Name"; -     //user Picture Address element name the     PrivateString urlelement = "url"; -     //User Nickname element text (open to the outside) -     PrivateString Nametext = ""; -     //user Picture Address element text (open to the outside) +     PrivateString Urltext = ""; -      + @Override A      PublicString Getelementname () { at         returnelementname; -     } -      -     /** - * Returns the extended XML string - * This string is used as a child element of the message element in      */ - @Override to      Publiccharsequence ToXML () { +StringBuilder SB =NewStringBuilder (); -          theSb.append ("<"); * sb.append (elementname); $Sb.append (">");Panax Notoginseng          -Sb.append ("<"); the sb.append (nameelement); +Sb.append (">"); A sb.append (nametext); theSb.append ("</"); + sb.append (nameelement); -Sb.append (">"); $              $Sb.append ("<"); - sb.append (urlelement); -Sb.append (">"); the sb.append (urltext); -Sb.append ("</");Wuyi sb.append (urlelement); theSb.append (">"); -          WuSb.append ("</"); - sb.append (elementname); AboutSb.append (">"); $          -         returnsb.tostring (); -     } -  A     /** + * can be ignored the      */ - @Override $      PublicString GetNamespace () { the         return""; the     } the      the      PublicString Getnametext () { -         returnNametext; in     } the  the      Public voidSetnametext (String nametext) { About          This. Nametext =Nametext; the     } the  the      PublicString Geturltext () { +         returnUrltext; -     } the Bayi      Public voidSeturltext (String urltext) { the          This. Urltext =Urltext; the     } -}

In a nutshell, the key is to implement the Extensionelement interface, and then implement your own ToXml method, which returns the XML string that will be extended as a child of the message element.

The generated message XML structure

1 <messageID= ' 76ws9-11 '>2     <Body>Hello, hi.</Body>3     <UserInfo>4         <name>Rookie</name>5         <URL>http://www.liaoku.org/</URL>6     </UserInfo>7 </message>

This is the basic process, and I hope to help the reader.

Smack message extension, adding custom elements (tags) experience sharing

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.