-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_tornado_redis.py
104 lines (86 loc) · 2.65 KB
/
websocket_tornado_redis.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import threading
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import redis
import pprint
# This is ugly but I did not want to create multiple files for a so trivial
# example.
TEMPLATE = """
<!DOCTYPE>
<html>
<head>
<title>Sample test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
</head>
<body>
<h1>Hello world</h1>
<form method='POST' action='./'>
<textarea name='data' id="data"></textarea>
<div><input type='submit'></div>
</form>
<div id="log"></div>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('form').submit(function(event){
var value = $('#data').val();
$.post("./", { data: value }, function(data){
$("#data").val('');
});
return false;
});
if ("WebSocket" in window) {
var ws = new WebSocket("ws://192.168.19.128:8080/realtime/");
ws.onopen = function() {};
ws.onmessage = function (evt) {
var received_msg = evt.data;
var html = $("#log").html();
html += "<p>"+received_msg+"</p>";
$("#log").html(html);
};
ws.onclose = function() {};
} else {
alert("WebSocket not supported");
}
});
</script>
</body>
</html>
"""
# need to be chaged to dict.
LISTENERS = []
def redis_listener():
r = redis.Redis('192.168.19.128')
p = r.pubsub()
p.subscribe('test_realtime')
for message in p.listen():
for element in LISTENERS:
element.write_message(message['data'])
class NewMsgHandler(tornado.web.RequestHandler):
def get(self):
self.write(TEMPLATE)
def post(self):
data = self.request.arguments['data'][0]
r = redis.Redis('192.168.19.128')
r.publish('test_realtime', data)
print(data);
class WsHandler(tornado.websocket.WebSocketHandler):
def open(self):
LISTENERS.append(self)
pprint.pprint(LISTENERS)
def on_message(self, message):
pass
def on_close(self):
LISTENERS.remove(self)
settings = {
'auto_reload': True,
}
application = tornado.web.Application([
(r'/realtime/', WsHandler),
], **settings)
if __name__ == "__main__":
threading.Thread(target=redis_listener).start()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8080)
tornado.ioloop.IOLoop.instance().start()