specimens-of-composite-port.../echoserver.py

37 lines
1,022 B
Python
Raw Normal View History

2019-02-03 16:43:32 +00:00
import tornado.websocket
import tornado.web
import tornado.ioloop
import os
web_dir = os.path.join(os.path.split(__file__)[0], 'www')
2019-02-04 17:37:55 +00:00
output_dir = os.path.join(os.path.split(__file__)[0], 'output')
2019-02-03 16:43:32 +00:00
# This is our WebSocketHandler - it handles the messages
# from the tornado server
class WebSocketHandler(tornado.websocket.WebSocketHandler):
connections = set()
# the client connected
def open(self):
self.connections.add(self)
print ("New client connected")
# the client sent the message
def on_message(self, message):
[con.write_message(message) for con in self.connections]
# client disconnected
def on_close(self):
self.connections.remove(self)
print ("Client disconnected")
application = tornado.web.Application([
(r"/ws", WebSocketHandler),
2019-02-04 17:37:55 +00:00
(r"/output/(.*)", tornado.web.StaticFileHandler, {"path": output_dir}),
2019-02-03 16:43:32 +00:00
(r"/(.*)", tornado.web.StaticFileHandler, {"path": web_dir, "default_filename": 'index.html'}),
],debug=True)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()