44import json
55import logging
66from datetime import datetime
7- from typing import Any , TypeVar
7+ from typing import Any , TypeVar , Union
88
99from fastapi import APIRouter , WebSocket , WebSocketDisconnect
1010from sqlmodel import Session , select
2222
2323class ConnectionManager :
2424 def __init__ (self ):
25- self .active_connections : dict [int | str , WebSocket ] = {}
25+ self .active_connections : dict [Union [ int , str ] , WebSocket ] = {}
2626
2727 async def connect (
28- self , websocket : WebSocket , client_id : int | str , register_client : bool = True
28+ self ,
29+ websocket : WebSocket ,
30+ client_id : Union [int , str ],
31+ register_client : bool = True ,
2932 ):
3033 await websocket .accept ()
3134 self .active_connections [client_id ] = websocket
@@ -45,7 +48,7 @@ def _register_new_client(client_id: int):
4548 murfey_db .commit ()
4649 murfey_db .close ()
4750
48- def disconnect (self , client_id : int | str , unregister_client : bool = True ):
51+ def disconnect (self , client_id : Union [ int , str ] , unregister_client : bool = True ):
4952 self .active_connections .pop (client_id )
5053 if unregister_client :
5154 murfey_db : Session = next (get_murfey_db_session ())
@@ -94,7 +97,7 @@ async def websocket_endpoint(websocket: WebSocket, client_id: int):
9497@ws .websocket ("/connect/{client_id}" )
9598async def websocket_connection_endpoint (
9699 websocket : WebSocket ,
97- client_id : int | str ,
100+ client_id : Union [ int , str ] ,
98101):
99102 await manager .connect (websocket , client_id , register_client = False )
100103 await manager .broadcast (f"Client { client_id } joined" )
@@ -158,7 +161,7 @@ async def close_ws_connection(client_id: int):
158161
159162
160163@ws .delete ("/connect/{client_id}" )
161- async def close_unrecorded_ws_connection (client_id : int | str ):
164+ async def close_unrecorded_ws_connection (client_id : Union [ int , str ] ):
162165 client_id_str = str (client_id ).replace ("\r \n " , "" ).replace ("\n " , "" )
163166 log .info (f"Disconnecting { client_id_str } " )
164167 manager .disconnect (client_id )
0 commit comments