简体   繁体   English

在 socket.emit() 中传递参数的问题

[英]Issue with passing parameters in socket.emit()

            document.querySelector('#sendmessage').onclick = () => {
                const channel = "sports";
                const name = document.querySelector('#displayname').data("displayname");
                const msg = document.querySelector('[name="message"]').value;
                const time = timeStamp();
                //message(n,d,t);
                socket.emit('updatemessage', {'channel':channel, 'name':name, 'msg':msg, 'time':time});
            }

I'm having issue with passing these four parameters (channel, name, msg, time) in socket.emit() part.我在 socket.emit() 部分传递这四个参数(通道、名称、味精、时间)时遇到问题。 I need to pass them over to store the message details over flask server.我需要将它们传递给 Flask 服务器以存储消息详细信息。 Am I allowed to pass more than one parameters?我可以传递多个参数吗? If not, how do I deal with this?如果没有,我该如何处理?

Edit: Flask snippet编辑:烧瓶片段

@socketio.on('updatemessage')
def updatemessage(data):
    print("initialised")
    channel = data["channel"]
    name = data["name"]
    channels[channel][name][0] = data["msg"]
    channels[channel][name][1] = data["time"]
    print(f" this is the update channel info : {channels}")
    m = channels[channel]
    print(f"m is :  {m}")
    emit('message loader', m, broadcast=True)

Actually I'm trying to store the message data in global variable in flask server实际上我正在尝试将消息数据存储在烧瓶服务器的全局变量中

Answering my own question!回答我自己的问题!

I checked found out there was some problem with this line const name = document.querySelector('#displayname').data("displayname");我检查发现这一行有一些问题const name = document.querySelector('#displayname').data("displayname");

and now it's working perfectly.现在它工作得很好。 I was in belief that it must have been socket.emit() part that's creating the problem我相信它一定是造成问题的socket.emit()部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM