如何向所有客户发送数据?

时间:2015-05-11 19:33:16

标签: tornado

我有简单的服务器:

# -*- coding: utf-8 -*-

import os
import logging
import tornado.httpserver
import tornado.ioloop
import tornado.options


class WSHandler(tornado.websocket.WebSocketHandler):

    def check_origin(self, origin):
        return True

    def open(self):
        print 'connection opened'

    def on_message(self, message):
        self.write_message("Echo: " + message)
        print 'received:', message

    def on_close(self):
        print 'connection closed'


class MainHandler(tornado.web.RequestHandler):

    def get(self):
        self.write('OK')


url_patterns = [
    (r'/ws', WSHandler),
    (r'/update', MainHandler),
]

application = tornado.web.Application(
    url_patterns,
    debug=False
)

if __name__ == "__main__":
    application.listen(9991)
    tornado.ioloop.IOLoop.instance().start()

当我从浏览器调用“/ update”时,我想通过websocket向所有连接的客户端发送一些数据。怎么做?

1 个答案:

答案 0 :(得分:2)

event