diff --git a/VERSION b/VERSION index 38f77a6..f93ea0c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.0.1 +2.0.2 \ No newline at end of file diff --git a/speechmatics/client.py b/speechmatics/client.py index c32eb16..614218f 100644 --- a/speechmatics/client.py +++ b/speechmatics/client.py @@ -38,6 +38,7 @@ logging.getLogger("websockets.protocol").setLevel(logging.INFO) HIDDEN_MSG_PREFIX = "Hidden_" + class WebsocketClient: """ Manage a transcription session with the server. @@ -275,24 +276,27 @@ async def _consumer_handler(self): raise ex self._consumer(message) - # async def _request_spkrids(self, interval = 5): - # LOGGER.warning("waiting for recognisnier") - # await self._recognition_started.wait() - # LOGGER.warning("done waiting for recognisnier started") - - # while self.session_running: - # await asyncio.sleep(interval) - # assert self.websocket - # await self.websocket.send(json.dumps({"message": "Hidden_GetSpeakerIds"})) - # # self._call_middleware({"message": "Hidden_GetSpeakerIds"}) - async def _send_message(self, msg): - # LOGGER.warning("waiting for recognisnier") - # await self._recognition_started.wait() - # LOGGER.warning("done waiting for recognisnier started") + """ + Sends a message to the server. A dict/json like object is expected as the msg param. + """ if self.session_running: - assert self.websocket - await self.websocket.send(json.dumps(msg)) + assert self.websocket + try: + await self.websocket.send(json.dumps(msg)) + except TypeError as ex: + LOGGER.info( + f"Cannot send this type of object {msg=} as a message. Exception occured:%s", + repr(ex), + ) + return + except websockets.exceptions.ConnectionClosedOK: + # Can occur if a timeout has closed the connection. + LOGGER.info("Cannot send from a closed websocket.") + return + except websockets.exceptions.ConnectionClosedError: + LOGGER.info("Disconnected while sending a message().") + return async def _producer_handler(self, stream, audio_chunk_size): """ @@ -363,9 +367,10 @@ def add_event_handler(self, event_name, event_handler): :raises ValueError: If the given event name is not valid. """ - if event_name.startswith( - HIDDEN_MSG_PREFIX - )and event_name not in self.event_handlers: + if ( + event_name.startswith(HIDDEN_MSG_PREFIX) + and event_name not in self.event_handlers + ): self.event_handlers[event_name] = [] if event_name == "all": for name in self.event_handlers.keys(): @@ -400,9 +405,10 @@ def add_middleware(self, event_name, middleware): :raises ValueError: If the given event name is not valid. """ - if event_name.startswith( - HIDDEN_MSG_PREFIX - ) and event_name not in self.middlewares: + if ( + event_name.startswith(HIDDEN_MSG_PREFIX) + and event_name not in self.middlewares + ): self.middlewares[event_name] = [] if event_name == "all": for name in self.middlewares.keys(): @@ -430,8 +436,6 @@ async def _communicate(self, stream, audio_settings): await self.websocket.send(start_recognition_msg) consumer_task = asyncio.create_task(self._consumer_handler()) - # request_speaker_ids_task = asyncio.create_task(self._request_spkrids()) - producer_task = asyncio.create_task( self._producer_handler(stream, audio_settings.chunk_size) )