-
Notifications
You must be signed in to change notification settings - Fork 0
/
crowddemo_tornado_loop.py
80 lines (64 loc) · 2.16 KB
/
crowddemo_tornado_loop.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
import ujson
import zmq
from zmq.eventloop import ioloop, zmqstream
"""
ioloop.install() must be called prior to instantiating *any* tornado objects,
and ideally before importing anything from tornado, just to be safe.
install() sets the singleton instance of tornado.ioloop.IOLoop with zmq's
IOLoop. If this is not done properly, multiple IOLoop instances may be
created, which will have the effect of some subset of handlers never being
called, because only one loop will be running.
"""
ioloop.install()
import tornado
from tornado import websocket, web
from app_settings import settings, zmq_router_fe, ws_tcp_port
cl = []
def printer(msg):
print "ZMQ Received: %s" % msg
ctx = zmq.Context()
s = ctx.socket(zmq.REQ)
s.connect('tcp://127.0.0.1:%d' % zmq_router_fe)
stream = zmqstream.ZMQStream(s)
stream.on_recv(printer)
class IndexHandler(web.RequestHandler):
def get(self):
self.render("app\\multi_brad_scene.html")
class SocketHandler(websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
if self not in cl:
cl.append(self)
print("...WebSocket opened!")
stream.on_recv(self.send_update)
def on_close(self):
if self not in cl:
cl.remove(self)
print("WebSocket closed")
stream.on_recv(self.send_update)
def on_message(self, message):
print("Received: " + message)
m = ujson.loads(message)
cmd = m['command']
print("...Command: " + cmd)
if cmd=='update':
dtime = str(m['data'])
stream.send_multipart([b"update", dtime])
def send_update(self, msg):
resp = msg[0]
print "ZMQ Received: %s" % resp
self.write_message(resp)
app = web.Application([
(r'/', IndexHandler),
(r'/ws', SocketHandler),
(r'/js/(.*)',web.StaticFileHandler, {'path': './app/js'},),
(r'/models/(.*)',web.StaticFileHandler, {'path': './app/models'},)
], **settings)
if __name__ == '__main__':
app.listen(ws_tcp_port)
print "Listening on %d..." % ws_tcp_port
try:
ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
print ' Interrupted'