在 XMPP 中交换自定义 XML 元素的最佳方式?
我有一个 XMPP 应用程序,其中两个客户端进行交互 (1) 使用 Smack (Scala/ Java)和(2)用 strope (Javascript) 编程的 GUI 聊天客户端。
应用程序需要交换自定义 XML(例如,如下所示)
<myPacket>
<response type='sensorData'>
<temperature units='Kelvin'>
234
</temperature>
</response>
</myPacket>
交换数据的最佳方式是什么?以下是我能想到的:
1) 编写自定义 XMPP 节
2) 将我的 XML 嵌入当前定义的节中(例如,在消息或 iq 中)
3) 对于 smack,使用 Message.get(set)Property
读取/写入自定义数据。
No. 3) 被排除,因为它要求两个客户端都理解 Java 对象并使用相同的库 (Smack)。
请指出解决同一问题的任何其他问题。
[编辑:] 经过一些研究,我发现 Dataforms (XEP-0004) 是最简单的方法,只要你的库支持它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很大程度上取决于数据的使用方式。我的经验法则是,如果我只传递键值对(简单数据),那么我会选择 Smack 中的属性功能。但属性功能只能在Message中使用。由于某种原因,属性扩展不使用 Smack 的提供程序架构,而是硬编码到
Packet
和PacketParserUtils
类中。因此,如果您无法在 IQ 或 Presence 数据包中重用它。如果您打算使用键值对以外的任何内容,那么您应该为您的自定义节编写一个提供程序。这需要一些工作,但是一旦您实现了编组/解编组,那么您的自定义节几乎可以在 Smack 框架中的任何地方工作。有关详细信息,请参阅 Smacks 提供程序架构。
我在我的一篇帖子。这不是这篇文章的主要内容,但希望你发现它也有帮助。
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
andPacketParserUtils
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.