Improve websocket stability

This commit is contained in:
tomkat83 2016-03-29 19:16:08 +02:00
parent dc7755ba6d
commit 9037f65318

View file

@ -16,17 +16,6 @@ import utils
############################################################################### ###############################################################################
TIMELINE_STATES = {
0: 'created',
2: 'matching',
3: 'downloading',
4: 'loading',
5: 'finished',
6: 'analyzing',
9: 'deleted'
}
@utils.logging @utils.logging
@utils.ThreadMethods @utils.ThreadMethods
class WebSocket(threading.Thread): class WebSocket(threading.Thread):
@ -83,20 +72,17 @@ class WebSocket(threading.Thread):
return frame.opcode, None return frame.opcode, None
elif frame.opcode == websocket.ABNF.OPCODE_PING: elif frame.opcode == websocket.ABNF.OPCODE_PING:
ws.pong("Hi!") ws.pong("Hi!")
return None, None return None, None
def getUri(self): def getUri(self):
server = utils.window('pms_server') server = utils.window('pms_server')
# Need to use plex.tv token, if any. NOT user token # Need to use plex.tv token, if any. NOT user token
token = utils.window('plex_token') token = utils.window('plex_token')
# Get the appropriate prefix for the websocket # Get the appropriate prefix for the websocket
if "https" in server: if "https" in server:
server = server.replace('https', "wss") server = server.replace('https', "wss")
else: else:
server = server.replace('http', "ws") server = server.replace('http', "ws")
uri = "%s/:/websockets/notifications" % server uri = "%s/:/websockets/notifications" % server
if token: if token:
uri += '?X-Plex-Token=%s' % token uri += '?X-Plex-Token=%s' % token
@ -142,6 +128,11 @@ class WebSocket(threading.Thread):
xbmc.sleep(1000) xbmc.sleep(1000)
except Exception as e: except Exception as e:
log("Unknown exception encountered: %s" % e) log("Unknown exception encountered: %s" % e)
try:
self.ws.shutdown()
except:
pass
self.ws = None
pass pass
log("##===---- WebSocketClient Stopped ----===##", 0) log("##===---- WebSocketClient Stopped ----===##", 0)