websocket send structured data
This commit is contained in:
parent
7da91b8aae
commit
93f67504c9
1 changed files with 10 additions and 3 deletions
|
@ -1,4 +1,5 @@
|
||||||
import secrets
|
import secrets
|
||||||
|
import logging
|
||||||
import dbm
|
import dbm
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
@ -9,8 +10,8 @@ from fastapi import (
|
||||||
HTTPException,
|
HTTPException,
|
||||||
Depends,
|
Depends,
|
||||||
status,
|
status,
|
||||||
WebSocketDisconnect,
|
|
||||||
)
|
)
|
||||||
|
from starlette.websockets import WebSocketDisconnect
|
||||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
@ -136,8 +137,14 @@ async def websocket_counter(websocket: WebSocket, cid: int):
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
await websocket.send_text(str(counter_store.get(cid)))
|
val = counter_store.get(cid)
|
||||||
except:
|
await websocket.send_json(Value(counter=cid, value=val).json())
|
||||||
|
except WebSocketDisconnect:
|
||||||
|
logging.debug("client disconnected")
|
||||||
|
manager.unsubscribe(cid, q)
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
logging.exception("unexpected error")
|
||||||
manager.unsubscribe(cid, q)
|
manager.unsubscribe(cid, q)
|
||||||
return
|
return
|
||||||
await q.get()
|
await q.get()
|
||||||
|
|
Loading…
Reference in a new issue