问题
There doesn't seem to be much out there right now on how to properly add WSS support to an Autobahn/Twisted setup. I'm starting with the Crossbar serial2ws example, which shows a WS-based connection between frontend and backend.
I'd like to know how to adapt the serial2ws example for an SSL connection.
I changed:
# serial2ws.py
router = args.router or 'ws://localhost:8080'
to
router = args.router or 'wss://localhost:8080'
And on the website JS:
connection = new autobahn.Connection({
url: (document.location.protocol === "http:" ? "ws:" : "wss:") + "//" + ip + ":" + port,
realm: 'realm1',
...
})
But, when I try to connect, it fails with:
WebSocket connection to 'wss://192.168.0.12:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED
The Python server logs:
2016-06-30 16:52:57-0400 [-] Log opened.
2016-06-30 16:52:57-0400 [-] Using Twisted reactor <class
'twisted.internet.epollreactor.EPollReactor'>
2016-06-30 16:52:59-0400 [-] WampWebSocketServerFactory starting on 8080
2016-06-30 16:52:59-0400 [-] Starting factory <autobahn.twisted.websocket.WampWebSocketServerFactory instance at 0x76669dc8>
2016-06-30 16:53:00-0400 [-] Starting factory <autobahn.twisted.websocket.WampWebSocketClientFactory instance at 0x766112b0>
2016-06-30 16:53:05-0400 [WampWebSocketClientProtocol (TLSMemoryBIOProtocol),client] Stopping factory <autobahn.twisted.websocket.WampWebSocketClientFactory instance at 0x766112b0>
To be clear, when the "wss" instances above are reverted to the original "ws", everything works.
Also tried:
Adding to serial2ws.py:
contextFactory = ssl.DefaultOpenSSLContextFactory('/root/keys/server.key', '/root/keys/server.crt')
# Change
reactor.listenTCP(args.web, Site(File(".")))
# to
reactor.listenSSL(args.web, Site(File(".")), contextFactory)
来源:https://stackoverflow.com/questions/38133690/adding-wss-to-websocket-autobahn-wamp-twisted