English 中文(简体)
页: 1 • 如何验证用户对服务器的投入
原标题:socket.io -- how to validate user input with server

I am trying to prompt user for input on the client, then check if the input is valid on the server. If the input is invalid the client should re-prompt until it receives a valid input. Only once the input is valid should anything else proceed.

客户。

while(looping){
    temp_name = prompt(usr + err_msg + pmsg, "");
    socket.emit("is_valid", temp_name, (response) => {
        looping = !response.status
        err_msg = response.message
    });
}

服务器。

// check if username is valid
    socket.on("is_valid", (temp_name, callback) => {
        // init
        let msg = "";
        let stat = false;

        // (check if valid)

        // return
        stat = (msg == "")
        response = {
            status: stat,
            message: msg,
        }
        callback(response);
    });

My code works if I remove the while loop in 客户。. But if I add the while loop, nothing works. No log statements print either.

虽然存在漏洞,因为如果投入无效,我希望再次促使用户使用。

为什么发生这种情况?

谢谢。

我期望它回头来,看上去的是假名是否有效。

If it is valid, looping = false and it should exit the while loop. If it is invalid, looping = true and it will prompt the user again. Currently all it does it repeatedly prompt the user no matter what is entered. But if I remove the while loop, it works correctly and returns the values I expect.

问题回答

固定。 解决办法是使用允诺(关于客户,使用书记本,而不是记本)。

否则,当你检查时,所有事情都是一样的,答复将无效。

Thank you





相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签