Python3 Websocket回调无法分配类成员函数

5jvtdoz2  于 2022-12-01  发布在  Python
关注(0)|答案(1)|浏览(174)

我在Ubuntu 18.04上使用Python 3.6。
我有一个类,我正在尝试向其中添加一个WebSocket服务器。我使用的Python websockets模块可以在这里找到:https://github.com/websocket-client/websocket-client
该项目有一个如下所示的示例:

import websocket

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    def run(*args):
        for i in range(3):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())

if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://echo.websocket.org/",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

在我的代码中,我在类的构造函数中执行以下操作:

# Configure a command and control websocket
self.commandWebsocket = None
self.commandWebsocketThread = threading.Thread(target=self.configureAndRunCommandChannelWebsocket, args=(commandAndControlPort,))
self.commandWebsocketThread.daemon = True
self.commandWebsocketThread.start()

然后按如下方式设置websocket:

def onCommandChannelWebsocketMessage(self, ws, message):
    self.debugPrint("Websocket Message: %s"%(message))

def onCommandChannelWebsocketError(self, ws, error):
    self.debugPrint("Websocket Error: %s"%(error))

def onCommandChannelWebsocketClose(self, ws):
    self.debugPrint("Websocket Closed ...")

def onCommandChannelWebsocketOpen(self, ws):
    self.debugPrint("Websocket Opened ...")
    def run(*args):
        for i in range(30000):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
    #thread.start_new_thread(run, ())

def configureAndRunCommandChannelWebsocket(self,wsPort):

    self.debugPrint("Configuring Command and Control Websocket with: %s ..."%(wsPort))
    websocket.enableTrace(True)
    self.commandWebsocket = websocket.WebSocketApp("ws://0.0.0.0:%s/"%(wsPort))
    self.commandWebsocket.on_message = self.onCommandChannelWebsocketMessage
    self.commandWebsocket.on_error = self.onCommandChannelWebsocketError
    self.commandWebsocket.on_close = self.onCommandChannelWebsocketClose
    self.commandWebsocket.on_open = self.onCommandChannelWebsocketOpen
    self.commandWebsocket.run_forever()

然而,我总是得到这样的错误:

error from callback <bound method Camera.onCommandChannelWebsocketError of <Cameras.Camera.Camera object at 0x7f34316a58>>: onCommandChannelWebsocketError() missing 1 required positional argument: 'error'
[Camera.py / Camera]: Pipeline: v4l2src device=/dev/video2 ! video/x-raw, width=(int)160, height=(int)120,format=GRAY16_LE ! appsink
  File "/usr/local/lib/python3.6/dist-packages/websocket/_app.py", line 343, in _callback
    callback(*args)
error from callback <bound method Camera.onCommandChannelWebsocketClose of <Cameras.Camera.Camera object at 0x7f34316a58>>: onCommandChannelWebsocketClose() missing 1 required positional argument: 'ws'
[Camera.py / Camera]: Initializing a Generic Camera
  File "/usr/local/lib/python3.6/dist-packages/websocket/_app.py", line 343, in _callback
    callback(*args)

我做错了什么,我该如何改正?

cyvaqqii

cyvaqqii1#

将设置更改为以下设置:

def onCommandChannelWebsocketMessage(self, message):
    self.debugPrint("Websocket Message: %s"%(message))

def onCommandChannelWebsocketError(self, error):
    self.debugPrint("Websocket Error: %s"%(error))

def onCommandChannelWebsocketClose(self):
    self.debugPrint("Websocket Closed ...")

def onCommandChannelWebsocketOpen(self):
    self.debugPrint("Websocket Opened ...")
    def run(*args):
        for i in range(30000):
            time.sleep(1)
            self.send("Hello %d" % i)
        time.sleep(1)
        self.close()
    #thread.start_new_thread(run, ())

def configureAndRunCommandChannelWebsocket(self,wsPort):

    self.debugPrint("Configuring Command and Control Websocket with: %s ..."%(wsPort))
    websocket.enableTrace(True)
    self.commandWebsocket = websocket.WebSocketApp("ws://0.0.0.0:%s/"%(wsPort))
    self.commandWebsocket.on_message = self.onCommandChannelWebsocketMessage
    self.commandWebsocket.on_error = self.onCommandChannelWebsocketError
    self.commandWebsocket.on_close = self.onCommandChannelWebsocketClose
    self.commandWebsocket.on_open = self.onCommandChannelWebsocketOpen
    self.commandWebsocket.run_forever()

相关问题