best way to exchange custom XML elements in XMPP?

女生的网名这么多〃 提交于 2019-12-11 01:31:00

问题


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.



来源:https://stackoverflow.com/questions/4222638/best-way-to-exchange-custom-xml-elements-in-xmpp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!