From c18515bd3c68fb353e34a4cab51e11e258c8f800 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 12 Feb 2019 22:57:09 +0100 Subject: [PATCH] Few more fixes on mopidy backend --- platypush/backend/music/mopidy.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/platypush/backend/music/mopidy.py b/platypush/backend/music/mopidy.py index 539c2d4e4..4356b493e 100644 --- a/platypush/backend/music/mopidy.py +++ b/platypush/backend/music/mopidy.py @@ -87,7 +87,9 @@ class MusicMopidyBackend(Backend): ws = websocket.create_connection(self.url) ws.send(msg) - return json.loads(ws.recv()).get('result') + response = json.loads(ws.recv()).get('result') + ws.close() + return response def _get_tracklist_status(self): @@ -102,21 +104,6 @@ class MusicMopidyBackend(Backend): 'method': 'core.tracklist.get_consume'}), } - async def _poll_events(self): - import websocket - - try: - while not self.should_stop(): - async with websockets.connect(self.url) as ws: - msg = await ws.recv() - if isinstance(msg, bytes): - msg = msg.decode() - self._handle_msg(msg) - except Exception as e: - self.logger.warning('The Mopidy backend raised an exception') - self.logger.exception(e) - time.sleep(2) # Wait a bit before retrying - def _on_msg(self): def hndl(msg): msg = json.loads(msg) @@ -210,6 +197,8 @@ class MusicMopidyBackend(Backend): return hndl def _connect(self): + import websocket + if not self._ws: self._ws = websocket.WebSocketApp(self.url, on_message=self._on_msg(),