Skip to content

Instantly share code, notes, and snippets.

@jegger
Created March 22, 2015 16:21
Show Gist options
  • Save jegger/d82a1197bd81b0daa1b3 to your computer and use it in GitHub Desktop.
Save jegger/d82a1197bd81b0daa1b3 to your computer and use it in GitHub Desktop.
WAMP autobahn reconnect
# Copied from https://gist.github.com/DenJohX/e6d0864738da10cb9685 -> DenJohX
# Slightly modified
import sys
from twisted.python import log
from twisted.internet import reactor
from twisted.internet.protocol import ReconnectingClientFactory
from autobahn.websocket.protocol import parseWsUrl
from autobahn.twisted import wamp, websocket
from autobahn.wamp import types
class MyFrontendComponent(wamp.ApplicationSession):
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
url = u"ws://url.com:8080/ws"
transport_factory = MyClientFactory(session_factory, url=url, debug=False)
## 3) start the client from a Twisted endpoint
isSecure, host, port, resource, path, params = parseWsUrl(url)
transport_factory.host = host
transport_factory.port = port
websocket.connectWS(transport_factory)
## 4) now enter the Twisted reactor loop
reactor.run()
@petercsmith
Copy link

Any thoughts on how I'd incorporate 'ssl.optionsForClientTLS' into the above code to make it strictly check certificate and hostname? I'm using your code to connect via wss and it works perfectly, just can't get the cert validation working.

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