简体   繁体   English

Node.js:在express.js路由中使用Socket.io将消息发送到特定客户端

[英]Node.js: Using Socket.io in an express.js route to send message to a specific client

I have made a very casual commenting system, and now I want to add replies. 我做了一个非常随意的评论系统,现在我想添加回复。 So, when someone posts a reply on someone else's comment, that user must be notified that someone replied to their comment. 因此,当某人对他人的评论发表回复时,必须通知该用户某人已回复了他们的评论。 In order to do that, when the replier clicks the reply button an AJAX post request is made to the server, the server then needs to get the id of the first commenter and send them a response text using socket.io (socket.io is not required to be used if there is another way to send the reply text with another module or express itself). 为此,当回复者单击回复按钮时,会向服务器发出AJAX发布请求,然后服务器需要获取第一个评论者的ID,并使用socket.io向他们发送响应文本(socket.io为如果有另一种方法可以通过另一个模块发送回复文本或表达自己,则不需要使用。 This is my code so far: 到目前为止,这是我的代码:

app.post('/reply', function(req, res){
  var commenterId = req.body.userId; // this is the id of the original commenter, not the replier
  User.findOne({'_id':commenterId}, function(err, user){
    user.send({'replied': req.user._id}); // this is what I need to do, and
  //I don't know if this specific code works and that's why I'm asking if there is a way to do it with socket.io,
  // io.to(socketId).emit('reply', 'some reply text here...'); // but if I do this I don't know how to get the socketId!
  //Is there even a way to do this? Maybe with another module,
  //or some express function I don't know about? And if it is done with express how would
  //the client side code, look like? Thank you!
  });
  res.end();
});
//app.js file   
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var routes = require('./routes/routes')(io);
app.use('/', routes);

//router file
var express = require('express');
var router = express.Router();
var _socket = null;

//list of socket users.once they logout delete the socket by 
//delete users[_socket.userid];
var users = {};

var returnRouter = function(io) {

    io.sockets.on('connection', function (socket) {
        //now _Socket is available inside routes
       _socket =  socket;
    });

    router.post('/login', function(req, res) {
        //authentication logic
        User.findOne({'email': req.body.email}, function (err, user) {

           //userid must be unique
           _socket.userId= user.userId
           //set session variable to store id of the user
           req.session.userId = user.userId;
           //now every user has a socket associated with their id
           users[_socket.userId] = _socket;
        });
    });

    router.post('/reply', function (req, res) {
       var commenterId = req.body.userId;
       User.findOne({'_id': commenterId}, function (err, user) {

       // you can get the id of the logged in user that is the creator
       //of the original post from req.session.userId
       //if you have implemented session store

       //the commenter user object is obtained from findOne method
       users[req.session.userId].emit('notification', {
         notification: user.username+' commented on your post'
       }});
       });
       res.end();
    });

    return router;
};
module.exports = returnRouter;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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