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

35 lines
885 B
Python

import tornado.websocket
import tornado.web
import tornado.ioloop
import os
web_dir = os.path.join(os.path.split(__file__)[0], 'www')
# 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),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": web_dir, "default_filename": 'index.html'}),
],debug=True)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()