socketIO-client python回调未收到参数

时间:2019-07-02 01:16:46

标签: python flask socket.io

我向烧瓶服务器发送命令(文件)。它通过发出响应来响应,作为参数,为字典提供了所请求的文件名。

from socketIO_client import SocketIO, LoggingNamespace, BaseNamespace


def on_data_command_response(*args):
    print('on_data_command_response')
    print(len(args))
    print("-----------")


with SocketIO("192.168.1.164", 5000, LoggingNamespace) as socketIO:

    socketIO.on('data command response', on_data_command_response)

    socketIO.emit('data event', {'command': 'files'}, on_data_command_response)
    socketIO.emit('data event', {'command': 'files'}, on_data_command_response)

    socketIO.wait_for_callbacks(seconds=5)

在烧瓶中,命令的接收方式为:

@socketio.on('data event')
def data_event(json, methods=['GET', 'POST']):
    print('received data command: {}'.format(json))

并发送响应:

def emit_data_command_response(payload_json):
    socketio.emit('data command response', payload_json, callback=messageReceived)

问题: 看起来发射被接收了两次。对于我发送的每个命令,一次使用args,一次不使用。

on_data_command_response
2
-----------
on_data_command_response
0
-----------
on_data_command_response
2
-----------
on_data_command_response
0
-----------

变化1:

socketIO.on('data command response', on_data_command_response)


socketIO.emit('data event', {'command': 'files'}, on_data_command_response)
socketIO.emit('data event', {'command': 'files'})
socketIO.emit('data event', {'command': 'files'})
socketIO.emit('data event', {'command': 'files'})

返回

on_data_command_response
2
-----------
on_data_command_response
0
-----------
on_data_command_response
2
-----------
on_data_command_response
2
-----------
on_data_command_response
2
-----------

但变量2(我认为这是最合逻辑的):

socketIO.on('data command response', on_data_command_response)
socketIO.emit('data event', {'command': 'files'})

on_data_command_response甚至没有被调用。

我不明白我在这里做错了。对使用args调用一次回调“ on_data_command_response”有帮助吗?

1 个答案:

答案 0 :(得分:0)

问题是我混淆了回调和发出。 ->回调是服务器“返回”发出命令时。 -> socketIO.on ....指导服务器何时进行发射。 socketIO.wait(seconds = 1)->等待回调。因此,如果未提供回调函数,则它不会执行任何操作。

https://pypi.org/project/socketIO-client/

客户:

import time
from socketIO_client import SocketIO, LoggingNamespace, BaseNamespace


def on_data_command_response(*args):
    if len(args) > 0:
        print(len(args))
        # print('on_data_command_response')
        # for arg in args[0]:
           # print((arg)) 
    else:
        print("no args received.")
    # return # send acknowledgement

def message_received_by_servercallback(*args):
    print("----all good----")

def on_disconnect():
    print("lode disconnect")



with SocketIO("192.168.1.164", 5000, LoggingNamespace) as socketIO:

    socketIO.on('disconnect', on_disconnect)

    socketIO.on('data command response', on_data_command_response)

    socketIO.emit('backend event', {'command': 'files'}, message_received_by_servercallback)

    socketIO.wait_for_callbacks(seconds=1)

使用服务器:

@socketio.on('backend event')
def backend_request(json):
    if "command" in json.keys():  
        if json["command"] == "files":
            response_json = {}
            response_json["status"] = "download files"

    payload_json = response_json
    socketio.emit('data command response', payload_json, callback=messageReceived)
    return ["received"] # will trigger the callback

按预期执行(客户端):

2
----all good----
lode disconnect
>>>