首页 > 解决方案 > Python3 Websocket回调无法分配类成员函数

问题描述

我在 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)

我做错了什么,我该如何解决?谢谢!

标签: pythonlinuxpython-3.xwebsocketcallback

解决方案


将您的设置更改为以下内容:

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()

推荐阅读