From 3c6b07353dc0243a799e26fed5d08af662600d1e Mon Sep 17 00:00:00 2001 From: Tom Nichols Date: Fri, 9 Jul 2010 16:06:53 -0400 Subject: added keepalive to send thread --- sleekxmpp/xmlstream/xmlstream.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 00af3d2e..7098e1c1 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -47,6 +47,7 @@ stanza_extensions = {} RECONNECT_MAX_DELAY = 3600 RECONNECT_QUIESCE_FACTOR = 1.6180339887498948 # Phi RECONNECT_QUIESCE_JITTER = 0.11962656472 # molar Planck constant times c, joule meter/mole +DEFAULT_KEEPALIVE = 300 # send a single byte every 5 minutes class XMLStream(object): "A connection manager with XML events." @@ -74,6 +75,9 @@ class XMLStream(object): self.use_tls = False self.ca_certs=None + self.keep_alive = DEFAULT_KEEPALIVE + self._last_sent_time = time.time() + self.stream_header = "" self.stream_footer = "" @@ -290,9 +294,12 @@ class XMLStream(object): data = self.sendqueue.get(True,5)[1] logging.debug("SEND: %s" % data) self.socket.sendall(data.encode('utf-8')) - except queue.Empty: -# logging.debug('Nothing on send queue') - pass + self._last_sent_time = time.time() + except queue.Empty: # send keep-alive if necessary + now = time.time() + if self._last_sent_time + self.keep_alive < now: + self.socket.sendall(' ') + self._last_sent_time = time.time() except socket.timeout: # this is to prevent a thread blocked indefinitely logging.debug('timeout sending packet data') -- cgit v1.2.3