如何使用 Python 和 XMPP 创建 MUC 并向现有 MUC 发送消息

发布于 2024-09-15 09:25:32 字数 627 浏览 2 评论 0原文

我想知道这里是否有人可以提供一些有关以下场景的代码示例。我对使用 xmpppy 来执行此操作特别感兴趣,因为我已经在我的应用程序中使用该库,但其他库也可以。不幸的是,xmpppy 项目网站没有任何这方面的示例。浏览专家/高级 API 文档,我不知道该怎么做,或者 xmpppy 不支持多用户聊天(MUC)?

  • 通过邀请特定用户(例如 2 或 3)创建 MUC

  • 向现有 MUC 发送消息(假设您知道它是 MUC JID 句柄)或昵称)

  • 在 XMPP 服务器上查找现有 MUC,获取 JID 或昵称等。如果这是通过获取来完成的名册,我们只想查找 MUC,忽略用户。

我在这里找到了某种答案,但随后我可能必须学习新的库 API 调用并弄清楚如何执行上述场景,因为此示例并未涵盖所有场景:

pyxmpp:创建 muc 客户端的快速教程?

我真的很想做一个负载生成器向 MUC 发送消息并创建具有许多参与者的大型 MUC。我已经准备好向用户收件人发送消息的部分。

I was wondering if anyone here can provide some code samples on the following scenarios. I'm particularly interested in using xmpppy to do this as I'm already using the library for my app, but other libraries ok too. It is unfortunate that the xmpppy project website doesn't have any samples on this. Browsing the expert/advanced API docs, I couldn't figure out how to do it, or is multi-user chat (MUC) not supported with xmpppy?

  • create a MUC by inviting specific users (say 2 or 3)

  • send message to an existing MUC (assuming you know it's MUC JID handle or nickname)

  • look up existing MUCs on the XMPP server, getting the JID or nickname, etc. If this is done by getting roster, we want to only look for MUCs, ignoring users.

I found sort of an answer here, but then I'd probably have to learn new library API calls and figure out how to do my above mentioned scenarios as this sample doesn't cover all of them:

pyxmpp: quick tutorial for creating a muc client?

I'm really looking to do a load generator that pumps messages to MUCs and creating large MUCs with many participants. I've already got the part in place for pumping messages to user recipients.

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

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

发布评论

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

评论(2

笑忘罢 2024-09-22 09:25:32

虽然我不知道具体的 MUC 接口,但 xmpppy 支持自定义消息,因此它支持整个 XMPP。

要加入聊天,您需要发送状态信息,conn.send(xmpp.Presence(to='{0}/{1}'.format(room, nick)))

发送消息聊天:

    stranza = "<message to='{0}' type='groupchat'><body>{1}</body></message>".format(room, text)
    conn.send(stranza)

至于创建新的聊天或在花名册中查找它,我手头没有现成的代码,但以同样的方式编写很容易,只需在 XEP 中查找所需的片段即可:

http://xmpp.org/extensions/xep-0045.html#createroom

http://xmpp.org/extensions/xep-0045.html#disco-rooms

http://xmpp.org/extensions/xep-0045.html#invite

While I dont know about specific MUC interface there, xmpppy supports custom messages, so it supports whole XMPP.

To join chat, you need to send presence stranza, conn.send(xmpp.Presence(to='{0}/{1}'.format(room, nick)))

To send a message to chat:

    stranza = "<message to='{0}' type='groupchat'><body>{1}</body></message>".format(room, text)
    conn.send(stranza)

As for creating new chat or looking it up in roster, I don't have ready code at hand, but it is easy to write in the same fashion, just look up needed stranzas in XEPs:

http://xmpp.org/extensions/xep-0045.html#createroom

http://xmpp.org/extensions/xep-0045.html#disco-rooms

http://xmpp.org/extensions/xep-0045.html#invite

櫻之舞 2024-09-22 09:25:32

Xmpppy确实支持使用MUC服务,但支持非常基础,需要一些额外的代码来管理多个房间。

要“加入”MUC 房间,您需要向 JID 广播您的存在。

conn.send(xmpp.Presence(to="%s/%s" % (room, nickname)))

然后,要发送消息,您可以使用“groupchat”消息类型将它们发送到房间 JID。

msg = xmpp.protocol.Message(body=text)
msg.setTo(room)
msg.setType('groupchat')
conn.send(msg)

至于您关于查找 MUC 房间的问题,这将通过服务发现来完成。

Xmpppy does support using MUC services, but the support is very basic and some extra code will be needed to manage multiple rooms.

To "join" a MUC room you need to broadcast your presence to the JID.

conn.send(xmpp.Presence(to="%s/%s" % (room, nickname)))

Then to send messages, you send them using the 'groupchat' message type to the room JID.

msg = xmpp.protocol.Message(body=text)
msg.setTo(room)
msg.setType('groupchat')
conn.send(msg)

As for your question regarding finding MUC rooms, that'll be done via service discovery.

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