Using a websocket client as a class in python

前端 未结 8 1017
名媛妹妹
名媛妹妹 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 14:03

    import websocket
    
    try:
        import thread
    except ImportError:
        import _thread as thread
    import time
    
    
    class OnyxGenericClient:
        """
        Onyx Client Interface
    
        """
    
        def __init__(self, ):
            websocket.enableTrace(True)
            ws = websocket.WebSocketApp("ws://localhost:3000/",
                                             on_message=self.on_message,
                                             on_error=self.on_error,
                                             on_close=self.on_close)
            self.ws = ws
            self.ws.on_open = self.on_open
            self.ws.run_forever()
    
        # def initiate(self):
    
        def on_message(self, message):
            print(message)
            return message
    
        def on_error(self, error):
            return error
    
        def on_close(self):
            print("### closed ###")
    
        def run(self, *args):
            global driver
            driver = True
            while driver:
                try:
                    time.sleep(1)
                    print("Say something nice")
                    p = input()
                    self.ws.send(p)
                except KeyboardInterrupt:
                    driver = False
            time.sleep(1)
            self.ws.close()
            print("thread terminating...")
    
        def on_open(self):
            thread.start_new_thread(self.run, ())
    
    
    if __name__ == "__main__":
        websocket.enableTrace(True)
        onyx_client = OnyxGenericClient()
    

    I wonder why everyone is still putting the ws parameter.

    Read the error log.

    File "venv/lib/python3.7/site-packages/websocket/_app.py", line 343, in _callback callback(*args)

        def _callback(self, callback, *args):
        if callback:
            try:
                if inspect.ismethod(callback):
                    callback(*args)
                else:
                    callback(self, *args)
    
            except Exception as e:
                _logging.error("error from callback {}: {}".format(callback, e))
                if _logging.isEnabledForDebug():
                    _, _, tb = sys.exc_info()
                    traceback.print_tb(tb)
    

    Looking at our callbacks, on_open(self, ws)

    When the try block executes it checks if our callback is a method or a function. if it is a method it would execute the callback(*args) already our self from our CustomClient is already passed as an argument in (*args). Mind you it already has its own self in def _callback(self, callback, *args). Hence, every callback that is an instance of your CustomClient should not have the ws argument.

提交回复
热议问题