简体   繁体   中英

node.js and socket.io how use callback?

I'm trying to learn about using node.js and socket.io. I was using before PHP and i have trouble with understanding how use callbacks in node.js.

I have part of code assigned to socket.io

if (validate_room(room)) {
    console.log('Room exist'); 
    io.sockets.clients(room).forEach(
        function (socket) {
            console.log(socket.id);
        });
    //do some fucnction using socket interface    
} else {
    console.log('Room not exist');
    //do other function using socket interface        
}

You can see, here that i need access to io.sockets object.

Function above validate_room

function validate_room(room) {

    mysql_connection.query('SELECT * FROM rooms WHERE room = ' + mysql_connection.escape(room), function(err, rows, fields) {
        if (err)
            throw err;

        if (rows.length.toString() > 0) {
            console.log('Validate room - true: ', rows.length.toString());

            return true;
        }
        console.log('Validate room - false: ', rows.length.toString());
        return false;
    });
}

I need second function to just return "true / false ".

When i was using "browser" i just put inside callback to another external function, but here i need access to socket.io object.

So i would like to have "if(validate_room(room))" here stop and wait for result true/false.

Maybe someone could point me, where i make mistake in my thinking.

Best regards Marc

Put something like this into your callback:

if (err)
    throw err;
afterRoomValidation(rows.length.toString() > 0);

and the function:

function afterRoomValidation(isValid) {
    if (isValid) {
        console.log('Room exist'); 
        io.sockets.clients(room).forEach(function (socket) { console.log(socket.id);  });
        //do some fucnction using socket interface    
    } else {
        console.log('Room not exist');
        //do other function using socket interface        
    }
}

function to just return "true / false ".

No. You can't do that just as you can't synchronously return the response from an AJAX call from a function? . Use callbacks:

function validate_room(room, validCallback, invalidCallback) {

    mysql_connection.query('SELECT * FROM rooms WHERE room = ' + mysql_connection.escape(room), function(err, rows, fields) {
        if (err)
            throw err;

        if (rows.length.toString() > 0) {
            console.log('Validate room - true: ', rows.length.toString());
            validCallback();
        } else
            console.log('Validate room - false: ', rows.length.toString());
            invalidCallback();
        }
    });
}

and

validate_room(room, function() {
    console.log('Room exist'); 
    io.sockets.clients(room).forEach(function (socket) {
        console.log(socket.id);
        //do some fucnction using socket interface
    });
}, function() {
    console.log('Room not exist');
    //do other function using socket interface        
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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