Reputation: 957
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...
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();
}
});
Upvotes: 1
Views: 5279
Reputation: 1072
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)
}
);
Upvotes: 1
Reputation: 1379
socket.on listen event 'new_user' this event is triggered by this way:
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
Upvotes: 0