如何断开 XMPPPY 客户端对象的连接
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,如果您想断开与 XMPP 服务器的连接,您将发送一个 type='unavailable' 的 Presence 节,如下所示:
请注意,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:
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
xmpp.dispatcher.Dispatcher
类有一个disconnect()
方法。从自动生成的文档中并不明显,因为它是通过PlugIn
机制动态加载的,但您可以在任何CommentClient
对象上使用它。The
xmpp.dispatcher.Dispatcher
class has adisconnect()
method. It's not obvious from the automatically generated documentation as it's loaded dynamically through thePlugIn
mechanism, but you can use it on anyCommentClient
object.