如何通过 python-socketio 使用从 node.js 服务器发送到 python 脚本客户端的值?

How to use values sent from node.js server to python script client using python-socketio?

我使用 node.js 服务器向 python 客户端发送数据。 Python 的控制台显示接收数据良好,但我无法在文档中找到在 Python 的客户端中使用它们的方法。

在Python的脚本控制台中:

engineio.client - INFO - Received packet MESSAGE data 2/flowRecognizedFairy,["myevent",{"spoken":"i'm speaking now"}]

我尝试了在 the API Documentation! 中找到的几个示例。

@sio.event
def message(data):
    print('I received a message!')

@sio.on('my message')
def on_message(data):
    print('I received a message!')

@sio.event
async def message(data):
    print('I received a message!')

我无法在控制台中打印任何内容。以下代码有效:

@sio.on('connect')
def on_connect():
    print('--> connection established')


@sio.on('disconnect')
def on_disconnect():
    print('--> disconnected from server')

没有错误信息。我希望首先使用打印功能打印接收数据,然后将它们与 python 脚本中的其他功能一起使用。

有什么技巧或想法吗?

您的服务器正在名为 /flowRecognizedFairy 的命名空间上发送消息。您的处理程序应设置为使用该命名空间。

@sio.on('connect', namespace='/flowRecognizedFairy')
def on_connect():
    print('--> connection established')

@sio.on('disconnect', namespace='/flowRecognizedFairy')
def on_disconnect():
    print('--> disconnected from server')

@sio.event(namespace='/flowRecognizedFairy')
def message(data):
    print('I received a message!')

@sio.on('my message', namespace='/flowRecognizedFairy')
def on_message(data):
    print('I received a message!')

@sio.event(namespace='/flowRecognizedFairy')
async def message(data):
    print('I received a message!')