@@ -140,32 +140,30 @@ def _on_connect(self, client: Dict[Text, Any], server: WebsocketServer) -> None:
140
140
141
141
# Send ready notification to client
142
142
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 )
143
147
except Exception as e :
144
148
logger .error (f"Failed to initialize client { client_id } : { e } " )
145
-
146
- # Send close notification to client
147
- self .send (client_id , "CLOSE" )
148
-
149
149
# Close audio source and remove client
150
150
self .close (client_id )
151
+ # Send close notification to client
152
+ self .send (client_id , "CLOSE" )
151
153
152
154
def _on_disconnect (self , client : Dict [Text , Any ], server : WebsocketServer ) -> None :
153
- """Handle client disconnection .
155
+ """Cleanup client state when a connection is closed .
154
156
155
157
Parameters
156
158
----------
157
159
client : Dict[Text, Any]
158
- Client information dictionary
160
+ Client metadata
159
161
server : WebsocketServer
160
- WebSocket server instance
162
+ Server instance
161
163
"""
162
164
client_id = client ["id" ]
163
165
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
169
167
self .close (client_id )
170
168
171
169
def _on_message_received (
@@ -191,17 +189,12 @@ def _on_message_received(
191
189
self ._clients [client_id ].audio_source .process_message (message )
192
190
except (socket .error , ConnectionError ) as e :
193
191
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
199
193
self .close (client_id )
200
-
201
194
except Exception as e :
202
- logger .error (f"Error processing message from client { client_id } : { e } " )
203
195
# Don't close the connection for non-connection related errors
204
196
# This allows the client to retry sending the message
197
+ logger .error (f"Error processing message from client { client_id } : { e } " )
205
198
206
199
def send (self , client_id : Text , message : AnyStr ) -> None :
207
200
"""Send a message to a specific client.
@@ -224,9 +217,10 @@ def send(self, client_id: Text, message: AnyStr) -> None:
224
217
self .server .send_message (client , message )
225
218
except Exception as e :
226
219
logger .error (f"Failed to send message to client { client_id } : { e } " )
220
+ raise
227
221
228
222
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 .
230
224
231
225
Parameters
232
226
----------
@@ -245,12 +239,10 @@ def close(self, client_id: Text) -> None:
245
239
client_state .audio_source .close ()
246
240
del self ._clients [client_id ]
247
241
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 } " )
251
243
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
254
246
self ._clients .pop (client_id , None )
255
247
256
248
def close_all (self ) -> None :
@@ -260,7 +252,6 @@ def close_all(self) -> None:
260
252
for client_id in self ._clients .keys ():
261
253
# Close audio source and remove client
262
254
self .close (client_id )
263
-
264
255
# Send close notification to client
265
256
self .send (client_id , "CLOSE" )
266
257
@@ -282,12 +273,10 @@ def run(self) -> None:
282
273
self .server .run_forever ()
283
274
break # If server exits normally, break the retry loop
284
275
except (socket .error , ConnectionError ) as e :
285
- logger .warning (f"WebSocket connection error: { e } " )
276
+ logger .warning (f"WebSocket server connection error: { e } " )
286
277
retry_count += 1
287
278
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 } )" )
291
280
else :
292
281
logger .error ("Max retry attempts reached. Server shutting down." )
293
282
except Exception as e :
0 commit comments