You need to keep a collection of openned websockets and iterate through that collection at will to send messages.
As an example, I'll send a message anytime a client connect to your.domain.example/test/
but the idea is the same whenever you want to send something:
import os.path
import logging
from tornado import ioloop, web, websocket
SERVER_FOLDER = os.path.abspath(os.path.dirname(__file__))
LOGGER = logging.getLogger('tornado.application')
class TestHandler(web.RequestHandler):
def get(self):
server = ioloop.IOLoop.current()
data = "whatever"
server.add_callback(DefaultWebSocket.send_message, data)
self.set_status(200)
self.finish()
class DefaultWebSocket(websocket.WebSocketHandler):
live_web_sockets = set()
def open(self):
LOGGER.debug("WebSocket opened")
self.set_nodelay(True)
self.live_web_sockets.add(self)
self.write_message("you've been connected. Congratz.")
def on_message(self, message):
LOGGER.debug('Message incomming: %s', message)
def on_close(self):
LOGGER.debug("WebSocket closed")
@classmethod
def send_message(cls, message):
removable = set()
for ws in cls.live_web_sockets:
if not ws.ws_connection or not ws.ws_connection.stream.socket:
removable.add(ws)
else:
ws.write_message(message)
for ws in removable:
cls.live_web_sockets.remove(ws)
def serve_forever(port=80, address=''):
application = web.Application([
(r"/test/", TestHandler),
(r"/websocket/", DefaultWebSocket),
...
],
static_path=os.path.join(SERVER_FOLDER, ...),
debug=True,
)
application.listen(port, address)
LOGGER.debug(
'Server listening at http://%s:%d/',
address or 'localhost', port)
ioloop.IOLoop.current().start()
if __name__ == "__main__":
serve_forever()
You'll obviously need to create a websocket in the browser using the following JavaScript:
socket = new WebSocket('ws://your.domain.example:80/websocket/');
And manage it accordingly.