2016-05-21 85 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。 –

相關問題