Commit ba3961e7 authored by echel0n's avatar echel0n

Refactored websockets handler.

parent 4cb9f3bc
......@@ -184,7 +184,7 @@ class VersionUpdater(object):
sickrage.app.config.view_changelog = True
if webui:
WebSocketMessage('task', {'cmd': 'restart'}).push()
WebSocketMessage('redirect', {'url': '{}/home/restart/?pid={}'.format(sickrage.app.config.web_root, sickrage.app.pid)}).push()
return True
......
This source diff could not be displayed because it is too large. You can view the blob instead.
import json
from queue import Queue
from tornado.websocket import WebSocketHandler, WebSocketClosedError
from tornado.websocket import WebSocketHandler
import sickrage
clients = set()
message_queue = Queue()
class WebSocketUIHandler(WebSocketHandler):
......@@ -17,12 +19,8 @@ class WebSocketUIHandler(WebSocketHandler):
def open(self, *args, **kwargs):
"""Client connected to the WebSocket."""
clients.add(self)
for n in sickrage.app.alerts.get_notifications(self.request.remote_ip):
try:
self.write_message(WebSocketMessage('notification', n.data).json())
except WebSocketClosedError:
pass
while not message_queue.empty():
self.write_message(message_queue.get())
def on_message(self, message):
"""Received a message from the client."""
......@@ -44,20 +42,20 @@ class WebSocketUIHandler(WebSocketHandler):
class WebSocketMessage(object):
"""Represents a WebSocket message."""
def __init__(self, event, data):
def __init__(self, message_type, data):
"""
Construct a new WebSocket message.
:param event: A string representing the type of message (e.g. notification)
:param message_type: A string representing the type of message (e.g. notification)
:param data: A JSON-serializable object containing the message data.
"""
self.event = event
self.type = message_type
self.data = data
@property
def content(self):
"""Get the message content."""
return {
'event': self.event,
'type': self.type,
'data': self.data
}
......@@ -67,8 +65,6 @@ class WebSocketMessage(object):
def push(self):
"""Push the message to all connected WebSocket clients."""
if not clients:
return
message_queue.put(self.json())
for client in clients:
sickrage.app.io_loop.add_callback(client.write_message, self.json())
sickrage.app.io_loop.add_callback(client.write_message, message_queue.get())
......@@ -40,15 +40,13 @@ $(document).ready(function ($) {
ws.onmessage = function (evt) {
var msg = JSON.parse(evt.data);
// Add handling for different kinds of events. For ex: {"event": "notification", "data": {"title": ..}}
if (msg.event === 'notification') {
SICKRAGE.notify(msg.data.type, msg.data.title, msg.data.body);
} else if (msg.event === 'task') {
switch (msg.data.cmd) {
case 'restart':
window.location.href = SICKRAGE.srWebRoot + '/home/restart/?pid=' + SICKRAGE.srPID;
break;
}
switch (msg.type) {
case 'notification':
SICKRAGE.notify(msg.data.type, msg.data.title, msg.data.body);
break;
case 'redirect':
window.location.href = msg.data.url;
break;
}
};
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment