__init__.py 2.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import json

from tornado.websocket import WebSocketHandler

import sickrage

clients = set()


class WebSocketUIHandler(WebSocketHandler):
    """WebSocket handler to send and receive data to and from a web client."""

    def check_origin(self, origin):
        """Allow alternate origins."""
        return True

    def open(self, *args, **kwargs):
        """Client connected to the WebSocket."""
        clients.add(self)

echel0n's avatar
echel0n committed
21
22
        #for n in sickrage.app.alerts.get_notifications(self.request.remote_ip):
        #    self.write_message(WebSocketMessage('notification', n.data).json())
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

    def on_message(self, message):
        """Received a message from the client."""
        sickrage.app.log.debug('WebSocket received message from {}: {}'.format(self.request.remote_ip, message))

    def data_received(self, chunk):
        """Received a streamed data chunk from the client."""
        super(WebSocketUIHandler, self).data_received(chunk)

    def on_close(self):
        """Client disconnected from the WebSocket."""
        clients.remove(self)

    def __repr__(self):
        """Client representation."""
        return '<{} Client: {}>'.format(type(self).__name__, self.request.remote_ip)


class WebSocketMessage(object):
    """Represents a WebSocket message."""

    def __init__(self, event, data):
        """
        Construct a new WebSocket message.
        :param event: A string representing the type of message (e.g. notification)
        :param data: A JSON-serializable object containing the message data.
        """
        self.event = event
        self.data = data

    @property
    def content(self):
        """Get the message content."""
        return {
            'event': self.event,
            'data': self.data
        }

61
62
63
64
    def json(self):
        """Return the message content as a JSON-serialized string."""
        return json.dumps(self.content)

65
66
67
68
69
70
    def push(self):
        """Push the message to all connected WebSocket clients."""
        if not clients:
            return

        for client in clients:
71
            sickrage.app.io_loop.add_callback(client.write_message, self.json())