对于在一个类中重写WebSocketApp 的回调函数遇到的问题

在创建websocket连接时,最好开启追踪回溯,不然你都发现不了这个错误!!!

开启这个就相当于可以看到ws得报错信息,我之前没开,一直没报错,花了很长时间解决。

复制代码
websocket.enableTrace(True)

问题描述:

将websocket的on_open()封装到一个类中,就会出现如题问题。

error from callback <bound method CallAI.on_message of <main.CallAI object at 0x000001D8EDC9B4C8>>: on_message() missing 1 required positional argument: 'message'
File "D:\Anaconda\envs\Game\lib\site-packages\websocket\_app.py", line 343, in _callback
callback(*args)

解决方案

问题出在websocket的_callback方法调用上。

python 复制代码
    def _callback(self, callback, *args):
        if callback:
            try:
                if inspect.ismethod(callback):
                    callback(*args)
                else:
                    callback(self, *args)

默认是调用函数,然后传入参数。

当on_open(self, ws)是class内的方法时,会把self也当成一个参数传给_callback,这样它就找不到想要的ws,可以把ws设置为一个类的全局变量,不需要传入到on_open中,直接从全局self.ws调用。

相关推荐
JHC00000021 小时前
基于Ollama,Milvus构建的建议知识检索系统
人工智能·python·milvus
mOok ONSC21 小时前
SpringBoot项目中读取resource目录下的文件(六种方法)
spring boot·python·pycharm
GIS兵墩墩1 天前
postgis--PostgreSQL16及其plpython3u扩展
python·postgis
new Object ~1 天前
LangChain的短期记忆存储实现
python·langchain
魔都吴所谓1 天前
【Python】从零构建:IP地理位置查询实战指南
开发语言·python·tcp/ip
测试19981 天前
使用Python自动化生成接口测试用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
智算菩萨1 天前
【Pygame】第10章 游戏状态管理与场景切换机制
python·游戏·pygame
songcream11 天前
TensorFlow的一些基本概念
人工智能·python·tensorflow
AI逐月1 天前
解决 ComfyUI 插件安装后 Nanobind 报错问题:soxr 版本冲突原理解读
开发语言·python