11from __future__ import annotations
22
3- import asyncio
43import json
54import logging
65from datetime import datetime
98from fastapi import APIRouter , WebSocket , WebSocketDisconnect
109from sqlmodel import Session , select
1110
12- import murfey .server .prometheus as prom
1311from murfey .server .murfey_db import get_murfey_db_session
1412from murfey .util import sanitise
1513from murfey .util .db import ClientEnvironment
@@ -69,31 +67,6 @@ async def broadcast(self, message: str):
6967manager = ConnectionManager ()
7068
7169
72- @ws .websocket ("/test/{client_id}" )
73- async def websocket_endpoint (websocket : WebSocket , client_id : int ):
74- await manager .connect (websocket , client_id )
75- await manager .broadcast (f"Client { client_id } joined" )
76- try :
77- while True :
78- data = await websocket .receive_text ()
79- try :
80- json_data : dict = json .loads (data )
81- if json_data ["type" ] == "log" : # and isinstance(json_data, dict)
82- json_data .pop ("type" )
83- await forward_log (json_data , websocket )
84- except Exception :
85- await manager .broadcast (f"Client #{ client_id } sent message { data } " )
86- except WebSocketDisconnect :
87- log .info (f"Disconnecting Client { int (sanitise (str (client_id )))} " )
88- murfey_db = next (get_murfey_db_session ())
89- client_env = murfey_db .exec (
90- select (ClientEnvironment ).where (ClientEnvironment .client_id == client_id )
91- ).one ()
92- prom .monitoring_switch .labels (visit = client_env .visit ).set (0 )
93- manager .disconnect (client_id )
94- await manager .broadcast (f"Client #{ client_id } disconnected" )
95-
96-
9770@ws .websocket ("/connect/{client_id}" )
9871async def websocket_connection_endpoint (
9972 websocket : WebSocket ,
@@ -120,17 +93,6 @@ async def websocket_connection_endpoint(
12093 await manager .broadcast (f"Client #{ client_id } disconnected" )
12194
12295
123- async def check_connections (active_connections : list [WebSocket ]):
124- log .info ("Checking connections" )
125- for connection in active_connections :
126- log .info ("Checking response" )
127- try :
128- await asyncio .wait_for (connection .receive (), timeout = 10 )
129- except asyncio .TimeoutError :
130- log .info (f"Disconnecting Client { connection [0 ]} " )
131- manager .disconnect (connection [0 ], connection [1 ])
132-
133-
13496async def forward_log (logrecord : dict [str , Any ], websocket : WebSocket ):
13597 record_name = logrecord ["name" ]
13698 logrecord .pop ("msecs" , None )
@@ -142,26 +104,8 @@ async def forward_log(logrecord: dict[str, Any], websocket: WebSocket):
142104 logging .getLogger (record_name ).handle (logging .makeLogRecord (logrecord ))
143105
144106
145- @ws .delete ("/test/{client_id}" )
146- async def close_ws_connection (client_id : int ):
147- murfey_db : Session = next (get_murfey_db_session ())
148- client_env = murfey_db .exec (
149- select (ClientEnvironment ).where (ClientEnvironment .client_id == client_id )
150- ).one ()
151- client_env .connected = False
152- visit_name = client_env .visit
153- murfey_db .add (client_env )
154- murfey_db .commit ()
155- murfey_db .close ()
156- client_id_str = str (client_id ).replace ("\r \n " , "" ).replace ("\n " , "" )
157- log .info (f"Disconnecting { client_id_str } " )
158- manager .disconnect (client_id )
159- prom .monitoring_switch .labels (visit = visit_name ).set (0 )
160- await manager .broadcast (f"Client #{ client_id } disconnected" )
161-
162-
163107@ws .delete ("/connect/{client_id}" )
164- async def close_unrecorded_ws_connection (client_id : int | str ):
108+ async def close_websocket_connection (client_id : int | str ):
165109 client_id_str = str (client_id ).replace ("\r \n " , "" ).replace ("\n " , "" )
166110 log .info (f"Disconnecting { client_id_str } " )
167111 manager .disconnect (client_id )
0 commit comments