处理客户端在5分钟空闲后与Openfire服务器的断开连接

0 投票
1 回答
1223 浏览
提问于 2025-04-17 09:14

我写了一个关于用pyxmpp2进行聊天的示例,但当客户端闲置大约5分钟后,服务器会和客户端断开连接。Openfire无法设置超时时间,所以我决定每5分钟发送一次状态消息。让我困惑的问题是,应该在什么时候发送这个状态消息呢?

import pyxmpp2

class EchoBot(EventHandler, XMPPFeatureHandler):
    """Echo Bot implementation."""
    def __init__(self, my_jid, settings):
        version_provider = VersionProvider(settings)
        self.client = Client(my_jid, [self, version_provider], settings)
    @event_handler(AuthorizedEvent)
    def handle_authorized(self,event):
        presence = Presence(to_jid ="....",stanza_type = "available")
        self.client.stream.send(presence)
    def run(self):
        """Request client connection and start the main loop."""
        self.client.connect()
        self.client.run()
    def disconnect(self):
        """"""
        self.client.disconnect()
    def keepconnect(self):
        presence = Presence(to_jid ="....",stanza_type = "available")
        self.client.stream.send(presence)
        print "send presence"
....
bot = McloudBot(JID(mcloudbotJID), settings)
try:
        bot.run()        
        t = threading.Thread(target=bot.run())
        timer=threading.Timer(5,bot.keepconnect())
        t.start()
        timer.start()
except KeyboardInterrupt:
        bot.disconnect()

但是好像不太管用……

1 个回答

0

可以看看这个链接:

http://community.igniterealtime.org/docs/DOC-2053

这里详细介绍了在OF中可以设置的“断开空闲连接”属性,单位是毫秒。

断开空闲的客户端在基于会话的通信中非常重要。不过,这更多是指客户端意外关闭,而不仅仅是因为空闲。

你可以在客户端中实现发送ping或心跳包,正如你上面提到的。也许可以看看pidgin如何实现空白IQ请求。

希望这些信息能帮到你。

詹姆斯

撰写回答