how to use Twisted ReconnectingClientFactory?

#1

Hi,

I’ve been following various discussions and questions about when client reconnect logic might be available in AutobahnPython, but I can’t wait any longer and need to implement something now. I’m hoping I’m almost there and that it’s something small I’m missing.

My client components are using ApplicationRunner, so what I’ve done is:

  1. Created a new class called MyReconnectingClientFactory that extends WampWebSocketClientFactory and Twisted’s ReconnectingClientFactory
  2. Duplicated the ApplicationRunner class from the AutobahnPython 0.12.1 release to a new class called MyReconnectingApplicationRunner, and changed the line in the run() method that creates a transport factory to instantiate a MyReconnectingClientFactory instance instead of a WampWebSocketClientFactory.
    Relevant code (I’ve omitted things here like importing txaio, etc.):

from autobahn.twisted.websocket import WampWebSocketClientFactory
from twisted.internet.protocol import ReconnectingClientFactory

This is the entire class:

class MyReconnectingClientFactory(WampWebSocketClientFactory, ReconnectingClientFactory):

maxDelay = 30

def clientConnectionFailed(self, connector, reason):
    print "WAMP connection failed: {}".format(reason)
    ReconnectingClientFactory.clientConnectionFailed(
        self,
        connector,
        reason)

def clientConnectionLost(self, connector, reason):
    print "WAMP connection lost: {}".format(reason)
    ReconnectingClientFactory.clientConnectionLost(
        self,
        connector,
        reason)

Duplicate of the 0.12.1 ApplicationRunner class, with only the one line changed as indicated:

class MyReconnectingApplicationRunner(object):
.
.
.
def run(self, make, start_reactor=True):
.
.
.

Changed line:

transport_factory = WampWebSocketClientFactory(create, url=self.url, serializers=self.serializers,

proxy=self.proxy, debug=self.debug)

    transport_factory = MyReconnectingClientFactory(create, url=self.url, serializers=self.serializers,
                                                    proxy=self.proxy, debug=self.debug)

.
.
.

``

Things don’t seem to be working yet - the client component connects fine initially, but I only get a TransportLost exception when I shut down the router and no indications that the client is attempting to reconnect. Am I missing something obvious?

The initial example I looked at was: https://gist.github.com/DenJohX/e6d0864738da10cb9685

Thanks for any help - I’m sure others have attempted this and that any help here could be useful to others as well. VERY MUCH looking forward to client reconnect logic being officially available in AutobahnPython as it is in AutobahnJS!

Dave

0 Likes

#2

Still not working right yet. But I would have thought someone else surely would have tried to tackle this. I must be doing something stupid, but just haven’t discovered it yet.

Alternatively, Tobias, if you’re reading this - somewhere else in here I seem to remember you saying that the Autobahn client-reconnecting code is in “trunk” already, just not yet released. I poked around GitHub but couldn’t find it. More stupidity on my part, I guess. Can you provide a pointer/URL?

Thanks,

Dave

0 Likes