-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathapp.py
66 lines (48 loc) · 1.77 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json
from typing import Optional, Awaitable
import tornado.web
import tornado.websocket
import tornado.ioloop
from tornado import web
open_websockets = []
class IndexHandler(web.RequestHandler):
def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
pass
def get(self):
self.render("index.html")
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
pass
def check_origin(self, origin: str) -> bool:
return True
def open(self):
print("New client connected")
if self not in open_websockets:
open_websockets.append(self)
# self.write_message("You are connected")
def on_message(self, message):
print("message received: " + message)
# self.write_message("from server--" + message)
def on_close(self) -> None:
if self in open_websockets:
open_websockets.remove(self)
print("client connection closed")
class ApiHandler(web.RequestHandler):
def data_received(self, chunk: bytes) -> Optional[Awaitable[None]]:
pass
def post(self):
if self.request.headers.get("Content-Type", "").startswith("application/json"):
json_body = json.loads(self.request.body)
order_code = json_body["orderCode"]
print(order_code)
for wsc in open_websockets:
wsc.write_message(json_body)
application = tornado.web.Application([
(r"/", IndexHandler),
(r"/ws", WebSocketHandler),
(r"/events", ApiHandler),
(r"/favicon.ico", tornado.web.StaticFileHandler, {'path', '/favicon.ico'})
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()