best way to exchange custom XML elements in XMPP?

2019-07-20 15:33发布

问题:

I have an XMPP application where two clients interact (1) a bot programmed with Smack (Scala/Java) and (2) a GUI chat client programmed in strophe (Javascript).

The app needs to exchange custom XML (e.g., as shown below)

<myPacket>
   <response type='sensorData'>
      <temperature units='Kelvin'>
         234
      </temperature>
   </response>
</myPacket>

What is the best way to exchange data? The following are what I could come up with:

1) Write custom XMPP stanzas

2) Embed my XML in currently defined stanzas (e.g, in message or iq)

3) For smack, use the Message.get(set)Property to read/write custom data.

No. 3) is ruled out because it requires that both clients understand Java objects and use the same library (Smack).

Kindly point me to any other questions addressing the same issue.

[EDIT:] After doing some research, I found that Dataforms (XEP-0004) is the easiest way to do it, provided your library supports it.

回答1:

That largely depends on how the data is going to be used. My rule of thumb is that if I'm only passing around key-value pairs (simple data) then I would go for the property feature in Smack. But the property feature can only be used in Message. For some reason, the property extension does not use Smack's provider architecture but its hardcoded into Packet and PacketParserUtils class. So if you can't reuse it in IQ or Presence packets.

If you are going to use anything more that a key-value pair, then you should write a provider for your custom stanza. It's a bit of work but once you've implemented the marshalling/unmarshalling then your custom stanza works pretty much everywhere in the Smack framework. See Smacks provider architecture for details.

I did blog about writing provider in one of my post. It's not the main trust of the post but hope you find it helpful as well.



标签: xml xmpp