如何在龙卷风中的同一网址上提供HTTP页面和websocket

时间:2016-07-29 16:38:51

标签: python websocket tornado

我有一个API,根据请求协议,我想要为客户端提供HTTP响应,或者我想连接websocket并发送响应,然后进行增量更新。但是在Tornado中,我只能为任何URL指定一个处理程序。

1 个答案:

答案 0 :(得分:0)

HTTP页面请求和websocket之间的区别在于标头的存在。不幸的是afaik没有办法告诉龙卷风路由器根据标题(主机除外)选择一个或另一个处理程序。

然而,我可以制作一个处理程序,它继承了我已经非常精细的MyBaseRequestHandlerWebSocketHandler,并带有一些魔力。以下代码适用于python 3.5和tornado 4.3,您的里程可能因其他版本而异:

class WebSocketHandlerMixin(tornado.websocket.WebSocketHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # since my parent doesn't keep calling the super() constructor,
        # I need to do it myself
        bases = type(self).__bases__
        assert WebSocketHandlerMixin in bases
        meindex = bases.index(WebSocketHandlerMixin)
        try:
            nextparent = bases[meindex + 1]
        except IndexError:
            raise Exception("WebSocketHandlerMixin should be followed "
                            "by another parent to make sense")

        # undisallow methods --- t.ws.WebSocketHandler disallows methods,
        # we need to re-enable these methods
        def wrapper(method):
            def undisallow(*args2, **kwargs2):
                getattr(nextparent, method)(self, *args2, **kwargs2)
            return undisallow

        for method in ["write", "redirect", "set_header", "set_cookie",
                       "set_status", "flush", "finish"]:
            setattr(self, method, wrapper(method))
        nextparent.__init__(self, *args, **kwargs)

    async def get(self, *args, **kwargs):
        if self.request.headers.get("Upgrade", "").lower() != 'websocket':
            return await self.http_get(*args, **kwargs)
        # super get is not async
        super().get(*args, **kwargs)


class MyDualUseHandler(WebSocketHandlerMixin, MyBaseHandler):
    def http_get():
        self.write("My HTTP page")

    def open(self, *args, **kwargs):
        self.write_message("Hello WS buddy")

    def on_message(self, message):
        self.write_message("I hear you: %s" % message)