-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.py
71 lines (48 loc) · 1.41 KB
/
server.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
# -*- coding: utf-8 -*-
"""
tornado websocket proxy for WF worker daemons
"""
from queue import PikaClient
from uuid import uuid4
from tornado import websocket, web, ioloop
class IndexHandler(web.RequestHandler):
def get(self):
self.render("index.html")
class SocketHandler(websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def _get_sess_id(self):
return self.sess_id;
def open(self):
self.sess_id = uuid4().hex
self.application.pc.register_websocket(self._get_sess_id(), self)
def on_message(self, message):
self.application.pc.redirect_incoming_message(self._get_sess_id(), message)
def on_close(self):
"""
remove connection from pool on connection close.
"""
self.application.pc.unregister_websocket(self._get_sess_id())
class Connect(web.RequestHandler):
@web.asynchronous
def get(self, *args):
self.render('connector.html')
class Index(web.RequestHandler):
@web.asynchronous
def get(self, *args):
self.render('index.html')
app = web.Application([
(r'/ws', SocketHandler),
(r'/connect', Connect),
(r'/', Index),
])
def runserver():
my_ioloop = ioloop.IOLoop.instance()
# setup pika client
pc = PikaClient(my_ioloop)
app.pc = pc
pc.connect()
app.listen(9001)
my_ioloop.start()
if __name__ == '__main__':
runserver()