@@ -53,11 +53,10 @@ class AsyncListenWebSocketClient: # pylint: disable=too-many-instance-attribute
5353 _socket : WebSocketClientProtocol
5454 _event_handlers : Dict [LiveTranscriptionEvents , list ]
5555
56- _last_datagram : Optional [datetime ] = None
57-
5856 _listen_thread : Union [asyncio .Task , None ]
5957 _keep_alive_thread : Union [asyncio .Task , None ]
6058 _flush_thread : Union [asyncio .Task , None ]
59+ _last_datagram : Optional [datetime ] = None
6160
6261 _kwargs : Optional [Dict ] = None
6362 _addons : Optional [Dict ] = None
@@ -79,11 +78,10 @@ def __init__(self, config: DeepgramClientOptions):
7978 self ._keep_alive_thread = None
8079 self ._flush_thread = None
8180
82- # exit
81+ # events
8382 self ._exit_event = asyncio .Event ()
8483
85- # auto flush
86- self ._flush_event = asyncio .Event ()
84+ # init handlers
8785 self ._event_handlers = {
8886 event : [] for event in LiveTranscriptionEvents .__members__ .values ()
8987 }
@@ -174,7 +172,7 @@ async def start(
174172 self ._logger .notice ("keepalive is disabled" )
175173
176174 # flush thread
177- if self ._config .is_auto_flush_enabled ():
175+ if self ._config .is_auto_flush_reply_enabled ():
178176 self ._logger .notice ("autoflush is enabled" )
179177 self ._flush_thread = asyncio .create_task (self ._flush ())
180178 else :
@@ -219,7 +217,7 @@ async def start(
219217 raise
220218 return False
221219
222- def is_connected (self ) -> bool :
220+ async def is_connected (self ) -> bool :
223221 """
224222 Returns the connection status of the WebSocket.
225223 """
@@ -311,7 +309,7 @@ async def _listening(self) -> None:
311309 self ._logger .verbose ("LiveResultResponse: %s" , msg_result )
312310
313311 # auto flush
314- if self ._config .is_inspecting_messages ():
312+ if self ._config .is_inspecting_listen ():
315313 inspect_res = await self ._inspect (msg_result )
316314 if not inspect_res :
317315 self ._logger .error ("inspect_res failed" )
@@ -400,6 +398,8 @@ async def _listening(self) -> None:
400398 self ._logger .debug ("AsyncListenWebSocketClient._listening LEAVE" )
401399 return
402400
401+ # we need to explicitly call self._signal_exit() here because we are hanging on a recv()
402+ # note: this is different than the speak websocket client
403403 self ._logger .error (
404404 "ConnectionClosed in AsyncListenWebSocketClient._listening with code %s: %s" ,
405405 e .code ,
@@ -508,11 +508,13 @@ async def _keep_alive(self) -> None:
508508 return
509509
510510 except websockets .exceptions .ConnectionClosed as e :
511- if e .code == 1000 :
511+ if e .code in [ 1000 , 1001 ] :
512512 self ._logger .notice (f"_keep_alive({ e .code } ) exiting gracefully" )
513513 self ._logger .debug ("AsyncListenWebSocketClient._keep_alive LEAVE" )
514514 return
515515
516+ # we need to explicitly call self._signal_exit() here because we are hanging on a recv()
517+ # note: this is different than the speak websocket client
516518 self ._logger .error (
517519 "ConnectionClosed in AsyncListenWebSocketClient._keep_alive with code %s: %s" ,
518520 e .code ,
@@ -635,11 +637,13 @@ async def _flush(self) -> None:
635637 return
636638
637639 except websockets .exceptions .ConnectionClosed as e :
638- if e .code == 1000 :
640+ if e .code in [ 1000 , 1001 ] :
639641 self ._logger .notice (f"_flush({ e .code } ) exiting gracefully" )
640642 self ._logger .debug ("AsyncListenWebSocketClient._flush LEAVE" )
641643 return
642644
645+ # we need to explicitly call self._signal_exit() here because we are hanging on a recv()
646+ # note: this is different than the speak websocket client
643647 self ._logger .error (
644648 "ConnectionClosed in AsyncListenWebSocketClient._flush with code %s: %s" ,
645649 e .code ,
@@ -731,6 +735,11 @@ async def send(self, data: Union[str, bytes]) -> bool:
731735 self ._logger .debug ("AsyncListenWebSocketClient.send LEAVE" )
732736 return False
733737
738+ if not await self .is_connected ():
739+ self ._logger .notice ("is_connected is False" )
740+ self ._logger .debug ("AsyncListenWebSocketClient.send LEAVE" )
741+ return False
742+
734743 if self ._socket is not None :
735744 try :
736745 await self ._socket .send (data )
@@ -741,7 +750,7 @@ async def send(self, data: Union[str, bytes]) -> bool:
741750 raise
742751 return True
743752 except websockets .exceptions .ConnectionClosed as e :
744- if e .code == 1000 :
753+ if e .code in [ 1000 , 1001 ] :
745754 self ._logger .notice (f"send({ e .code } ) exiting gracefully" )
746755 self ._logger .debug ("AsyncListenWebSocketClient.send LEAVE" )
747756 if self ._config .options .get ("termination_exception_send" ) == "true" :
@@ -897,7 +906,7 @@ async def _signal_exit(self) -> None:
897906 except websockets .exceptions .ConnectionClosedOK as e :
898907 self ._logger .notice ("_signal_exit - ConnectionClosedOK: %s" , e .code )
899908 except websockets .exceptions .ConnectionClosed as e :
900- self ._logger .notice ("_signal_exit - ConnectionClosed: %s" , e .code )
909+ self ._logger .error ("_signal_exit - ConnectionClosed: %s" , e .code )
901910 except websockets .exceptions .WebSocketException as e :
902911 self ._logger .error ("_signal_exit - WebSocketException: %s" , str (e ))
903912 except Exception as e : # pylint: disable=broad-except
@@ -931,6 +940,11 @@ async def _signal_exit(self) -> None:
931940 self ._socket = None # type: ignore
932941
933942 async def _inspect (self , msg_result : LiveResultResponse ) -> bool :
943+ # auto flush_inspect is generically used to track any messages you might want to snoop on
944+ # place additional logic here to inspect messages of interest
945+
946+ # for auto flush functionality
947+ # set the last datagram
934948 sentence = msg_result .channel .alternatives [0 ].transcript
935949 if len (sentence ) == 0 :
936950 return True
0 commit comments