feat: webscoket
This commit is contained in:
parent
29c6e368ae
commit
8da0127729
14
backend/controller/ws.py
Normal file
14
backend/controller/ws.py
Normal file
@ -0,0 +1,14 @@
|
||||
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())
|
@ -1,3 +1,7 @@
|
||||
import random
|
||||
import time
|
||||
|
||||
|
||||
def list_to_tree(
|
||||
menus, parent_flag: str = "pid", children_key: str = "children"
|
||||
) -> list:
|
||||
@ -24,3 +28,15 @@ def list_to_tree(
|
||||
else:
|
||||
arr.append(menu)
|
||||
return arr
|
||||
|
||||
|
||||
def get_system_info():
|
||||
"""获取系统信息"""
|
||||
time.sleep(1)
|
||||
return {
|
||||
"usage": {
|
||||
"cpu": f"{random.random() * 100: .2}",
|
||||
"memory": f"{random.random() * 100: .2}",
|
||||
"disk": f"{random.random() * 100: .2}",
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from controller.ws import ws_app
|
||||
from core.events import close_orm, init_orm
|
||||
from core.exceptions import exception_handlers
|
||||
from core.log import logger
|
||||
@ -14,6 +15,8 @@ app = FastAPI(
|
||||
exception_handlers=exception_handlers,
|
||||
)
|
||||
|
||||
app.mount("/", ws_app)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
|
BIN
backend/mini.db-shm
Normal file
BIN
backend/mini.db-shm
Normal file
Binary file not shown.
0
backend/mini.db-wal
Normal file
0
backend/mini.db-wal
Normal file
Loading…
Reference in New Issue
Block a user