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.
Revisions
-
chrenova renamed this gist
Jan 27, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
chrenova created this gist
Jan 27, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ # 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()