Using a websocket client as a class in python

前端 未结 8 1206
名媛妹妹
名媛妹妹 2021-02-04 13:19

I\'m trying access some data using websockets, but I cannot really get around the examples given in the websockets documentation.

I have this code (https://pypi.org/proj

8条回答
  •  青春惊慌失措
    2021-02-04 13:55

    This is working:

    class MySocket(object):
        def __init__(self):
            websocket.enableTrace(True)
            self.ws = websocket.WebSocketApp("ws://echo.websocket.org:12300/foo",
                                    on_message = self.on_message,
                                    on_error = self.on_error,
                                    on_close = self.on_close)
    
        @staticmethod
        def on_message(ws, message):
            print message
    
        @staticmethod
        def on_error(ws, error):
            print error
    
        @staticmethod
        def on_close(ws):
            print "### closed ###"
    
        @staticmethod
        def on_open(ws):
            ws.send("Hello %d" % i)
    

    But you don't have access to self

提交回复
热议问题