add banbanmini backend
This commit is contained in:
29
talkingq-url/services/connection_manager.py
Normal file
29
talkingq-url/services/connection_manager.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import asyncio
|
||||
from typing import Dict, Optional
|
||||
from fastapi import WebSocket
|
||||
|
||||
|
||||
class ConnectionManager:
|
||||
def __init__(self):
|
||||
self.connections: Dict[str, WebSocket] = {}
|
||||
self.lock = asyncio.Lock()
|
||||
|
||||
async def add_connection(self, device_id: str, websocket: WebSocket):
|
||||
async with self.lock:
|
||||
self.connections[device_id] = websocket
|
||||
|
||||
async def remove_connection(self, device_id: str):
|
||||
async with self.lock:
|
||||
if device_id in self.connections:
|
||||
del self.connections[device_id]
|
||||
|
||||
async def get_connection(self, device_id: str) -> Optional[WebSocket]:
|
||||
async with self.lock:
|
||||
return self.connections.get(device_id)
|
||||
|
||||
async def get_all_connections(self) -> Dict[str, WebSocket]:
|
||||
async with self.lock:
|
||||
return dict(self.connections)
|
||||
|
||||
|
||||
connection_manager = ConnectionManager()
|
||||
Reference in New Issue
Block a user