cleanup SSL handling
This commit is contained in:
@@ -847,7 +847,7 @@ async def ws_serve(websocket, path):
|
||||
|
||||
ws_connections[websocket] = path
|
||||
remote_address = websocket.remote_address
|
||||
if verbose: print(f"DBG ws_serve: {remote_address}")
|
||||
if verbose: print(f"DBG ws_serve: {remote_address}: {path}")
|
||||
while True:
|
||||
try:
|
||||
name = await websocket.recv()
|
||||
@@ -866,10 +866,11 @@ async def ws_serve(websocket, path):
|
||||
await websocket.send(jmsg)
|
||||
|
||||
if verbose: print(f"DBG ws_serve: close {remote_address}")
|
||||
try:
|
||||
del ws_connections[websocket]
|
||||
except Exception as e:
|
||||
print(f"warning: failed to delete websocket: {e}")
|
||||
await websocket.wait_closed()
|
||||
# try:
|
||||
# del ws_connections[websocket]
|
||||
# except Exception as e:
|
||||
# print(f"warning: failed to delete websocket: {e}")
|
||||
|
||||
def websocketupdater():
|
||||
loop.run_forever()
|
||||
@@ -879,15 +880,19 @@ def msg_to_websockets(typ: str, msg: str):
|
||||
jmsg = json.dumps({'type': typ, 'data': msg})
|
||||
to_close = []
|
||||
for ws in ws_connections:
|
||||
if ws.closed:
|
||||
to_close.append(ws)
|
||||
continue
|
||||
try:
|
||||
asyncio.run(ws.send(jmsg))
|
||||
asyncio.run_coroutine_threadsafe(ws.send(jmsg), loop)
|
||||
except Exception:
|
||||
to_close.append(ws)
|
||||
print("ws.send exception: closed")
|
||||
|
||||
for ws in to_close:
|
||||
ws.close()
|
||||
del ws_connections[ws]
|
||||
asyncio.run_coroutine_threadsafe(ws.wait_closed(), loop)
|
||||
if ws in ws_connections:
|
||||
del ws_connections[ws]
|
||||
#
|
||||
# Main
|
||||
#
|
||||
@@ -1119,19 +1124,20 @@ except:
|
||||
print(("failed to start server on %s:%s" % (hbd_host, hbd_port)))
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
#
|
||||
loop=asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
||||
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
wss_pem = pathlib.Path(WSS_PEM)
|
||||
wss_key = pathlib.Path(WSS_KEY)
|
||||
|
||||
ssl_context.load_cert_chain(wss_pem, keyfile=wss_key)
|
||||
wss_start_server = websockets.serve(ws_serve, hbd_host, WSSPORT, ssl=ssl_context)
|
||||
wss_start_server = websockets.serve(ws_serve, hbd_host, WSSPORT,
|
||||
ssl=ssl_context, loop=loop, subprotocols="hbd")
|
||||
loop.run_until_complete(wss_start_server)
|
||||
|
||||
ws_start_server = websockets.serve(ws_serve, hbd_host, WSPORT, loop = loop)
|
||||
|
||||
ws_start_server = websockets.serve(ws_serve, hbd_host, WSPORT,
|
||||
loop = loop, subprotocols="hbd")
|
||||
loop.run_until_complete(ws_start_server)
|
||||
|
||||
servthread = threading.Thread(target=serv.serve_forever)
|
||||
|
||||
Reference in New Issue
Block a user