Socket.io and Node.js with mysql not returning results as expeted -
edit: i'm rephrasing question: socket.io not waiting callback , connection never accepted. please see edit 2/attempt 1 below
this check authentication token against database. can spot wrong here?
var checkauth = function(auth) { var rs = 0; var sql = 'select * clients pword=\''+auth+'\''; // copied debug session , got results: select * clients pword='d98e623c7a74a178703d17e1fd536b1488724acd41e71f178331c768c385bda2c82d2bcb60cbb4650be375ad4734c63fb694bd164c138f9abe0c51f37f9a7e33' var query = connection.query(sql); query .on('error', function(err) { console.log( err ); }) .on('result', function( row ) { rs = number(row.client_id); // never fires!!?!?! }) .on('end',function(){ }); if (rs == 0) { sql = 'select * users pword=\''+auth+'\''; query = connection.query(sql); query .on('error', function(err) { console.log( err ); }) .on('result', function( row ) { rs = number(row.client_id); // never fires!!?!?! }) .on('end',function(){ }); } return rs; }
edit 1: use follwoing loop authenticate socket.io connections runs function, ok understand have wait db finish, put callback?
io.use(function(socket, next){ console.log("query: ", socket.handshake.query); // return result of next() accept connection. socket.clientid = 0; socket.clientid = checkauth(socket.handshake.query.auth); console.log("cid:"+socket.clientid); if (socket.clientid != 0) { return next(); } // call next() error if need reject connection. next(new error('authentication error')); });
edit 2/ attempt 1 according johannes' suggest:
var checkauth = function(auth, cb) { var rs = 0; var sql = 'select * clients pword=?'; var query = connection.query(sql, [auth]); query .on('error', function(err) { console.log(err); }) .on('result', function(row) { rs = number(row.client_id); }) .on('end', function() { if (rs == 0) { sql = 'select * users pword=?'; query = connection.query(sql, [auth]); query .on('error', function(err) { console.log(err); }) .on('result', function(row) { rs = number(row.client_id); }) .on('end', function() { cb(rs); }); } }); } io.use(function(socket, next){ console.log("query: ", socket.handshake.query); // return result of next() accept connection. socket.clientid = 0; var auth = socket.handshake.query.auth; checkauth(auth, function(clientid){ socket.clientid = clientid; if (clientid != 0) { return next(); } console.log('clienid', clientid); next(new error('authentication error')); }); // if (socket.clientid != 0) { // return next(); // } // // call next() error if need reject connection. // next(new error('authentication error')); });
this whole process async. have 2 things:
move event callback , add callback or promise checkauth function. should escape inserted data.
edited solution
var checkauth = function(auth, cb) { var rs = 0; var sql = 'select * clients pword=?'; var query = connection.query(sql, [auth], (err, client) => { if(err || !client || client.length < 1) return cb(err || new error('unkown client')); sql = 'select * users pword=?'; query = connection.query(sql, [auth], (err, user) => { if(err || !user || user.length < 1) return cb(err || new error('unkown user')); cb(null, client[0].client_id); }); }); }
note: why doing 2 queries in first place , storing password each client of user?
original answer:
var checkauth = function(auth, cb) { var rs = 0; var sql = 'select * clients pword=?'; var query = connection.query(sql, [auth]); query .on('error', function(err) { console.log(err); }) .on('result', function(row) { rs = number(row.client_id); // never fires!!?!?! }) .on('end', function() { if (rs == 0) { sql = 'select * users pword=?'; query = connection.query(sql, [auth]); query .on('error', function(err) { console.log(err); }) .on('result', function(row) { rs = number(row.client_id); // never fires!!?!?! }) .on('end', function() { cb(rs); }); } }); } checkauth(123, function(clientid){ console.log('clienid', clientid); });
Comments
Post a Comment