Forked from chrenova/tornado_websocket_server_with_periodic_callback.py
Created
March 4, 2021 13:03
-
-
Save afreerunner/de255511d766745ef277cd804f6691e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # http://stackoverflow.com/questions/19542333/websocket-server-sending-messages-periodically-in-python | |
| import tornado.httpserver | |
| import tornado.websocket | |
| import tornado.ioloop | |
| from tornado.ioloop import PeriodicCallback | |
| import tornado.web | |
| class WSHandler(tornado.websocket.WebSocketHandler): | |
| def open(self): | |
| self.callback = PeriodicCallback(self.send_hello, 120) | |
| self.callback.start() | |
| def send_hello(self): | |
| self.write_message('hello') | |
| def on_message(self, message): | |
| pass | |
| def on_close(self): | |
| self.callback.stop() | |
| application = tornado.web.Application([ | |
| (r'/', WSHandler), | |
| ]) | |
| if __name__ == "__main__": | |
| http_server = tornado.httpserver.HTTPServer(application) | |
| http_server.listen(8888) | |
| tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment