如何通过仅向 ejabberd 服务器发送一条 <presence> 消息来加入多个房间

How to join multiple rooms by just sending one <presence> message to ejabberd server

比如我有20个房间要加入。简单的解决方案是向每个房间 ID 发送 20 条消息。考虑到性能,这很糟糕。

我想通过发送一条<presence>消息加入20个房间,如何实现?编写一个模块来挂钩自定义 <presence> 消息?但是我不知道怎么写这种模块。

XEP-0045 Multi User Chat 中,没有定义使用一个状态数据包加入 20 个聊天室的方法。

但是,通过将其他 XMPP 扩展与多用户聊天相结合,您绝对可以在纯 XMPP 中实现这一点,而无需编写自定义 ejabberd 扩展。

你可以依靠XEP-0033 Extended Stanza Addressing, you can send an XMPP packet to several recipient. It also works with presence as shown in this example

ejabberd 配置

XEP-0033 自 ejabberd 15.04 起默认支持扩展节寻址。确保通过在 ejabberd 配置模块部分添加 mod_multicast 来启用该功能:

modules:
...
  mod_multicast: {}

启用该服务后,您应该在支持该功能的服务器上有一个新服务(默认名为 multicast.example.nethttp://jabber.org/protocol/address:

<iq type='get'
    to='multicast.example.net'
    id='info1'>
  <query xmlns='http://jabber.org/protocol/disco#info'/>
</iq>

响应是:

<iq from="multicast.example.net" type="result" to="test@example.net/laptop" id="info1">
<query xmlns="http://jabber.org/protocol/disco#info">
<identity category="service" type="multicast" name="Multicast"/>
<feature var="http://jabber.org/protocol/disco#info"/>
<feature var="http://jabber.org/protocol/disco#items"/>
<feature var="vcard-temp"/>
<feature var="http://jabber.org/protocol/address"/>
</query>
</iq>

用法

启用后,很容易发送针对多个 MUC 房间的状态数据包:

<presence to='multicast.example.net'>
    <addresses xmlns='http://jabber.org/protocol/address'>
        <address type='bcc' jid='testroom@conference.example.net/Usernick'/>
        <address type='bcc' jid='testroom2@ conference.example.net/Usernick'/>
    </addresses>
</presence>

因此您会看到您同时加入了多个房间。

问题

XEP-0033 没有具体提及该用例,并且该模块尚未测试加入多个房间。在编写这个示例时,我发现在断开连接时,用户没有正确离开他加入的房间。 这意味着您必须等待以下 Github 问题得到解决才能在生产中使用该功能:Broadcast presence change after multicast presence packet.