[英]How to post method inside add the socket connections in node.js
var admins = db.collection('admin');
app.post('/form-data', urlencodedParser, function (req, res) {
response = {
Username: req.body.Username,
Password: req.body.Password
};
console.log(response);
var exists = false;
admins.find().toArray(function (err, key) {
var length = key.length;
if (key[0].username.toLowerCase() === req.body.Username.toLowerCase() && key[0].password.toLowerCase() === req.body.Password.toLowerCase())
{
res.redirect('/chat/');
app.get('/chat/', function (req, res) {
res.render('chat');
});
}
else
{
res.redirect('/');
app.get('/', function (req, res) {
res.render('index');
console.log("palanivelm");
socket.emit('messages', 'username & password is incorrect');
});
//io.on('connection', function(client) {
// console.log('Client connected...');
// client.emit('messages', 'username & password is incorrect');
//});
}
});
我试图静态存储mongolab(db)一个用户名和密码,以将mongolab收集到我的实际编码中,以便从用户名和passowrd进行comapare。 一切都将正确访问,但我无法在post方法中从socket.io连接中添加。
如何在post方法中添加socket.io(打开或发射)?
将io.connection
移出app.post
,并将客户端记录在另一个变量中
var ioClient = null;
io.on('connection', function(client) {
console.log('Client connected...');
ioClient = client;
});
然后要在app.post
emit
消息,如下所示,
app.post('/form-data', urlencodedParser, function (req, res) {
...
// handling socket.io
if (ioClient)
ioClient.emit('messages', 'username & password is incorrect');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.