@@ -192,32 +192,37 @@ def _is_connection_closed(self) -> bool:
192192 if self ._ws_connection is None :
193193 return True
194194
195- # Try different ways to check connection status for compatibility
196195 try :
197- # For websockets library (newer versions)
198- if hasattr (self ._ws_connection , 'closed' ):
199- return self ._ws_connection .closed
200-
201- # For ClientConnection or other connection types
202- if hasattr (self ._ws_connection , 'state' ):
203- # Check if state indicates closed
204- state = self ._ws_connection .state
205- if hasattr (state , 'name' ):
206- return state .name in ('CLOSED' , 'CLOSING' )
207- # For numeric states
208- return state in (3 , 4 ) # Common closed/closing state values
209-
210- # Fallback: try to access connection state indirectly
211- if hasattr (self ._ws_connection , 'close_code' ):
212- return self ._ws_connection .close_code is not None
213-
214- # Last resort: assume open if we can't determine state
215- return False
216-
196+ return self ._check_connection_state ()
217197 except Exception :
218198 # If any error occurs while checking state, assume closed
219199 return True
220200
201+ def _check_connection_state (self ) -> bool :
202+ """Check connection state using various methods for compatibility."""
203+ # For websockets library (newer versions)
204+ if hasattr (self ._ws_connection , 'closed' ):
205+ return self ._ws_connection .closed
206+
207+ # For ClientConnection or other connection types
208+ if hasattr (self ._ws_connection , 'state' ):
209+ return self ._check_state_attribute ()
210+
211+ # Fallback: try to access connection state indirectly
212+ if hasattr (self ._ws_connection , 'close_code' ):
213+ return self ._ws_connection .close_code is not None
214+
215+ # Last resort: assume open if we can't determine state
216+ return False
217+
218+ def _check_state_attribute (self ) -> bool :
219+ """Check connection state attribute."""
220+ state = self ._ws_connection .state
221+ if hasattr (state , 'name' ):
222+ return state .name in {'CLOSED' , 'CLOSING' }
223+ # For numeric states
224+ return state in {3 , 4 } # Common closed/closing state values
225+
221226 async def _receive_events (self ):
222227 """Main loop for receiving and processing WebSocket messages."""
223228 try :
0 commit comments