fix:客户端断开连接时,服务端处理websocket异常

This commit is contained in:
zy7y
2022-09-17 19:10:11 +08:00
parent 8e37bba724
commit 85a38a0840
6 changed files with 20 additions and 22 deletions

View File

@@ -1,4 +1,9 @@
import asyncio
from starlette.websockets import WebSocket
from core.security import generate_token, verify_password
from core.utils import get_system_info
from dbhelper.user import get_user
from schemas import LoginForm, LoginResult, Response
@@ -18,3 +23,14 @@ async def login(auth_data: LoginForm) -> Response[LoginResult]:
async def about() -> Response:
"""关于"""
pass
async def websocket(ws: WebSocket):
await ws.accept()
try:
while True:
await asyncio.sleep(1)
await ws.send_json(get_system_info())
except Exception as e:
print("断开了链接", e)
await ws.close()

View File

@@ -1,14 +0,0 @@
from fastapi import FastAPI
from fastapi.websockets import WebSocket
from core.utils import get_system_info
# websocket app
ws_app = FastAPI()
@ws_app.websocket("/ws")
async def ws_func(ws: WebSocket):
await ws.accept()
while True:
await ws.send_json(get_system_info())