24 lines
631 B
Python
24 lines
631 B
Python
from fastapi import APIRouter
|
|
from starlette.websockets import WebSocket, WebSocketDisconnect
|
|
|
|
from db.postgres.ws_manager import ConnectionManager
|
|
|
|
ws_manager = ConnectionManager()
|
|
|
|
|
|
iot_ws_router = APIRouter()
|
|
|
|
|
|
@iot_ws_router.websocket("/device-status")
|
|
async def websocket_device_status(websocket: WebSocket):
|
|
await ws_manager.connect(websocket)
|
|
print("[WS] client connected")
|
|
|
|
try:
|
|
while True:
|
|
# 这里不需要接收任何消息
|
|
await websocket.receive_text()
|
|
except WebSocketDisconnect:
|
|
await ws_manager.disconnect(websocket)
|
|
print("[WS] client disconnected")
|