Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix websocket automatic reconnection #812

Merged
merged 2 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 4 additions & 12 deletions jellyfin_kodi/jellyfin/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def __init__(self, client):

self.client = client
threading.Thread.__init__(self)
self.retry_count = 0

def send(self, message, data=""):

Expand All @@ -62,23 +61,18 @@ def run(self):

while not self.stop:

time.sleep(self.retry_count * 5)
self.wsc.run_forever(ping_interval=10)
self.wsc.run_forever(ping_interval=10, reconnect=10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the main functional change in this PR is the addition of the reconnect parameter here.

Could you elaborate on the reasoning behind removing the back-off delay?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reconnect param makes the reconnect logic handled by the websocket library. I added logging to the backoff delay and found it wasn't getting called anymore so I removed it for clarity.


if not self.stop and monitor.waitForAbort(5):
break

# Wait a maximum of 60 seconds before retrying connection
if self.retry_count < 12:
self.retry_count += 1

LOG.info("---<[ websocket ]")

def on_error(self, ws, error):
LOG.error(error)

def on_open(self, ws):
LOG.info("--->[ websocket ]")
LOG.info("--->[ websocket opened ]")
# Avoid a timing issue where the capabilities are not correctly registered
time.sleep(5)
if settings('remoteControl.bool'):
self.client.jellyfin.post_capabilities({
'PlayableMediaTypes': "Audio,Video",
Expand All @@ -99,8 +93,6 @@ def on_open(self, ws):
"PlayableMediaTypes": "Audio, Video",
"SupportsMediaControl": False
})
# Reinitialize the retry counter after successful connection
self.retry_count = 0

def on_message(self, ws, message):

Expand Down
2 changes: 1 addition & 1 deletion release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ dependencies:
- addon: 'script.module.addon.signals'
version: '0.0.5+matrix.1'
- addon: 'script.module.websocket'
version: '0.58.0+matrix.1'
version: '1.6.4'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you verified that this version of the websocket library actually exists in the Kodi repositories for the supported versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this version is available for Matrix, Nexus and Omega

2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pytest >= 4.6.11
coverage >= 5.2
flake8 >= 3.8
flake8-import-order >= 0.18
websocket-client >= 0.57.0
websocket-client >= 1.6.4

types-requests >= 2.31
types-PyYAML >= 6.0
Expand Down