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

Well, I found the solution thanks to you.

  1. When you use the source code with “connector.connect()” on “clientConnectionLost” and “clientConnectionFailed”, like my previous email; if server is down, the client will try to reconnect every time as he can (3 times per second or more)

  2. When you use the source code with “ReconnectingClientFactory”; if server is down, the client will try to reconnect and if he failed, he tries after 2 secondes, and after 6 seconds, and after 8 seconds… until 3600 seconds (by default)

Look my example, I block the max delay to 10 seconds :

class TesteeClientFactory(ReconnectingClientFactory, WebSocketClientFactory):

protocol = TesteeClientProtocol

def startedConnecting(self, connector):

print ‘Started to connect.’

def buildProtocol(self, addr):

print ‘Connected.’

self.maxDelay = 10

return WebSocketClientFactory.buildProtocol(self, addr)

def clientConnectionLost(self, connector, reason):

print ‘Lost connection. Reason:’, reason

ReconnectingClientFactory.clientConnectionLost(self, connector, reason)

def clientConnectionFailed(self, connector, reason):

print ‘Connection failed. Reason:’, reason

ReconnectingClientFactory.clientConnectionFailed(self, connector,

reason)

Thank you for your help.

0 Likes

#3

Hi Kevin,

yes, the ReconnectingClientFactory code does (jitter'ed) exponential backoff.

I've added an example to the repo:

https://github.com/tavendo/AutobahnPython/blob/master/examples/websocket/echo/client_reconnecting.py

/Tobias

···

Am 21.06.2013 12:14, schrieb Kevin Campion:

Well, I found the solution thanks to you.

1) When you use the source code with "connector.connect()" on
"clientConnectionLost" and "clientConnectionFailed", like my previous
email; if server is down, the client will try to reconnect every time as
he can (3 times per second or more)

2) When you use the source code with "ReconnectingClientFactory"; if
server is down, the client will try to reconnect and if he failed, he
tries after 2 secondes, and after 6 seconds, and after 8 seconds...
until 3600 <tel:3600> seconds (by default)

Look my example, I block the max delay to 10 seconds :

class TesteeClientFactory(ReconnectingClientFactory,
WebSocketClientFactory):

     protocol = TesteeClientProtocol

     def startedConnecting(self, connector):
         print 'Started to connect.'

     def buildProtocol(self, addr):
         print 'Connected.'
         self.maxDelay = 10

         return WebSocketClientFactory.buildProtocol(self, addr)

     def clientConnectionLost(self, connector, reason):
         print 'Lost connection. Reason:', reason
         ReconnectingClientFactory.clientConnectionLost(self, connector,
reason)

     def clientConnectionFailed(self, connector, reason):
         print 'Connection failed. Reason:', reason
         ReconnectingClientFactory.clientConnectionFailed(self, connector,
                                                          reason)

Thank you for your help.

--
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

#4

Thank you

0 Likes

#5

URL 404’d,
new url for anyone looking

https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/websocket/echo_variants/client_reconnecting.py

···

On Saturday, 22 June 2013 19:49:12 UTC+10, Tobias Oberstein wrote:

Hi Kevin,

yes, the ReconnectingClientFactory code does (jitter’ed) exponential
backoff.

I’ve added an example to the repo:

https://github.com/tavendo/AutobahnPython/blob/master/examples/websocket/echo/client_reconnecting.py

/Tobias

Am 21.06.2013 12:14, schrieb Kevin Campion:

Well, I found the solution thanks to you.

  1. When you use the source code with “connector.connect()” on

“clientConnectionLost” and “clientConnectionFailed”, like my previous

email; if server is down, the client will try to reconnect every time as

he can (3 times per second or more)

  1. When you use the source code with “ReconnectingClientFactory”; if

server is down, the client will try to reconnect and if he failed, he

tries after 2 secondes, and after 6 seconds, and after 8 seconds…

until 3600 tel:3600 seconds (by default)

Look my example, I block the max delay to 10 seconds :

class TesteeClientFactory(ReconnectingClientFactory,

WebSocketClientFactory):

 protocol = TesteeClientProtocol
 def startedConnecting(self, connector):
     print 'Started to connect.'
 def buildProtocol(self, addr):
     print 'Connected.'
     self.maxDelay = 10
     return WebSocketClientFactory.buildProtocol(self, addr)
 def clientConnectionLost(self, connector, reason):
     print 'Lost connection.  Reason:', reason
     ReconnectingClientFactory.clientConnectionLost(self, connector,

reason)

 def clientConnectionFailed(self, connector, reason):
     print 'Connection failed. Reason:', reason
     ReconnectingClientFactory.clientConnectionFailed(self, connector,
                                                      reason)

Thank you for your help.

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