简体   繁体   English

callback(false)和callback(true)有什么作用?

[英]What does callback(false) and callback(true) do?

I'm studying an example project of a nodejs chat, and I can't really understand what happens when callback(false) and callback(true) are called here... 我正在研究一个nodejs聊天的示例项目,但我真的无法理解在这里callback(false)callback(true)时会发生什么...

io.sockets.on('connection', function(socket){
    socket.on('new user', function(data, callback){
        if(usernames.indexOf(data) != -1){
            callback(false);
        } else {
            callback(true);
            socket.username = data;
            usernames.push(socket.username);
            updateUsernames();
        }
    });

callback is the acknowledgement function 回调是确认功能

server 服务器

        socket.on('new user', 
          function(data, calback){
                  // incidentally(not needed in this case) send back data value true 
                  calback(true);
          }
         );

client 客户

    socket.emit('new user', 
              data, 
              function(confirmation){
                       console.log(confirmation);
                      //value of confirmation == true if you call callback(true)
                      //value of confirmation == false if you call callback(false) 
              }
             );

socket.on listen event 'new_user' this event is triggered by this way: socket.on监听事件“ new_user”,此事件是通过以下方式触发的:

    var data= "mydata"
    var callback=function(bool){
      if(bool){
        console.log('success')
      }else{
        console.log('error')
      }
    }

socket.trigger('new_user',[data,callback])

callback is just a function pass as parameter of the trigger 回调只是函数传递,作为触发器的参数

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

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