Extended Smack Message

Source: Internet
Author: User
Tags extend

XMPP is a subset of XML based on standard Common Markup Language protocol, which inherits the flexible development in XML environment.

Smack is an open-source, Java-written XMPP (jabber) client code base

Because XMPP is XML, it is convenient to extend it. The smack also provides support for the message extension.

Smack requires two interfaces for message extensions

Org.jivesoftware.smack.packet.PacketExtension
Org.jivesoftware.smack.provider.PacketExtensionProvider

Suppose we need to extend a message receipt, we need to define a received node in the message:

Package Org.jivesoftware.smack.packet;import Org.jivesoftware.smack.packet.packetextension;import Org.jivesoftware.smack.util.stringutils;public class Received implements Packetextension {public static final String NAME = "Received";p ublic static final String name_space = "urn:xmpp:receipts";p rivate String id = "";p rivate Integer Statu s = 0;/** * @return The ID */public String getId () {return ID;} /** * @param id The ID to set */public void SetId (String id) {this.id = ID;} /** * @return The status */public Integer getStatus () {return status;} /** * @param status The status to set */public void SetStatus (Integer status) {this.status = status;} @Overridepublic String Getelementname () {return "received";} @Overridepublic String GetNamespace () {return "urn:xmpp:receipts";} @Overridepublic String ToXML () {StringBuilder buffer = new StringBuilder (); Buffer.append ("<received xmlns=\" urn: Xmpp:receipts\ ""); Buffer.append ("id=\"). Append (Stringutils.escapeforxml (ID)). Append ("\" "); Buffer.append (" StaTus=\ ""). Append (Status). Append ("\" "); Buffer.append ("/> "); return buffer.tostring ();}} 

  

Then we need to define a received parser.

Package Org.jivesoftware.smack.provider;import Org.jivesoftware.smack.packet.packetextension;import Org.jivesoftware.smack.packet.received;import Org.xmlpull.v1.xmlpullparser;public class ReceivedProvider Implements Packetextensionprovider {@Overridepublic packetextension parseextension (Xmlpullparser parser) throws Exception {Boolean done = false; Received Received = new Received (); while (!done) {int eventtype = Parser.next (); String name = Parser.getname ();//xml tab Label if (EventType = = Xmlpullparser.start_tag) {if (Name.equals ("id")) { Received.setid (Parser.nexttext ());} if (Name.equals ("status")) {Received.setstatus (Integer.parseint (Parser.nexttext (). Trim ()));}} if (EventType = = Xmlpullparser.end_tag) {if (Received.NAME.equals (NAME)) {done = true;}}} return received;}}

 

We need to add the extended provider to the Providermanager when we use smack.

Providermanager.getinstance (). Addextensionprovider (Received.name, Received.name_space, New ReceivedProvider ());

 

We want message to add received

Message message = new Message (); Message.setto ("to");//target Message.setfrom ("from");//source Received received = new received () ; Received.setid ("cjq74-0");//Receipt Message idreceived.setstatus (1);//Receipt Status Message.addextension (Received); System.out.println (Message.toxml ());//View XML

  

Format of XML

<message id= "Cjq74-1" to= "to" from= "from" ><received xmlns= "urn:xmpp:receipts" id= "cjq74-0" status= "1"/> </message>

 

We can get received like this.

Received received= (Received) message.getextension (Received.name, received.name_space);

  

Extended Smack Message

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.