refactor: 初步完成单一ws重构

This commit is contained in:
DLmaster361
2025-08-27 17:50:56 +08:00
parent 4c2a6407a1
commit fbcc149849
10 changed files with 454 additions and 342 deletions

View File

@@ -23,6 +23,7 @@ __version__ = "5.0.0"
__author__ = "DLmaster361 <DLmaster_361@163.com>"
__license__ = "GPL-3.0 license"
from .core import router as core_router
from .info import router as info_router
from .scripts import router as scripts_router
from .plan import router as plan_router
@@ -32,6 +33,7 @@ from .history import router as history_router
from .setting import router as setting_router
__all__ = [
"core_router",
"info_router",
"scripts_router",
"plan_router",

47
app/api/core.py Normal file
View File

@@ -0,0 +1,47 @@
# AUTO_MAA:A MAA Multi Account Management and Automation Tool
# Copyright © 2024-2025 DLmaster361
# Copyright © 2025 MoeSnowyFox
# This file is part of AUTO_MAA.
# AUTO_MAA is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
# AUTO_MAA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with AUTO_MAA. If not, see <https://www.gnu.org/licenses/>.
# Contact: DLmaster_361@163.com
import asyncio
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from app.core import Config, Broadcast
from app.services import System
from app.models.schema import *
router = APIRouter(prefix="/api/core", tags=["核心信息"])
@router.websocket("/ws")
async def connect_websocket(websocket: WebSocket):
await websocket.accept()
Config.websocket = websocket
while True:
try:
data = await asyncio.wait_for(websocket.receive_json(), timeout=30.0)
await Broadcast.put(data)
except asyncio.TimeoutError:
await websocket.send_json(
WebSocketMessage(type="Signal", data={"Ping": "无描述"}).model_dump()
)
except WebSocketDisconnect:
break
await System.set_power("KillSelf")

View File

@@ -70,33 +70,3 @@ async def power_task(task: PowerIn = Body(...)) -> OutBase:
code=500, status="error", message=f"{type(e).__name__}: {str(e)}"
)
return OutBase()
@router.websocket("/ws/{websocketId}")
async def websocket_endpoint(
websocket: WebSocket,
websocketId: str = Path(..., description="要连接的WebSocket ID"),
):
await websocket.accept()
try:
uid = uuid.UUID(websocketId)
except ValueError:
await websocket.close(code=1008, reason="无效的WebSocket ID")
return
if uid in TaskManager.connection_events and uid not in TaskManager.websocket_dict:
TaskManager.websocket_dict[uid] = websocket
TaskManager.connection_events[uid].set()
while True:
try:
data = await asyncio.wait_for(websocket.receive_json(), timeout=30.0)
await Broadcast.put(data)
except asyncio.TimeoutError:
await websocket.send_json(
TaskMessage(type="Signal", data={"Ping": "无描述"}).model_dump()
)
except WebSocketDisconnect:
TaskManager.websocket_dict.pop(uid, None)
break
else:
await websocket.close(code=1008, reason="任务不存在或已结束")

View File

@@ -189,7 +189,7 @@ async def get_web_config() -> InfoOut:
@router.post(
"/get/overview", summary="信息总览", response_model=InfoOut, status_code=200
)
async def add_overview() -> InfoOut:
async def get_overview() -> InfoOut:
try:
stage = await Config.get_stage_info("Info")
proxy = await Config.get_proxy_overview()