本文介绍了如何在内部发布方法以在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连接中添加。
I am trying to store mongolab(db)one username and password statically to collect the mongolab into my actual coding to comapare from username and passowrd. Everything will be access correctly but I cannot add from the socket.io connections in post method.
如何在post方法中添加socket.io(打开或发射)? / p>
How to add socket.io (on or emit) in post method?
推荐答案
将 io.connection
从中移出app.post
,并将客户端记录在另一个变量中
Move the io.connection
out of app.post
, and record the client in another variable
var ioClient = null;
io.on('connection', function(client) {
console.log('Client connected...');
ioClient = client;
});
然后在<$中发送 emit
消息c $ c> app.post 如下,
Then to emit
message in the app.post
as below,
app.post('/form-data', urlencodedParser, function (req, res) {
...
// handling socket.io
if (ioClient)
ioClient.emit('messages', 'username & password is incorrect');
});
这篇关于如何在内部发布方法以在node.js中添加套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!