我想通过socketIO通过MySQL数据库进行身份验证。我建立了连接并可以查询结果而没有问题,但由于某种原因,我无法通过用户是否被认证到socketio的connection
部分。这个想法是我的应用程序有主持人和观众。如果连接到应用程序时未在QueryString
中发送密码,则该应用程序假定其为查看器并接受连接。如果发送密码,则会对数据库进行检查并接受/拒绝连接。我想要一个变量传入connection
,这样我就可以在我的应用程序事件中使用它。以下是我目前为止的内容,但显然data.query['ishost']
没有传入应用。SocketIO + MySQL身份验证
sio.configure(function() {
sio.set('authorization', function (data, accept) {
UserID = data.query['username'];
try {
UserID = UserID.toLowerCase();
} catch(err) {
return accept("No WebBot Specified. ("+err+")", false);
}
// if not sending a password, skip authorization and connect as a viewer
if (data.query['password'] === 'undefined')
{
return accept(null, true);
}
// if sending a password, attempt authorization and connect as a host
else
{
client.query(
'SELECT * FROM web_users WHERE username = "'+UserID+'" LIMIT 1',
function selectCb(err, results, fields) {
if (err) {
throw err;
}
// Found match, hash password and check against DB
if (results.length != 0)
{
// Passwords match, authenticate.
if (hex_md5(data.query['password']) == results[0]['password'])
{
data.query['ishost'] = true;
accept(null, true);
}
// Passwords don't match, do not authenticate
else
{
data.query['ishost'] = false;
return accept("Invalid Password", false);
}
}
// No match found, add to DB then authenticate
else
{
client.query(
'INSERT INTO web_users (username, password) VALUES ("'+UserID+'", "'+hex_md5(data.query['password'])+'")', null);
data.query['ishost'] = "1";
accept(null, true);
}
client.end();
}
);
// Should never reach this
return accept("Hacking Attempt", false);
}
// Definitely should never reach this
return accept("Hacking Attempt", false);
});
});
写入data.query
使其可通过handshakeData访问。但由于某种原因,它没有通过应用程序传递。任何帮助表示赞赏,谢谢。
很好用。我会看看passport.js。谢谢你的帮助。 –
如果你回退到闪存套接字,这个工作会吗? – NightWolf
我不确定,我通常会避免使用Flash套接字来简化我的服务器配置 –