![](/img/trans.png)
[英]WebSocket Handler on_close method async Implementation tornado
[英]Tornado websocket handler , self.close() is closing connection without triggering the on_close() method
我是新的(python,stackoverflow,龍卷風),所以請耐心等待:)。 糾正我。
我正在使用龍卷風在一個實時應用程序。 當我在Websocket處理程序類中調用self.close()時,on_close方法沒有被啟動,這次我做了一個小包裝,解決了問題並(例如)丟棄了連接代理(純avascript wss api客戶端)正確。
所有的網絡問題都被丟棄了,因為我糟糕的包裝器工作得非常好並且我在局域網環境中工作。
有人有同樣的問題嗎? 我無法入睡,沒有解釋。
謝謝,真的。
## 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 ...
當且僅當客戶端關閉websocket連接的一側時,才執行self.on_close
。 試一試:如果你打開一個包含連接到服務器的Javascript客戶端的網頁,那么你關閉頁面, on_close
就會運行。 如果在服務器代碼中調用self.close
,則不應運行on_close
。
你的包裝是你問題的合理解決方案; 也就是說,這是確保在調用self.close
或客戶端斷開連接時運行相同代碼的合理方法。
如上所述, on_close()
僅在客戶端關閉連接時執行,因此它是清理資源的好地方。
對於任何清理,請使用此處記錄的on_finish()
方法https://tornado.readthedocs.org/en/latest/web.html#tornado.web.RequestHandler.on_finish
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.