mirror of
https://github.com/jellyfin/jellyfin-kodi.git
synced 2024-12-25 18:26:15 +00:00
Merge pull request #598 from mcarlton00/reconnect-websockets
Attempt to re-establishe websocket communication if the connection fails
This commit is contained in:
commit
018a9eec22
2 changed files with 28 additions and 23 deletions
|
@ -5,6 +5,7 @@ from __future__ import division, absolute_import, print_function, unicode_litera
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
from kodi_six import xbmc
|
from kodi_six import xbmc
|
||||||
|
|
||||||
|
@ -34,6 +35,7 @@ class WSClient(threading.Thread):
|
||||||
|
|
||||||
self.client = client
|
self.client = client
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
self.retry_count = 0
|
||||||
|
|
||||||
def send(self, message, data=""):
|
def send(self, message, data=""):
|
||||||
|
|
||||||
|
@ -48,23 +50,28 @@ class WSClient(threading.Thread):
|
||||||
token = self.client.config.data['auth.token']
|
token = self.client.config.data['auth.token']
|
||||||
device_id = self.client.config.data['app.device_id']
|
device_id = self.client.config.data['app.device_id']
|
||||||
server = self.client.config.data['auth.server']
|
server = self.client.config.data['auth.server']
|
||||||
server = server.replace('https', "wss") if server.startswith('https') else server.replace('http', "ws")
|
server = server.replace('https://', 'wss://') if server.startswith('https') else server.replace('http://', 'ws://')
|
||||||
wsc_url = "%s/socket?api_key=%s&device_id=%s" % (server, token, device_id)
|
wsc_url = "%s/socket?api_key=%s&device_id=%s" % (server, token, device_id)
|
||||||
|
|
||||||
LOG.info("Websocket url: %s", wsc_url)
|
LOG.info("Websocket url: %s", wsc_url)
|
||||||
|
|
||||||
self.wsc = websocket.WebSocketApp(wsc_url,
|
self.wsc = websocket.WebSocketApp(wsc_url,
|
||||||
|
on_open=lambda ws: self.on_open(ws),
|
||||||
on_message=lambda ws, message: self.on_message(ws, message),
|
on_message=lambda ws, message: self.on_message(ws, message),
|
||||||
on_error=lambda ws, error: self.on_error(ws, error))
|
on_error=lambda ws, error: self.on_error(ws, error))
|
||||||
self.wsc.on_open = self.on_open
|
|
||||||
|
|
||||||
while not self.stop:
|
while not self.stop:
|
||||||
|
|
||||||
|
time.sleep(self.retry_count * 5)
|
||||||
self.wsc.run_forever(ping_interval=10)
|
self.wsc.run_forever(ping_interval=10)
|
||||||
|
|
||||||
if not self.stop and monitor.waitForAbort(5):
|
if not self.stop and monitor.waitForAbort(5):
|
||||||
break
|
break
|
||||||
|
|
||||||
|
# Wait a maximum of 60 seconds before retrying connection
|
||||||
|
if self.retry_count < 12:
|
||||||
|
self.retry_count += 1
|
||||||
|
|
||||||
LOG.info("---<[ websocket ]")
|
LOG.info("---<[ websocket ]")
|
||||||
|
|
||||||
def on_error(self, ws, error):
|
def on_error(self, ws, error):
|
||||||
|
@ -72,6 +79,23 @@ class WSClient(threading.Thread):
|
||||||
|
|
||||||
def on_open(self, ws):
|
def on_open(self, ws):
|
||||||
LOG.info("--->[ websocket ]")
|
LOG.info("--->[ websocket ]")
|
||||||
|
self.client.jellyfin.post_capabilities({
|
||||||
|
'PlayableMediaTypes': "Audio,Video",
|
||||||
|
'SupportsMediaControl': True,
|
||||||
|
'SupportedCommands': (
|
||||||
|
"MoveUp,MoveDown,MoveLeft,MoveRight,Select,"
|
||||||
|
"Back,ToggleContextMenu,ToggleFullscreen,ToggleOsdMenu,"
|
||||||
|
"GoHome,PageUp,NextLetter,GoToSearch,"
|
||||||
|
"GoToSettings,PageDown,PreviousLetter,TakeScreenshot,"
|
||||||
|
"VolumeUp,VolumeDown,ToggleMute,SendString,DisplayMessage,"
|
||||||
|
"SetAudioStreamIndex,SetSubtitleStreamIndex,"
|
||||||
|
"SetRepeatMode,"
|
||||||
|
"Mute,Unmute,SetVolume,"
|
||||||
|
"Play,Playstate,PlayNext,PlayMediaSource"
|
||||||
|
),
|
||||||
|
})
|
||||||
|
# Reinitialize the retry counter after successful connection
|
||||||
|
self.retry_count = 0
|
||||||
|
|
||||||
def on_message(self, ws, message):
|
def on_message(self, ws, message):
|
||||||
|
|
||||||
|
|
|
@ -160,7 +160,8 @@ class Monitor(xbmc.Monitor):
|
||||||
def server_instance(self, server_id=None):
|
def server_instance(self, server_id=None):
|
||||||
|
|
||||||
server = Jellyfin(server_id).get_client()
|
server = Jellyfin(server_id).get_client()
|
||||||
self.post_capabilities(server)
|
session = server.jellyfin.get_device(self.device_id)
|
||||||
|
server.config.data['app.session'] = session[0]['Id']
|
||||||
|
|
||||||
if server_id is not None:
|
if server_id is not None:
|
||||||
self.servers.append(server_id)
|
self.servers.append(server_id)
|
||||||
|
@ -177,26 +178,6 @@ class Monitor(xbmc.Monitor):
|
||||||
|
|
||||||
self.additional_users(server)
|
self.additional_users(server)
|
||||||
|
|
||||||
def post_capabilities(self, server):
|
|
||||||
LOG.info("--[ post capabilities/%s ]", server.auth.server_id)
|
|
||||||
|
|
||||||
server.jellyfin.post_capabilities({
|
|
||||||
'PlayableMediaTypes': "Audio,Video",
|
|
||||||
'SupportsMediaControl': True,
|
|
||||||
'SupportedCommands': (
|
|
||||||
"MoveUp,MoveDown,MoveLeft,MoveRight,Select,"
|
|
||||||
"Back,ToggleContextMenu,ToggleFullscreen,ToggleOsdMenu,"
|
|
||||||
"GoHome,PageUp,NextLetter,GoToSearch,"
|
|
||||||
"GoToSettings,PageDown,PreviousLetter,TakeScreenshot,"
|
|
||||||
"VolumeUp,VolumeDown,ToggleMute,SendString,DisplayMessage,"
|
|
||||||
"SetAudioStreamIndex,SetSubtitleStreamIndex,"
|
|
||||||
"SetRepeatMode,"
|
|
||||||
"Mute,Unmute,SetVolume,"
|
|
||||||
"Play,Playstate,PlayNext,PlayMediaSource"
|
|
||||||
),
|
|
||||||
})
|
|
||||||
session = server.jellyfin.get_device(self.device_id)
|
|
||||||
server.config.data['app.session'] = session[0]['Id']
|
|
||||||
|
|
||||||
def additional_users(self, server):
|
def additional_users(self, server):
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue