@@ -140,32 +140,30 @@ def _on_connect(self, client: Dict[Text, Any], server: WebsocketServer) -> None:
140140
141141 # Send ready notification to client
142142 self .send (client_id , "READY" )
143+ except (socket .error , ConnectionError ) as e :
144+ logger .warning (f"Client { client_id } connection failed: { e } " )
145+ # Just cleanup since client is already disconnected
146+ self .close (client_id )
143147 except Exception as e :
144148 logger .error (f"Failed to initialize client { client_id } : { e } " )
145-
146- # Send close notification to client
147- self .send (client_id , "CLOSE" )
148-
149149 # Close audio source and remove client
150150 self .close (client_id )
151+ # Send close notification to client
152+ self .send (client_id , "CLOSE" )
151153
152154 def _on_disconnect (self , client : Dict [Text , Any ], server : WebsocketServer ) -> None :
153- """Handle client disconnection .
155+ """Cleanup client state when a connection is closed .
154156
155157 Parameters
156158 ----------
157159 client : Dict[Text, Any]
158- Client information dictionary
160+ Client metadata
159161 server : WebsocketServer
160- WebSocket server instance
162+ Server instance
161163 """
162164 client_id = client ["id" ]
163165 logger .info (f"Client disconnected: { client_id } " )
164-
165- # Send close notification to client
166- self .send (client_id , "CLOSE" )
167-
168- # Close audio source and remove client
166+ # Just cleanup resources, no need to send CLOSE as client is already disconnected
169167 self .close (client_id )
170168
171169 def _on_message_received (
@@ -191,17 +189,12 @@ def _on_message_received(
191189 self ._clients [client_id ].audio_source .process_message (message )
192190 except (socket .error , ConnectionError ) as e :
193191 logger .warning (f"Client { client_id } disconnected: { e } " )
194-
195- # Send close notification to client
196- self .send (client_id , "CLOSE" )
197-
198- # Close audio source and remove client
192+ # Just cleanup since client is already disconnected
199193 self .close (client_id )
200-
201194 except Exception as e :
202- logger .error (f"Error processing message from client { client_id } : { e } " )
203195 # Don't close the connection for non-connection related errors
204196 # This allows the client to retry sending the message
197+ logger .error (f"Error processing message from client { client_id } : { e } " )
205198
206199 def send (self , client_id : Text , message : AnyStr ) -> None :
207200 """Send a message to a specific client.
@@ -224,9 +217,10 @@ def send(self, client_id: Text, message: AnyStr) -> None:
224217 self .server .send_message (client , message )
225218 except Exception as e :
226219 logger .error (f"Failed to send message to client { client_id } : { e } " )
220+ raise
227221
228222 def close (self , client_id : Text ) -> None :
229- """Close a specific client's connection and cleanup resources .
223+ """Close and cleanup resources for a specific client .
230224
231225 Parameters
232226 ----------
@@ -245,12 +239,10 @@ def close(self, client_id: Text) -> None:
245239 client_state .audio_source .close ()
246240 del self ._clients [client_id ]
247241
248- logger .info (
249- f"Closed connection and cleaned up state for client: { client_id } "
250- )
242+ logger .info (f"Cleaned up resources for client: { client_id } " )
251243 except Exception as e :
252- logger .error (f"Error closing client { client_id } : { e } " )
253- # Ensure client is removed from dictionary even if cleanup fails
244+ logger .error (f"Error cleaning up resources for client { client_id } : { e } " )
245+ # Ensure client is removed even if cleanup fails
254246 self ._clients .pop (client_id , None )
255247
256248 def close_all (self ) -> None :
@@ -260,7 +252,6 @@ def close_all(self) -> None:
260252 for client_id in self ._clients .keys ():
261253 # Close audio source and remove client
262254 self .close (client_id )
263-
264255 # Send close notification to client
265256 self .send (client_id , "CLOSE" )
266257
@@ -282,12 +273,10 @@ def run(self) -> None:
282273 self .server .run_forever ()
283274 break # If server exits normally, break the retry loop
284275 except (socket .error , ConnectionError ) as e :
285- logger .warning (f"WebSocket connection error: { e } " )
276+ logger .warning (f"WebSocket server connection error: { e } " )
286277 retry_count += 1
287278 if retry_count < max_retries :
288- logger .info (
289- f"Attempting to restart server (attempt { retry_count + 1 } /{ max_retries } )"
290- )
279+ logger .info (f"Attempting to restart server (attempt { retry_count + 1 } /{ max_retries } )" )
291280 else :
292281 logger .error ("Max retry attempts reached. Server shutting down." )
293282 except Exception as e :
0 commit comments