[英]Tornado websocket handler , self.close() is closing connection without triggering the on_close() method
im new at (python, stackoverflow, tornado) , so please, be patient :). 我是新的(python,stackoverflow,龙卷风),所以请耐心等待:)。 Correct me.
纠正我。
Im working with tornado on a real-time app. 我正在使用龙卷风在一个实时应用程序。 When i call self.close() inside the Websocket handler class , the on_close method is not fired up , for this time i did a little wrapper , fixing the problem and (for example) discarding that connected agent (pure avascript wss api client) correctly.
当我在Websocket处理程序类中调用self.close()时,on_close方法没有被启动,这次我做了一个小包装,解决了问题并(例如)丢弃了连接代理(纯avascript wss api客户端)正确。
All network problems are discarded , since my poor wrapper is working nicely and im in a LAN enviroment. 所有的网络问题都被丢弃了,因为我糟糕的包装器工作得非常好并且我在局域网环境中工作。
is someone having the same problem ? 有人有同样的问题吗? I cant sleep without an explanation.
我无法入睡,没有解释。
THANK YOU , really. 谢谢,真的。
## imports and other stuff
AGENTS = set()
class BackofficeWSSRailMain(tornado.websocket.WebSocketHandler):
def on_open(self):
pass
def on_message(self,raw_message):
json_msg=json.loads(raw_message)
login = function_that_process_login(json_msg)
if login == True:
AGENTS.add(self)
self.write_message("ok")
else:
self.write_message("nologin")
self.close() ### this part should fireup
### the on_close method but nothing
### happens so AGENT is not discarded.
### here is when i actually call on_close_wrapper(),
### the method below.
def on_close_wrapper(self):
self.close() ### this is my actual solution ,
### waiting for more research.
self.on_close()
def on_close(self):
AGENTS.discard(self)
## Calling ioloop ...
self.on_close
is executed if and only if the client closes its side of the websocket connection. 当且仅当客户端关闭websocket连接的一侧时,才执行
self.on_close
。 Try it: If you open a web page that contains a Javascript client which connects to your server, then you close the page, on_close
will run. 试一试:如果你打开一个包含连接到服务器的Javascript客户端的网页,那么你关闭页面,
on_close
就会运行。 on_close
is not supposed to run if you call self.close
in your server code. 如果在服务器代码中调用
self.close
,则不应运行on_close
。
Your wrapper is a reasonable solution to your problem; 你的包装是你问题的合理解决方案; that is, it's a reasonable way to ensure that the same code runs either when you call
self.close
or when the client disconnects. 也就是说,这是确保在调用
self.close
或客户端断开连接时运行相同代码的合理方法。
As mentioned, on_close()
is only executed if the client closes the connection, so it's a good place to clean up resources. 如上所述,
on_close()
仅在客户端关闭连接时执行,因此它是清理资源的好地方。
For any cleanup use the on_finish()
method documented here https://tornado.readthedocs.org/en/latest/web.html#tornado.web.RequestHandler.on_finish 对于任何清理,请使用此处记录的
on_finish()
方法https://tornado.readthedocs.org/en/latest/web.html#tornado.web.RequestHandler.on_finish
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.