How to send custom XML blocks in message stanzas?

I want to be able to send custom blocks of XML to clients in messages. What are the classes and components I would need to extend to achieve this?

I guess I would need a custom ExtensionProvider, but what else would I need to do?

Advice appreciated.

Hi Matt. You need to start by writing a custom PacketExtension. This just needs to provide get and set methods for any data that it represents, and an implementation of the toXML() in order to tell Smack how to create the XML string that will be sent over the wire as part of the packet. Also decide on an element name and namespace.

Next, write a custom PacketExtensionProvider. This is basically a hand written parser for the toXML() string you previously created in the PacketExtension. You get passed a PullParser that points to the first element, and you iterate over the extension pulling out the values as you go. The PacketExtensionProvider returns a PacketExtension (which is obviuously an instance of the custom sub-type you previously defined), so you can now create an instance and populate it with the values you pull out from the XML.

Now you have completed the steps for going from data to XML and from XML to data. Next we need to tell SMACK to look out for the element name and namespace of the extension, so that it knows that it is in fact able to parse it. The ProviderManager lets you register a new instance of the PacketExtensionProvider against a particular elementname & namespace pair.

So, now the final things we need to do is to see how we can actually add PacketExtensions to Packets in order to send then across the wire, and also for incoming packets, be able to detect PacketExtensions. Packet subclasses (Message, Presence, IQ) inherit the *addPacketExtension *method which allows us to simply construct a packet, an extension, and to add the extension to the packet. The packet can then be sent across the wire via the *sendPacket() *method on the XMPPConnection class. To detect incoming extensions, simply register a packet listener (with an appropriate filter) * * and use the *getPacketExtension *(which takes element name and nampespace as params) to pull out the extension. It will be null if there was no extension of that element name & namespace found.

Hello deeringc!

I need to write extensions for this protocol http://www.frogx.org/xep/multi-user_gaming.html

Does this mean I have to write a custom PacketExtension for each XML fragment (http://www.frogx.org/xep/multi-user_gaming.html ) ? Or simply I need a custom extension for each XML schema ( http://www.frogx.org/xep/multi-user_gaming.html#schema )?

Thanks in advance!

you need a custom extension for each

1 Like