如何在Tornado中任意发送WebSocket消息?

7

我对Tornado非常陌生,想知道是否可以在我的Python程序中随时向所有客户端发送消息(write_message)? 例如,假设我的程序正在监视目录以查看文件是否出现/存在。 当它出现时,我想向浏览器客户端发送一个Web Socket消息,告诉其文件已存在。 我似乎无法理解如何在没有先接收到WebSocket消息(on_message handler)的情况下调用"write_message"方法。

即使我使用"PeriodicCallback"方法,我仍然不清楚如何实际调用"write_message"方法。 是否有任何示例展示如何在没有在"on_message"处理程序中执行的情况下调用"write_message"方法?

1个回答

7
你需要保留已打开的WebSocket集合,并随时遍历该集合发送消息。
例如,每当客户端连接到your.domain.example/test/时,我会发送一条消息,但是在你想要发送任何内容时,思路都是相同的。
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()

你显然需要使用以下JavaScript在浏览器中创建一个websocket:

socket = new WebSocket('ws://your.domain.example:80/websocket/');

并相应地进行管理。

1
呃,你必须将 self 保存到全局变量中。我在想是否有更好的方法来完成这个操作。 - Collin Bell
@CollinBell 你可以始终使用类变量来进行“set”操作,并将send函数转换为@classmethod ;) - 301_Moved_Permanently

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接