研究 XMPP 应用程序的线程实现有哪些好的资源?

发布于 2024-07-05 22:36:25 字数 187 浏览 7 评论 0原文

根据我的理解,XMPP 协议基于始终在线的连接,在这种连接中,您无法立即指示 XML 消息何时结束。

这意味着您必须在流到来时对其进行评估。 这也意味着您可能必须处理异步连接,因为套接字可能由于消息长度或连接速度慢而在 XML 消息中间阻塞。

我希望每个答案都有一个来源,这样我们就可以修改它们并看看最喜欢的是什么。

From my understanding the XMPP protocol is based on an always-on connection where you have no, immediate, indication of when an XML message ends.

This means you have to evaluate the stream as it comes. This also means that, probably, you have to deal with asynchronous connections since the socket can block in the middle of an XML message, either due to message length or a connection being slow.

I would appreciate one source per answer so we can mod them up and see what's the favourite.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

<逆流佳人身旁 2024-07-12 22:36:25

您想同时处理多个连接吗? 在这种情况下,必须进行良好的异步套接字处理,以避免每个连接一个线程。

否则,您只需要一个可以一次处理一大块字节的 XML 解析器。 Expat 是典型的例子; 如果您使用 Java,请尝试 XP。 这些类型的 XML 解析器将尽可能触发事件,并缓冲部分节,直到其余节到达。

现在,要解决您的断言,即当结束时没有通知,这并不是真的。 重要的是不要将 XML 流当作文档序列来处理。 使用以下伪代码:

stanza = null
while parser has more:
  switch on token type:
     START_TAG:
       elem =  create element from parser state
       if stanza is not null:
         add elem as child of stanza
       stanza = elem
     END_TAG:
       parent = parent of stanza
       if parent is not null:
         fire OnStanza event
       stanza = parent

此方法应该与基于事件的或拉式解析器一起使用。 它只需要保留一个指针的状态。 显然,您还需要处理属性、字符数据、实体引用(如 & 等)以及特殊用途的 Stream:stream 标记,但这应该可以帮助您入门。

Are you wanting to deal with multiple connections at once? Good asynch socket processing is a must in that case, to avoid one thread per connection.

Otherwise, you just need an XML parser that can deal with a chunk of bytes at a time. Expat is the canonical example; if you're in Java, try XP. These types of XML parsers will fire events as possible, and buffer partial stanzas until the rest arrives.

Now, to address your assertion that there is no notification when a stanza ends, that's not really true. The important thing is not to process the XML stream as if it is a sequence of documents. Use the following pseudo-code:

stanza = null
while parser has more:
  switch on token type:
     START_TAG:
       elem =  create element from parser state
       if stanza is not null:
         add elem as child of stanza
       stanza = elem
     END_TAG:
       parent = parent of stanza
       if parent is not null:
         fire OnStanza event
       stanza = parent

This approach should work with an event-based or pull parser. It only requires holding on to one pointer worth of state. Obviously, you'll also need to handle attributes, character data, entity references (like & and the like), and special-purpose the stream:stream tag, but this should get you started.

乱世争霸 2024-07-12 22:36:25

Igniterealtime.org 提供了一个用 java 编写的开源 XMPP 服务器和客户端

Igniterealtime.org provides an open source XMPP-server and client written in java

梦在深巷 2024-07-12 22:36:25

ejabberd 是用 Erlang 编写的。 我不知道 ejabberd 实现的细节,但使用 Erlang 的一个优点是线程非常便宜。 我推测他们为每个 XMPP 连接启动一个线程。 在 Erlang 术语中,这些被称为进程,但它们不是受保护的内存地址空间,它们是轻量级用户空间线程。

ejabberd is written in Erlang. I don't know the details of the ejabberd implementation, but one advantage of using Erlang is really inexpensive threads. I'll speculate they start a thread per XMPP connection. In Erlang terminology these would be called processes, but these are not protected-memory address spaces they are lightweight user-space threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文