Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save afreerunner/de255511d766745ef277cd804f6691e8 to your computer and use it in GitHub Desktop.

Select an option

Save afreerunner/de255511d766745ef277cd804f6691e8 to your computer and use it in GitHub Desktop.

Revisions

  1. @chrenova chrenova renamed this gist Jan 27, 2017. 1 changed file with 0 additions and 0 deletions.
  2. @chrenova chrenova created this gist Jan 27, 2017.
    30 changes: 30 additions & 0 deletions tornado_s_server_with_periodic_callback.py
    Original 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()