Reafctor app
This commit is contained in:
parent
b8216f6ade
commit
7740be8bb5
36 changed files with 389 additions and 208 deletions
19
app/util/websocket_wrapper.py
Normal file
19
app/util/websocket_wrapper.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
from fastapi import WebSocket, WebSocketDisconnect
|
||||
from websockets import ConnectionClosed
|
||||
|
||||
# Wrapper to transform a FastAPI websocket to a standard websocket
|
||||
class WebSocketWrapper():
|
||||
def __init__(self, websocket: WebSocket):
|
||||
self._websocket = websocket
|
||||
|
||||
async def recv(self) -> str:
|
||||
try:
|
||||
return await self._websocket.receive_text()
|
||||
except WebSocketDisconnect as e:
|
||||
raise ConnectionClosed(e.code, 'WebSocketWrapper')
|
||||
|
||||
async def send(self, msg: str) -> None:
|
||||
await self._websocket.send_text(msg)
|
||||
|
||||
async def close(self, code: int, reason: str) -> None:
|
||||
await self._websocket.close(code)
|
Loading…
Add table
Add a link
Reference in a new issue