据我了解,您想要这样的东西:
class MyWebSocketHandler(tornado.websocket.WebSocketHandler): # other methods def on_message(self, message): # do some stuff with the message that takes a long time self.write_message(response)
每个Websocket连接都有您自己的子类WebSocketHandler中的对象。
您甚至可以保存连接并在其他地方使用它:
ws_clients = []class MyWebSocketHandler(tornado.websocket.WebSocketHandler): # other methods def open(self): if self not in ws_clients: ws_clients.append(self) def on_close(self): if self in ws_clients: ws_clients.remove(self)def send_message_to_all(self, message): for c in ws_clients: c.write_message(message)



