2016-05-21 84 views
0

我做了一个非常随意的评论系统,现在我想添加回复。所以,当有人发表回复他人的评论时,该用户必须通知有人回复了他们的评论。为了做到这一点,当回答者点击答复按钮时,向服务器发出AJAX发布请求,然后服务器需要获得第一个评论者的ID并使用socket.io向他们发送响应文本(socket.io是如果有另一种方式将回复文本发送到另一个模块或表达自己,则不需要使用它们)。这是到目前为止我的代码:Node.js:在express.js路径中使用Socket.io将消息发送到特定客户端

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(); 
}); 
+0

哦,还有一两件事,我怎么可以绑定一个socket.io用户ID与明确的用户ID?也许像一个JSON对象,但我怎么用一个得到另一个? – Jim

+0

你需要一个查找表,一个带'commenterId'键和socket.io连接值的对象。您在创建可回复数据时进行更新。 – dandavis

+0

好吧,但是如何将快速用户标识和socket.io用户标识保存在一个JSON对象中。我需要将express用户标识“附加”到socket.io用户标识,但我不知道如何使用express来获取它(socket.io用户标识)。而相反,我不知道如何使用socket.io用户标识获取快捷用户标识。我能以某种方式做到这一点吗? – Jim

回答

1
//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; 
+0

但是,当用户导航到我的网站内的另一个页面时,不会更改套接字ID吗? – Jim

+0

完全读取代码'users [_socket.userId] = _socket;'存储即使在refiresh上它仍然存在的socket.so。 –

相关问题