Client reconnecting from Websockets in Python

#1

Yep, sounds a bit like you are piling up new clients which each tries to reconnect again and again.

Probably instead of implementing a reconnection policy yourself (yes, we are guilty of that also), we should use

http://twistedmatrix.com/documents/current/api/twisted.internet.protocol.ReconnectingClientFactory.html
http://twistedmatrix.com/documents/13.0.0/core/howto/clients.html

The "problem" is, Autobahn (currently) defines

class WebSocketClientFactory(protocol.ClientFactory, WebSocketFactory):
    """
    A Twisted factory for WebSocket client protocols.
    """

    protocol = WebSocketClientProtocol

···

Am 20.06.2013 16:35, schrieb Kevin Campion:

Hi,

I am trying to get Autobahn.Python to work with disconnecting when
server down, without having to restart the program. The client program
must try to reconnect every x seconds to the server.

My source code works but when client reconnects, I can see in server's
log multiple connection for the same client. I have an
additional connection per trying reconnection.

I think, it's when I use "connector.connect()"

==

Probably this works:

class MyFactory(ReconnectingClientFactory, WebSocketClientFactory)

Not sure, cannot test right now.

Possibly, the empty implementation in WebSocketClientFactory.clientConnectionFailed and clientConnectionLost
are "in the way" ..

Anyway, I'd try to either use ReconnectingClientFactory or have a look at the implementation.

/Tobias

I use this discussion and the example on GitHub :
https://groups.google.com/forum/?fromgroups#!searchin/autobahnws/reconnect|sort:relevance/autobahnws/HCNFWKGYQCM/G1EGNGxOZXkJ
https://github.com/tavendo/AutobahnTestSuite/blob/master/autobahntestsuite/autobahntestsuite/testee.py#L87
<https://github.com/tavendo/AutobahnTestSuite/blob/master/autobahntestsuite/autobahntestsuite/testee.py#L87>

My TesteeClientFactory class looks like this :

    class TesteeClientFactory(WebSocketClientFactory):
        protocol = TesteeClientProtocol
        def clientConnectionLost(self, connector, reason):
           connector.connect()

        def clientConnectionFailed(self, connector, reason):
           print "Connection to %s failed (%s)" % (self.url,
    reason.getErrorMessage())
           self.tryToConnect(connector)
        def tryToConnect(self, connector):
           connector.disconnect()
           var = 1
           while var == 1 :
               time.sleep( 2 )
               newConnection = connector.connect()
               if newConnection != None:
                   break
               connector.disconnect()

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Likes

#2

Hi I was looking at the new example and apparently mis-understand something.

I was told to use “ReconnectingClientFactory.resetDelay()” after the connection has completed due to the reconnect back-off.

if I place that line in MyClientProtocol.onConnect I get a loop of connection losses/reconnect attempts.

If I place it in MyClientProtocol.onOpen I get an error that I need an instance of ReconnectingClientFactory.

Where is the proper place to put “ReconnectingClientFactory.resetDelay()”?

Thanks!

···

On Thursday, June 20, 2013 at 1:43:33 PM UTC-5, Tobias Oberstein wrote:

Am 20.06.2013 16:35, schrieb Kevin Campion:

Hi,

I am trying to get Autobahn.Python to work with disconnecting when

server down, without having to restart the program. The client program

must try to reconnect every x seconds to the server.

My source code works but when client reconnects, I can see in server’s

log multiple connection for the same client. I have an

additional connection per trying reconnection.

I think, it’s when I use “connector.connect()”

Yep, sounds a bit like you are piling up new clients which each tries
to reconnect again and again.

Probably instead of implementing a reconnection policy yourself (yes, we
are guilty of that also), we should use

http://twistedmatrix.com/documents/current/api/twisted.internet.protocol.ReconnectingClientFactory.html

http://twistedmatrix.com/documents/13.0.0/core/howto/clients.html

The “problem” is, Autobahn (currently) defines

class WebSocketClientFactory(protocol.ClientFactory, WebSocketFactory):

"""

A Twisted factory for WebSocket client protocols.

"""



protocol = WebSocketClientProtocol

==

Probably this works:

class MyFactory(ReconnectingClientFactory, WebSocketClientFactory)

Not sure, cannot test right now.

Possibly, the empty implementation in
WebSocketClientFactory.clientConnectionFailed and clientConnectionLost

are “in the way” …

Anyway, I’d try to either use ReconnectingClientFactory or have a look
at the implementation.

/Tobias

I use this discussion and the example on GitHub :

https://groups.google.com/forum/?fromgroups#!searchin/autobahnws/reconnect|sort:relevance/autobahnws/HCNFWKGYQCM/G1EGNGxOZXkJ

https://github.com/tavendo/AutobahnTestSuite/blob/master/autobahntestsuite/autobahntestsuite/testee.py#L87

<https://github.com/tavendo/AutobahnTestSuite/blob/master/autobahntestsuite/autobahntestsuite/testee.py#L87>

My TesteeClientFactory class looks like this :

class TesteeClientFactory(WebSocketClientFactory):
    protocol = TesteeClientProtocol
    def clientConnectionLost(self, connector, reason):
       connector.connect()
    def clientConnectionFailed(self, connector, reason):
       print "Connection to %s failed (%s)" % (self.url,
reason.getErrorMessage())
       self.tryToConnect(connector)
    def tryToConnect(self, connector):
       connector.disconnect()
       var = 1
       while var == 1 :
           time.sleep( 2 )
           newConnection = connector.connect()
           if newConnection != None:
               break
           connector.disconnect()

You received this message because you are subscribed to the Google

Groups “Autobahn” group.

To unsubscribe from this group and stop receiving emails from it, send

an email to autobahnws+...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

0 Likes

#3

Hi James,

This works for me: https://github.com/tavendo/AutobahnPython/pull/522/files

···

On Friday, 11 September 2015 11:43:24 UTC-6, James Firth wrote:

Where is the proper place to put “ReconnectingClientFactory.resetDelay()”?


meejah

0 Likes

#4

This appears to have helped a lot, thanks!

···

On Thursday, September 24, 2015 at 10:17:14 AM UTC-5, meejah wrote:

On Friday, 11 September 2015 11:43:24 UTC-6, James Firth wrote:

Where is the proper place to put “ReconnectingClientFactory.resetDelay()”?

Hi James,

This works for me: https://github.com/tavendo/AutobahnPython/pull/522/files


meejah

0 Likes