如何断开 XMPPPY 客户端对象的连接

发布于 2024-09-25 19:12:36 字数 715 浏览 0 评论 0原文

使用 XMPPPY 连接到 XMPP 服务器非常简单。

from xmpp.client import Client as XMPPClient

self.xmppClient = XMPPClient("jabber.foo.com")
if not self.xmppClient.connect(server="localhost"):
    raise IOError('Cannot connect to server.')
if not self.xmppClient.auth("node", "password", "resource"):
    raise IOError('Can not auth with server.')
    self.xmppClient.RegisterHandler("message", self.messageHandler)
self.xmppClient.sendInitPresence()

然而,有时我的客户不得不强制断开连接,但仍继续做其他事情。我想确保客户端正确断开连接 - 套接字没有“闲置”并且服务器资源没有被浪费。

预期的模式是否只是将客户端设置为 None 并让 GC 清理对象?

self.xmppClient = None

我在客户端中看到“断开连接的处理程序”,但我不知道如何调用它们。 XMPPPY 附带的文档非常糟糕。

有人知道断开连接的“正确方法”吗?

Connecting to an XMPP server with XMPPPY is simple.

from xmpp.client import Client as XMPPClient

self.xmppClient = XMPPClient("jabber.foo.com")
if not self.xmppClient.connect(server="localhost"):
    raise IOError('Cannot connect to server.')
if not self.xmppClient.auth("node", "password", "resource"):
    raise IOError('Can not auth with server.')
    self.xmppClient.RegisterHandler("message", self.messageHandler)
self.xmppClient.sendInitPresence()

However, there are times when my client has to force a disconnect, yet still keep going doing other things. I'd like to make sure that the client disconnects properly - that the socket is not 'hanging around' and the server resources are not being wasted.

Is the intended pattern to simply set the client to None and let GC clean up the object?

self.xmppClient = None

I see "disconnected handlers" in the client, but I do not see how to invoke them. And the documentation that comes with XMPPPY is horrible.

Anyone have any clue of the "right way" to disconnect?

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

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

发布评论

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

评论(2

帅冕 2024-10-02 19:12:36

一般来说,如果您想断开与 XMPP 服务器的连接,您将发送一个 type='unavailable' 的 Presence 节,如下所示:

<presence type='unavailable' />

请注意,Presence 没有收件人地址。如果您想了解更多信息,这里有一个指向 XMPP RFC 的链接。 (第 5.1.5 节)

之后,您可以优雅地断开与服务器的连接,因为您发送的存在基本上告诉服务器“我出去了”。

我查看了 XMPPPY 文档(是的,我同意它还有改进的空间),似乎 xmpp.Client.Client 包含一个函数调用 sendPresence(...)。您可以使用该功能发送不可用状态吗?

以下是 API 文档: http://xmpppy.sourceforge.net/ apidocs/xmpp.client.Client-class.html#sendPresence

Generally speaking, if you want to disconnect from an XMPP server, you'll send a Presence stanze with type='unavailable', like the following:

<presence type='unavailable' />

Note that Presence does NOT have a recipient address. Here's a link to XMPP's RFC in case you'd like to know more. (Section 5.1.5)

After that, you can gracefully disconnect from the server, since the presence you sent basically tells the server, "I'm out.".

I took a look at the XMPPPY documentation (Yes, I agree that it has room for improvement), and it seems xmpp.Client.Client contains a function call sendPresence(...). You might be able to send an unavailable presence using the function?

Here's the API Documentation: http://xmpppy.sourceforge.net/apidocs/xmpp.client.Client-class.html#sendPresence

北渚 2024-10-02 19:12:36

xmpp.dispatcher.Dispatcher 类有一个 disconnect() 方法。从自动生成的文档中并不明显,因为它是通过 PlugIn 机制动态加载的,但您可以在任何 CommentClient 对象上使用它。

The xmpp.dispatcher.Dispatcher class has a disconnect() method. It's not obvious from the automatically generated documentation as it's loaded dynamically through the PlugIn mechanism, but you can use it on any CommentClient object.

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