2016-05-21 23 views
0

非常にカジュアルなコメントシステムを作成しましたが、今では返信を追加したいと思います。だから、誰かが他の人のコメントに返信を投稿したとき、そのユーザーはであることが通知されている必要があります。誰かがコメントに返信しました。これを行うために、回答者が返信ボタンをクリックするとAJAXの投稿リクエストがサーバーに送られ、サーバーは最初の投稿者のIDを取得し、socket.io(socket.io is別のモジュールで返信テキストを送信したり、自分自身を表現する別の方法がある場合には、使用する必要はありません。これは私のコードは、これまでのところです: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

ああともう一つ、私は特急のユーザーIDとsocket.ioのユーザーIDをバインドすることができますか?おそらくJSONオブジェクトのようなものですが、どうすれば他のオブジェクトを取得できますか? – Jim

+0

ルックアップテーブル、 'commenterId'のキーを持つオブジェクト、socket.io接続の値が必要です。返信可能なデータを作成するときにこれを作成/更新します。 – dandavis

+0

しかし、明示的なユーザーIDとsocket.ioユーザーIDを1つのJSONオブジェクトに保存するにはどうすればよいですか。エクスプレスユーザIDをsocket.ioユーザIDに「接続」する必要がありますが、express()を使用してソケットIDを取得する方法はわかりません。逆に、私はsocket.ioユーザIDを使って明示的なユーザIDを取得する方法を知らない。何とかこれをやることができますか? – 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が保存されます。 –

関連する問題