【发布时间】:2019-04-19 06:23:01
【问题描述】:
我正在使用 tornado 版本 6.0.2 构建一个 Web 应用程序。我正在使用 WebSocket 处理程序来建立与客户端的连接。
示例服务器端实现:
from tornado import websocket
import connectionhandler
class WebSocketHandler(websocket.WebSocketHandler):
def initialize(self, connectionhandler):
self.connectionhandler = connectionhandler
async def open(self):
print("WebSocket opened.")
await self.connectionhandler.connection_established_websocket()
async def on_close(self):
print("WebSocket closed.")
await self.connectionhandler.connection_closed_websocket()
示例客户端实现:
ws = websocket.create_connection("ws://localhost:80/ws?")
ws.close()
当客户端建立连接时,它会调用 open 方法,一切正常。
但是当客户端关闭连接时,我得到错误 on_close is never awaited.
当我删除本机协程时 on_close 方法正在工作。
问题:
如何为 on_close 方法添加原生协程或从 on_close() 调用异步方法?
【问题讨论】:
标签: python python-3.x websocket tornado