model
This commit is contained in:
29
main.py
29
main.py
@@ -1,7 +1,8 @@
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Request, Depends, HTTPException
|
||||
from fastapi.responses import HTMLResponse
|
||||
import ConnectionManager
|
||||
from ObjectManager import ConnectionManager, TaskManager, ServerManager, Task
|
||||
from typing import List
|
||||
|
||||
|
||||
'''
|
||||
@@ -55,8 +56,8 @@ app = FastAPI()
|
||||
async def get():
|
||||
return HTMLResponse(html)
|
||||
|
||||
manager = ConnectionManager.ConnectionManager()
|
||||
|
||||
connection_manager = ConnectionManager()
|
||||
task_manager = TaskManager()
|
||||
|
||||
# 接收客户端的websocket连接
|
||||
@app.websocket("/ws/{client_id}")
|
||||
@@ -67,16 +68,17 @@ async def websocket_endpoint(websocket: WebSocket, client_id: int):
|
||||
# if token is None:
|
||||
# raise HTTPException(status_code=401, detail="Unauthorized")
|
||||
|
||||
await manager.connect(websocket=websocket, client_id=client_id)
|
||||
await connection_manager.connect(websocket=websocket, client_id=client_id)
|
||||
try:
|
||||
while True:
|
||||
data = await websocket.receive_text()
|
||||
await manager.send_personal_message(f"You wrote: {data}", client_id=client_id)
|
||||
await manager.broadcast(f"Client #{client_id} says: {data}")
|
||||
await connection_manager.send_personal_message(f"You wrote: {data}", client_id=client_id)
|
||||
await connection_manager.broadcast(f"Client #{client_id} says: {data}")
|
||||
# TODO: 处理客户端的请求变化(理论上并没有)
|
||||
except WebSocketDisconnect:
|
||||
manager.disconnect(websocket)
|
||||
await manager.broadcast(f"Client #{client_id} left the chat")
|
||||
connection_manager.disconnect(client_id=client_id)
|
||||
await connection_manager.broadcast(f"Client #{client_id} left the chat")
|
||||
|
||||
|
||||
# 通知所有的ws客户端
|
||||
@app.post("/notify")
|
||||
@@ -84,5 +86,16 @@ async def notify():
|
||||
pass
|
||||
|
||||
|
||||
@app.get("/tasks", response_model=List[Task])
|
||||
async def get_tasks():
|
||||
return task_manager.query_all()
|
||||
|
||||
|
||||
@app.post("/tasks", response_model=Task)
|
||||
async def create_task():
|
||||
return task_manager.add({
|
||||
"name": "test",
|
||||
})
|
||||
|
||||
# 维护一个任务队列, 任务队列中的任务会被分发给worker节点
|
||||
# 任务状态变化时通知对应的客户端
|
||||
|
Reference in New Issue
Block a user