-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime.py
55 lines (44 loc) · 1.24 KB
/
realtime.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
import json
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornadio
tornado.websocket.WebSocketHandler.allow_draft76 = (lambda self: True)
clients = set()
current_status = 'stopped'
def broadcast(msg):
for c in clients:
c.send(msg)
class MainHandler(tornado.web.RequestHandler):
def get(self):
global current_status
action = self.get_argument('action', None)
if action == 'add':
broadcast({'type': 'addMiles',
'miles': float(self.get_argument('miles')),
'monies': float(self.get_argument('monies')),
'anim': int(self.get_argument('anim')),
})
else:
current_status = self.get_argument('status')
print "status:", current_status
broadcast(current_status)
self.write("Hello, world")
class SocketHandler(tornadio.SocketConnection):
def on_open(self, *args, **kwargs):
print "OPEN"
clients.add(self)
def on_message(self, message):
print "MSG", message
def on_close(self):
print "CLOSE"
try:
clients.remove(self)
except: pass
application = tornado.web.Application([
(r'/update', MainHandler),
tornadio.get_router(SocketHandler).route(),
])
if __name__ == '__main__':
application.listen(5679)
tornado.ioloop.IOLoop.instance().start()