Only catch OSError exceptions instead of all.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Thu, 9 Jul 2020 21:29:02 +0000 (23:29 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Thu, 9 Jul 2020 21:29:02 +0000 (23:29 +0200)
lagarde.py

index eca29c9f60512771d8543bb212964d561c99ce7e..f984354f3fdd7ceb9aa5ea5b7ef9bf2fede2f76f 100755 (executable)
@@ -175,44 +175,47 @@ class Lagarde:
             self.pipe.set_state(Gst.State.NULL)
 
     async def talk_to_websocket(self, uri):
+        async for msg in self.websocket:
+            msg_json = json.loads(msg)
+            msg_type = msg_json['Type']
+            msg_value = msg_json['Value']
+            assert self.session_id is None or self.session_id == msg_json['SessionID']
+            if msg_type == 'newSession':
+                self.session_id = msg_json['SessionID']
+                log.info(f"New session {self.session_id}")
+            elif msg_type == 'gotOffer':
+                value_json = json.loads(msg_value)
+                sdp = value_json['sdp']
+                log.info(f'Got SDP offer:\n{sdp}')
+                self.sdp_offer = sdp
+            elif msg_type == 'addCallerIceCandidate':
+                value_json = json.loads(msg_value)
+                log.info(f'Got ICE candidate: {value_json}')
+                self.received_ice_candidates.append(value_json)
+            elif msg_type == 'roomClosed':
+                log.info(f'Oh noes, the room went away (session {self.session_id})!')
+                self.session_id = None
+                return
+            else:
+                log.error(f'Unknown message type {msg_type}')
+
+    async def run(self, uri):
         ssl_context = ssl.SSLContext()
         ssl_context.check_hostname = False
         ssl_context.verify_mode = ssl.CERT_NONE
-        async with websockets.connect(uri, ssl=ssl_context) as self.websocket:
-            async for msg in self.websocket:
-                msg_json = json.loads(msg)
-                msg_type = msg_json['Type']
-                msg_value = msg_json['Value']
-                assert self.session_id is None or self.session_id == msg_json['SessionID']
-                if msg_type == 'newSession':
-                    self.session_id = msg_json['SessionID']
-                    log.info(f"New session {self.session_id}")
-                elif msg_type == 'gotOffer':
-                    value_json = json.loads(msg_value)
-                    sdp = value_json['sdp']
-                    log.info(f'Got SDP offer:\n{sdp}')
-                    self.sdp_offer = sdp
-                elif msg_type == 'addCallerIceCandidate':
-                    value_json = json.loads(msg_value)
-                    log.info(f'Got ICE candidate: {value_json}')
-                    self.received_ice_candidates.append(value_json)
-                elif msg_type == 'roomClosed':
-                    log.info(f'Oh noes, the room went away (session {self.session_id})!')
-                    self.session_id = None
-                    return
-                else:
-                    log.error(f'Unknown message type {msg_type}')
-
-    async def run(self, uri):
-        talk_to_websocket_task = asyncio.Task(self.talk_to_websocket(uri))
-        listen_to_gstreamer_bus_task = asyncio.Task(self.listen_to_gstreamer_bus())
-        done, pending = await asyncio.wait(
-            [talk_to_websocket_task, listen_to_gstreamer_bus_task],
-            return_when=asyncio.FIRST_COMPLETED)
-        for d in done:
-            d.result()
-        for p in pending:
-            p.cancel()
+        try:
+            async with websockets.connect(uri, ssl=ssl_context) as self.websocket:
+                talk_to_websocket_task = asyncio.Task(self.talk_to_websocket(uri))
+                listen_to_gstreamer_bus_task = asyncio.Task(self.listen_to_gstreamer_bus())
+                done, pending = await asyncio.wait(
+                    [talk_to_websocket_task, listen_to_gstreamer_bus_task],
+                    return_when=asyncio.FIRST_COMPLETED)
+                for d in done:
+                    d.result()
+                for p in pending:
+                    p.cancel()
+        except OSError as e:
+            print(e)
 
 
 def main():