Added SSL to HTTP backend websockets as well

This commit is contained in:
Fabio Manganiello 2018-11-01 23:57:50 +01:00
parent a253e5f49e
commit 727b604bb4

View file

@ -380,18 +380,26 @@ class HttpBackend(Backend):
import websockets import websockets
async def register_websocket(websocket, path): async def register_websocket(websocket, path):
self.logger.info('New websocket connection from {}'.format(websocket.remote_address[0])) address = websocket.remote_address[0] if websocket.remote_address \
else '<unknown client>'
self.logger.info('New websocket connection from {}'.format(address))
self.active_websockets.add(websocket) self.active_websockets.add(websocket)
try: try:
await websocket.recv() await websocket.recv()
except websockets.exceptions.ConnectionClosed: except websockets.exceptions.ConnectionClosed:
self.logger.info('Websocket client {} closed connection'.format(websocket.remote_address[0])) self.logger.info('Websocket client {} closed connection'.format(address))
self.active_websockets.remove(websocket) self.active_websockets.remove(websocket)
websocket_args = {}
if self.ssl_context:
websocket_args['ssl'] = self.ssl_context
loop = get_or_create_event_loop() loop = get_or_create_event_loop()
loop.run_until_complete( loop.run_until_complete(
websockets.serve(register_websocket, '0.0.0.0', self.websocket_port)) websockets.serve(register_websocket, '0.0.0.0', self.websocket_port,
**websocket_args))
loop.run_forever() loop.run_forever()