在 XMPP 中交换自定义 XML 元素的最佳方式? [英] best way to exchange custom XML elements in XMPP?

查看:26
本文介绍了在 XMPP 中交换自定义 XML 元素的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XMPP 应用程序,其中两个客户端交互 (1) 一个使用 Smack 编程的机器人(Scala/Java) 和 (2) 在 strophe (Javascript) 中编程的 GUI 聊天客户端.

应用程序需要交换自定义 XML(例如,如下所示)

<响应类型='sensorData'><温度单位='开尔文'>234</温度></响应></myPacket>

交换数据的最佳方式是什么?以下是我能想到的:

1) 编写自定义 XMPP 节

2) 将我的 XML 嵌入当前定义的节(例如,在消息或 iq 中)

3) 对于 smack,使用 Message.get(set)Property 读取/写入自定义数据.

没有.3) 被排除,因为它要求两个客户端都理解 Java 对象并使用相同的库 (Smack).

请指出解决同一问题的任何其他问题.

在做了一些研究之后,我发现 Dataforms (XEP-0004) 是最简单的方法,前提是您的图书馆支持它.

解决方案

这在很大程度上取决于将如何使用数据.我的经验法则是,如果我只传递键值对(简单数据),那么我会选择 Smack 中的属性功能.但是属性特性只能在Message中使用.出于某种原因,属性扩展不使用 Smack 的提供程序架构,而是将其硬编码到 PacketPacketParserUtils 类中.因此,如果您不能在 IQ 或 Presence 数据包中重复使用它.

如果您打算使用键值对以外的任何东西,那么您应该为您的自定义节编写一个提供程序.这是一些工作,但是一旦您实现了编组/解组,那么您的自定义节几乎可以在 Smack 框架中的任何地方工作.有关详细信息,请参阅 Smacks provider 架构.

我在我的一篇帖子中写了一篇关于编写提供程序的博客.这不是该帖子的主要信任,但希望您发现它也有帮助.

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.

解决方案

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.

这篇关于在 XMPP 中交换自定义 XML 元素的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆