添加websocket工具类

This commit is contained in:
qiangyanwen 2022-12-06 14:59:56 +08:00
parent accd6b6511
commit 7e504f13b1
2 changed files with 62 additions and 0 deletions

View File

@ -78,3 +78,31 @@
2022-12-06 11:46:42 | MainThread:4530525632 | create_app:app:39 - INFO - Server address http://192.168.9.93:8000
2022-12-06 11:46:42 | MainThread:4530525632 | create_app:app:40 - INFO - Api doc address http://192.168.9.93:8000/docs
2022-12-06 11:46:42 | MainThread:4530525632 | create_app:app:41 - INFO - Api redoc address http://192.168.9.93:8000/redoc
2022-12-06 13:46:33 | MainThread:4458747328 | <module>:__init__:16 - INFO - start the automation service development environment
2022-12-06 13:46:33 | MainThread:4458747328 | <module>:__init__:22 - INFO - loading environment configuration file
2022-12-06 13:46:33 | MainThread:4458747328 | create_app:app:28 - INFO - loading application configuration
2022-12-06 13:46:33 | MainThread:4458747328 | create_app:app:29 - INFO - create FastApi app object
2022-12-06 13:46:33 | MainThread:4458747328 | loading_router_middleware:app:16 - INFO - Adding a New route success
2022-12-06 13:46:33 | MainThread:4458747328 | loading_router_middleware:app:24 - INFO - loading middleware success
2022-12-06 13:46:33 | MainThread:4458747328 | create_app:app:38 - INFO - Application started successfully:CORSMiddleware
2022-12-06 13:46:33 | MainThread:4458747328 | create_app:app:39 - INFO - Server address http://192.168.9.93:8000
2022-12-06 13:46:33 | MainThread:4458747328 | create_app:app:40 - INFO - Api doc address http://192.168.9.93:8000/docs
2022-12-06 13:46:33 | MainThread:4458747328 | create_app:app:41 - INFO - Api redoc address http://192.168.9.93:8000/redoc
2022-12-06 13:46:33 | MainThread:4801131968 | <module>:__init__:16 - INFO - start the automation service development environment
2022-12-06 13:46:33 | MainThread:4801131968 | <module>:__init__:22 - INFO - loading environment configuration file
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:28 - INFO - loading application configuration
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:29 - INFO - create FastApi app object
2022-12-06 13:46:34 | MainThread:4801131968 | loading_router_middleware:app:16 - INFO - Adding a New route success
2022-12-06 13:46:34 | MainThread:4801131968 | loading_router_middleware:app:24 - INFO - loading middleware success
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:38 - INFO - Application started successfully:CORSMiddleware
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:39 - INFO - Server address http://192.168.9.93:8000
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:40 - INFO - Api doc address http://192.168.9.93:8000/docs
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:41 - INFO - Api redoc address http://192.168.9.93:8000/redoc
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:28 - INFO - loading application configuration
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:29 - INFO - create FastApi app object
2022-12-06 13:46:34 | MainThread:4801131968 | loading_router_middleware:app:16 - INFO - Adding a New route success
2022-12-06 13:46:34 | MainThread:4801131968 | loading_router_middleware:app:24 - INFO - loading middleware success
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:38 - INFO - Application started successfully:CORSMiddleware
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:39 - INFO - Server address http://192.168.9.93:8000
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:40 - INFO - Api doc address http://192.168.9.93:8000/docs
2022-12-06 13:46:34 | MainThread:4801131968 | create_app:app:41 - INFO - Api redoc address http://192.168.9.93:8000/redoc

View File

@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name websocket_manager
Description :
Author : qiangyanwen
date 2022/12/6
-------------------------------------------------
"""
from fastapi import WebSocket
from typing import List
class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def send_message(self, message: str, websocket: WebSocket):
await websocket.send_text(message)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
manager = ConnectionManager()