-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
60 lines (50 loc) · 1.82 KB
/
monitor.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 tornado.websocket
import tornado.gen
import tornadoredis
import tornado.httpserver
import tornado.web
import tornado.ioloop
import tornadoredis
import os
from config import redis_config, redis_channel
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("app/templates/index.html")
class MessageHandler(tornado.websocket.WebSocketHandler):
def __init__(self, *args, **kwargs):
super(MessageHandler, self).__init__(*args, **kwargs)
self.listen()
@tornado.gen.engine
def listen(self):
self.client = tornadoredis.Client(**redis_config)
self.client.connect()
yield tornado.gen.Task(self.client.subscribe, redis_channel)
self.client.listen(self.on_message)
def on_message(self, msg):
if type(msg) == unicode:
import pdb; pdb.set_trace()
if msg.kind == 'message':
self.write_message(str(msg.body))
if msg.kind == 'disconnect':
# Do not try to reconnect, just send a message back
# to the client and close the client connection
self.write_message('The connection terminated '
'due to a Redis server error.')
self.close()
def on_close(self):
if self.client.subscribed:
self.client.unsubscribe('test_channel')
self.client.disconnect()
settings = {
'debug': True,
}
static_path = os.path.join(os.path.dirname(__file__), 'app/assets')
application = tornado.web.Application([
(r'/websocket', MessageHandler),
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': static_path}),
(r'/', MainHandler)
], **settings)
if __name__ == '__main__':
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(1235)
tornado.ioloop.IOLoop.instance().start()