-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (41 loc) · 1.33 KB
/
main.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
import os
import uuid
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import socket
import json
import connections
import sys
from command_queue import CommandQueue
if len(sys.argv) > 1 and sys.argv[1] == "--real":
type = 'real'
else:
type = 'dummy'
clients = connections.Connections()
commands = CommandQueue(clients, type)
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(request):
request.render("index.html")
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
clients.add_conneciton(self)
self.write_message(json.dumps(commands.get_state()))
def on_message(self, message):
parsed_json = json.loads(message)
commands.queue(parsed_json)
def on_close(self):
print('connection closed')
clients.close_connection(self)
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static"),
}
application = tornado.web.Application([(r'/ws', WSHandler), (r'/', IndexHandler)], **settings)
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(80)
myIP = socket.gethostbyname(socket.gethostname())
print('*** Websocket Server Started at %s***' % myIP)
tornado.ioloop.IOLoop.instance().start()