aboutsummaryrefslogtreecommitdiffstats
path: root/deckit/obsclient.py
diff options
context:
space:
mode:
Diffstat (limited to 'deckit/obsclient.py')
-rw-r--r--deckit/obsclient.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/deckit/obsclient.py b/deckit/obsclient.py
index 181225a..87eefff 100644
--- a/deckit/obsclient.py
+++ b/deckit/obsclient.py
@@ -30,18 +30,31 @@ class ObsClient:
self._stop = threading.Event()
self._thread = None
+ def _recv_json(self) -> dict:
+ raw = self.ws.recv()
+ if not raw:
+ raise ModuleError("OBS closed the connection")
+ try:
+ return json.loads(raw)
+ except ValueError:
+ raise ModuleError("OBS sent a non-JSON reply")
+
def connect(self):
import websocket # only needed when actually talking to OBS
self.ws = websocket.create_connection(f"ws://{self.host}:{self.port}",
timeout=5, suppress_origin=True)
- hello = json.loads(self.ws.recv())
+ hello = self._recv_json()
auth = {}
if hello.get("d", {}).get("authentication"):
a = hello["d"]["authentication"]
auth["authentication"] = auth_string(self.password, a["salt"], a["challenge"])
self.ws.send(json.dumps({"op": 1, "d": {"rpcVersion": 1, **auth, "eventSubscriptions": 0xFFFFFFFF}}))
- if json.loads(self.ws.recv()).get("op") != 2:
+ try:
+ identified = self._recv_json()
+ except ModuleError:
+ raise ModuleError("OBS rejected the password" if auth else "OBS did not identify")
+ if identified.get("op") != 2:
raise ModuleError("OBS did not identify")
self.connected = True
self._stop.clear()