ralphm/wokkel — Issue #3: Python Twisted Wokkel keeps disconnecting
Author: flxfxp
Created: 2012-03-08 11:02:14+00:00
State: open

For some reason my bot keeps disconnecting, following by a reconnect. I thought it might had to do with responding to probes to keep alive, but I'm still being disconnected after ~3 minutes. Example code:

```python
from twisted.words.xish import domish
from wokkel.xmppim import MessageProtocol, AvailablePresence
from twisted.words.protocols.jabber.jid import JID
from wokkel.client import XMPPClient
from wokkel import component, server, xmppim

#Config
THIS_JID = JID('10804_81231@chat.hipchat.com')
ROOM_JID = JID('10804_test_channel@conf.hipchat.com')
NICK = 'Bad Robot'
SECRET = 'l0wl3v3l'

# Protocol handlers

class PresenceAcceptingHandler(xmppim.PresenceProtocol):
"""
Presence accepting XMPP subprotocol handler.

This handler blindly accepts incoming presence subscription requests,
confirms unsubscription requests and responds to presence probes.

Note that this handler does not remember any contacts, so it will not
send presence when starting.
"""
def subscribedReceived(self, presence):
"""
Subscription approval confirmation was received.

This is just a confirmation. Don't respond.
"""
pass

def unsubscribedReceived(self, presence):
"""
Unsubscription confirmation was received.

This is just a confirmation. Don't respond.
"""
pass

def subscribeReceived(self, presence):
"""
Subscription request was received.

Always grant permission to see our presence.
"""
self.subscribed(recipient=presence.sender,
sender=presence.recipient)
self.available(recipient=presence.sender,
status=u"I'm here",
sender=presence.recipient)

def unsubscribeReceived(self, presence):
"""
Unsubscription request was received.

Always confirm unsubscription requests.
"""
self.unsubscribed(recipient=presence.sender,
sender=presence.recipient)

def probeReceived(self, presence):
"""
A presence probe was received.

Always send available presence to whoever is asking.
"""
self.available(recipient=presence.sender,
status=u"I'm here",
sender=presence.recipient)
print "probe send!"

class EchoBotProtocol(MessageProtocol):
def connectionMade(self):
print "Connected!"

# send initial presence
self.send(AvailablePresence())

def connectionLost(self, reason):
print "Disconnected! "+str(reason)

def onMessage(self, msg):
print str(msg)

if msg["type"] == 'chat' and hasattr(msg, "body"):
reply = domish.Element((None, "message"))
reply["to"] = msg["from"]
reply["from"] = msg["to"]
reply["type"] = 'chat'
reply.addElement("body", content="echo: " + str(msg.body))

self.send(reply)

if __name__ == "__main__":
# Start as a regular Python script.

import sys
from twisted.python import log
from twisted.internet import reactor

log.startLogging(sys.stdout, setStdout=0)
client = XMPPClient(THIS_JID, SECRET)
client.logTraffic = False
client.send('') # Hello, OpenFire!
client.startService()

presenceHandler = PresenceAcceptingHandler()
presenceHandler.setHandlerParent(client)

personalHandler = EchoBotProtocol()
personalHandler.setHandlerParent(client)

reactor.run()
else:
# Start as a Twisted Application

from twisted.application import service
application = service.Application("MUC Client")

client = XMPPClient(THIS_JID, SECRET)
client.logTraffic = False
client.setServiceParent(application)

presenceHandler = PresenceAcceptingHandler()
presenceHandler.setHandlerParent(client)

personalHandler = EchoBotProtocol()
personalHandler.setHandlerParent(client)
```
The log looks like this:
```
Connected!
Disconnected! [Failure instance: Traceback (failure with no frames): : Connection was closed cleanly.
]
2012-03-08 11:24:06+0100 [XmlStream,client] will retry in 3 seconds
2012-03-08 11:24:06+0100 [XmlStream,client] Stopping factory
2012-03-08 11:24:09+0100 [-] Starting factory
2012-03-08 11:24:09+0100 [-] DNSDatagramProtocol starting on 56948
2012-03-08 11:24:09+0100 [-] Starting protocol
2012-03-08 11:24:09+0100 [-] (UDP Port 56948 Closed)
2012-03-08 11:24:09+0100 [-] Stopping protocol
Connected!
```
Enabling logTraffic doesn't help, as I don't see any messages about my disconnect. Can you help me out? Many thanks!