99from .backend import Backend
1010
1111backend_var : ContextVar [Backend ] = ContextVar ("backend" )
12- endpoint_var : ContextVar [str ] = ContextVar ("endpoint" )
1312
1413api = Blueprint ("api" , url_prefix = "/" )
1514
@@ -45,7 +44,7 @@ async def status_handler(request: Request):
4544async def socket_send (request : Request , connectionid : str ):
4645 """Send a message to a connected socket."""
4746 LOGGER .info ("Inbound message for %s" , connectionid )
48- LOGGER .info ("Existing connections: %s" , active_connections .keys ())
47+ LOGGER .debug ("Existing connections: %s" , active_connections .keys ())
4948
5049 if connectionid not in active_connections :
5150 return text ("FAIL" , status = 500 )
@@ -62,7 +61,7 @@ async def socket_send(request: Request, connectionid: str):
6261async def socket_disconnect (request : Request , connectionid : str ):
6362 """Disconnect a socket."""
6463 LOGGER .info ("Disconnect %s" , connectionid )
65- LOGGER .info ("Existing connections: %s" , active_connections .keys ())
64+ LOGGER .debug ("Existing connections: %s" , active_connections .keys ())
6665
6766 if connectionid not in active_connections :
6867 return text ("FAIL" , status = 500 )
@@ -78,37 +77,26 @@ async def socket_handler(request: Request, websocket: Websocket):
7877 global lifetime_connections
7978 backend = backend_var .get ()
8079 socket_id = None
81- endpoint = endpoint_var .get ()
82- send = f"{ endpoint } /socket/{ socket_id } /send"
83- disconnect = f"{ endpoint_var .get ()} /socket/{ socket_id } /disconnect"
8480 try :
8581 # register user
8682 LOGGER .info ("new client connected" )
87- socket_id = websocket .connection .id .hex
83+ socket_id = websocket .ws_proto .id .hex
8884 active_connections [socket_id ] = websocket
8985 lifetime_connections += 1
90- LOGGER .info ("Existing connections: %s" , active_connections .keys ())
91- LOGGER .info ("Added connection: %s" , socket_id )
92- LOGGER .info ("Request headers: %s" , dict (request .headers .items ()))
86+ LOGGER .debug ("Existing connections: %s" , active_connections .keys ())
87+ LOGGER .debug ("Added connection: %s" , socket_id )
88+ LOGGER .debug ("Request headers: %s" , dict (request .headers .items ()))
9389
9490 await backend .socket_connected (
95- {
96- "connection_id" : socket_id ,
97- "headers" : dict (request .headers .items ()),
98- "send" : send ,
99- "disconnect" : disconnect ,
100- },
91+ connection_id = socket_id ,
92+ headers = dict (request .headers .items ()),
10193 )
10294
10395 async for message in websocket :
10496 if message :
10597 await backend .inbound_socket_message (
106- {
107- "connection_id" : socket_id ,
108- "send" : send ,
109- "disconnect" : disconnect ,
110- },
111- message ,
98+ connection_id = socket_id ,
99+ message = message ,
112100 )
113101 else :
114102 LOGGER .warning ("empty message received" )
@@ -118,4 +106,4 @@ async def socket_handler(request: Request, websocket: Websocket):
118106 if socket_id :
119107 del active_connections [socket_id ]
120108 LOGGER .info ("Removed connection: %s" , socket_id )
121- await backend .socket_disconnected ({ "connection_id" : socket_id } )
109+ await backend .socket_disconnected (socket_id )
0 commit comments