Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save afreerunner/de255511d766745ef277cd804f6691e8 to your computer and use it in GitHub Desktop.
Save afreerunner/de255511d766745ef277cd804f6691e8 to your computer and use it in GitHub Desktop.
# 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