Passing data between classes in Tweepy and Tornado WebSocket

喜欢而已 提交于 2019-12-06 06:18:49

on_message is an instance method, not a class method. You need to call it on an instance, like this:

handler = WSHandler()
handler.on_message('hello world')

However, you can't just do that as instances need to be created by a browser connection in order to actually send and receive messages.

What you probably want is to keep a list of open connections (the Tornado websocket demo is a good example of this):

class WSHandler(tornado.websocket.WebSocketHandler):
    connections = []

    def open(self):
        self.connections.append(self)

    ....

    def on_close(self):
        self.connections.remove(self)

then, in StdOutListener.on_status, you can do something like:

for connection in WSHandler.connections:
    connection.write_message(status.text)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!