websocket [tornado]的单个实例可以处理不同的请求吗? [英] can a single instance of websocket[tornado] to handle different requests?

查看:32
本文介绍了websocket [tornado]的单个实例可以处理不同的请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ajax和Websockets的项目中工作.任务是摆脱Ajax,仅使用Websockets.在服务器端,我正在使用龙卷风和django以及tornado-url-dispatcher.我想使用websocket(tornado.websocket.WebSocketHandler)的单个实例重用django中已经定义的一些方法.此类具有3个默认处理程序,但我通过添加新的处理程序进行了扩展,该处理程序重定向到现有的django方法,并修改了分派器以指向新方法.

I work on a project which uses Ajax and Websockets. The task is to get rid of the Ajax and to use Websockets only. On the server side I'm using tornado and django with a tornado-url-dispatcher. I want to reuse some methods already defined in django using a single instance of websocket(tornado.websocket.WebSocketHandler). This class has 3 default handlers but I extended it by adding new handlers which redirects to the existent django methods and modified the dispatcher to point to the new methods.

class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        ...
    def on_message(self, message):
        ...
    def on_close(self):
        ...
    def new_handler_1(self, request):
        ...

tornado_app = tornado.web.Application(
    [
      (r'/ws/new_handler', wshandler.WSHandler.new_handler_1),
      (r'/ws', wshandler.WSHandler),
    ]

我应该使用哪种类型的响应才能通过websocket从new_handler_1方法回复请求?谢谢.

What type of response shall I use in order to reply from new_handler_1 method to a request done via a websocket ? Thanks.

推荐答案

您不能执行此操作;将为每个请求创建一个新的处理程序类实例.取而代之的是,使处理程序可以使用其他共享对象在它们之间进行通信.您可以将该对象附加到Application对象,也可以将其作为 initialize 参数传递给处理程序.

You can't do this; a new instance of the handler class is created for every request. Instead, make some other shared object that the handlers can use to communicate between themselves. You can pass this object to the handlers either by attaching it to the Application object or passing it as an initialize argument to the handler.

这篇关于websocket [tornado]的单个实例可以处理不同的请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆