Skip to content

Instantly share code, notes, and snippets.

@DenJohX
Created June 21, 2014 08:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DenJohX/e6d0864738da10cb9685 to your computer and use it in GitHub Desktop.
Save DenJohX/e6d0864738da10cb9685 to your computer and use it in GitHub Desktop.
# Autobahn client with auto-reconnect capability
# Totally based on https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/wamp/beginner/client.py
import sys
from twisted.python import log
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from twisted.internet.protocol import ReconnectingClientFactory
from autobahn.twisted import wamp, websocket
from autobahn.wamp import types
class MyFrontendComponent(wamp.ApplicationSession):
# TODO: coding fun :)
def onJoin(self, details):
pass
def onDisconnect(self):
pass
class MyClientFactory(websocket.WampWebSocketClientFactory, ReconnectingClientFactory):
maxDelay = 30
def clientConnectionFailed(self, connector, reason):
print "*************************************"
print "Connection Failed"
print "reason:", reason
print "*************************************"
ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def clientConnectionLost(self, connector, reason):
print "*************************************"
print "Connection Lost"
print "reason:", reason
print "*************************************"
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
if __name__ == '__main__':
## 0) start logging to console
log.startLogging(sys.stdout)
## 1) create a WAMP application session factory
component_config = types.ComponentConfig(realm = "realm1")
session_factory = wamp.ApplicationSessionFactory(config = component_config)
session_factory.session = MyFrontendComponent
## 2) create a WAMP-over-WebSocket transport client factory
transport_factory = MyClientFactory(session_factory)
## 3) start the client from a Twisted endpoint
transport_factory.host = 'localhost'
transport_factory.port = 8080
websocket.connectWS(transport_factory)
## 4) now enter the Twisted reactor loop
reactor.run()
@jjmontesl
Copy link

Thanks for this. Best example I've found. The reconnecting stuff I find critical.

@fleur
Copy link

fleur commented Jun 3, 2016

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment