目前似乎没有太多关于如何将WSS支持正确添加到高速公路/扭曲设置的内容。我从Crossbar serial2ws示例开始,该示例显示了前端和后端之间基于WS的连接。
我想知道如何使serial2ws example适应SSL连接。
我改变了:
# serial2ws.py
router = args.router or 'ws://localhost:8080'
to
router = args.router or 'wss://localhost:8080'
在JS网站上:
connection = new autobahn.Connection({
url: (document.location.protocol === "http:" ? "ws:" : "wss:") + "//" + ip + ":" + port,
realm: 'realm1',
...
})
但是,当我尝试连接时,它失败了:
WebSocket connection to 'wss://192.168.0.12:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED
Python服务器记录:
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>
要明确的是,当上面的“wss”实例恢复到原来的“ws”时,一切正常。
添加到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)