mirror of
https://github.com/nexy7574/LCC-bot.git
synced 2024-09-19 18:16:34 +01:00
Fix websocket lock
This commit is contained in:
parent
f79247928c
commit
b673459f19
1 changed files with 13 additions and 3 deletions
|
@ -292,23 +292,33 @@ async def bridge(req: Request):
|
|||
@app.websocket("/bridge/recv")
|
||||
async def bridge_recv(ws: WebSocket, secret: str = Header(None)):
|
||||
await ws.accept()
|
||||
print("Websocket %r accepted.", ws)
|
||||
if secret != app.state.bot.http.token:
|
||||
print("Closing websocket %r, invalid secret.", ws)
|
||||
raise _WSException(code=1008, reason="Invalid Secret")
|
||||
if app.state.ws_connected.locked():
|
||||
print("Closing websocket %r, already connected.", ws)
|
||||
raise _WSException(code=1008, reason="Already connected.")
|
||||
queue: asyncio.Queue = app.state.bot.bridge_queue
|
||||
|
||||
async with app.state.ws_connected:
|
||||
while True:
|
||||
try:
|
||||
data = queue.get_nowait()
|
||||
except asyncio.QueueEmpty:
|
||||
await asyncio.sleep(0.5)
|
||||
await ws.send_json({"status": "ping"})
|
||||
except (WebSocketDisconnect, WebSocketException):
|
||||
print("Websocket %r disconnected.", ws)
|
||||
break
|
||||
|
||||
try:
|
||||
data = await asyncio.wait_for(queue.get(), timeout=5)
|
||||
except asyncio.TimeoutError:
|
||||
continue
|
||||
|
||||
try:
|
||||
print("Sent data %r to websocket %r.", data, ws)
|
||||
await ws.send_json(data)
|
||||
except (WebSocketDisconnect, WebSocketException):
|
||||
print("Websocket %r disconnected.", ws)
|
||||
break
|
||||
finally:
|
||||
queue.task_done()
|
||||
|
|
Loading…
Reference in a new issue