-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
60 lines (43 loc) · 1.57 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
import json
import tornado
import tornado.web
import tornado.websocket
from tornado.log import gen_log
class FeedHandler(tornado.web.RequestHandler):
def get(self):
self.render('templates/subscriber.html')
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
class FeedWebSocketHandler(tornado.websocket.WebSocketHandler):
subscribed_channels = None
def check_origin(self, origin):
return True
def open(self, *args, **kwargs):
gen_log.debug("SDFSDFSDS")
self.application.pika_connector.add_subscriber(self)
gen_log.info("WebSocket opened")
def on_close(self):
gen_log.info("WebSocket closed")
self.application.pika_connector.remove_subscriber(self)
def has_subscribed_to(self, collection_name):
if self.subscribed_channels is None:
return True
else:
return collection_name in self.subscribed_channels
def subscribe(self, collection_name):
if self.subscribed_channels is None:
self.subscribed_channels = []
if collection_name not in self.subscribed_channels:
self.subscribed_channels.append(collection_name)
def unsubscribe(self, collection_name):
if self.subscribed_channels:
if collection_name in self.subscribed_channels:
self.subscribed_channels.remove(collection_name)
def update_subscriptions(self, collection_list):
self.subscribed_channels = collection_list
def on_message(self, message):
message_dict = json.loads(message)
if message_dict.get('subscribed_channels', None):
self.update_subscriptions(message_dict['subscribed_channels'])
gen_log.info("message: {}".format(message))