init app of test arena
This commit is contained in:
22
asf-cloud-server/testarena/backend/app/socket_manager.py
Normal file
22
asf-cloud-server/testarena/backend/app/socket_manager.py
Normal file
@@ -0,0 +1,22 @@
|
||||
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 broadcast(self, message: str):
|
||||
for connection in self.active_connections:
|
||||
try:
|
||||
await connection.send_text(message)
|
||||
except:
|
||||
self.disconnect(connection)
|
||||
|
||||
manager = ConnectionManager()
|
||||
Reference in New Issue
Block a user