-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
50 lines (47 loc) · 1.47 KB
/
app.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
import tornado
import rethinkdb as r
import tornado.ioloop
import tornado.web
from tornado.web import asynchronous
import time
class MainHandler(tornado.web.RequestHandler):
def initialise(self, source):
self.set_header('content-type', 'text/event-stream')
self.set_header('cache-control', 'no-cache')
@asynchronous
def get(self):
for i in range(10):
time.sleep(1)
self.write(str(i))
self.flush()
self.finish()
class IndexHandler(tornado.web.RequestHandler):
s = """
<!DOCTYPE html>
<html>
<head>
<script>
var source = new EventSource("/stream");
source.onmessage = function(event) {
console.log(event)
document.getElementById("output").innerHTML += event.data + "<br/>"
}
</script>
</head>
<body>
<h1>Output</h1>
<div id="output"></div>
</body>
</html>
"""
def get(self):
self.finish(self.s)
def make_app():
return tornado.web.Application([
(r"/stream", MainHandler),
(r"/", IndexHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()