from tornado import web, ioloop, options, websocket import os.path clients = [] messages = [] class Application(web.Application): def __init__(self): handlers = [ (r"/", MainHandler), (r"/chat", ChatHandler), ] settings = dict( cookie_secret="tes12323hhdfdfre9312313", template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), xsrf_cookies=True, ) super().__init__(handlers, **settings) class MainHandler(web.RequestHandler): def get(self): self.render("index.html") class ChatHandler(websocket.WebSocketHandler): def open(self): print("Web socket opened") clients.append(self) def on_message(self, message): messages.append({'msg':message}) for msg in messages: self.write_message(msg) def check_origin(self, origin): return True def main(): options.parse_command_line() app = Application() app.listen(8888) ioloop.IOLoop.current().start() if __name__ == '__main__': main()