node.js - Socket.io Private messaging, with multiple users online at the same time -
So I'm building chat app and need help to know about sending private messages. The code is 'Send Message'
User = {} socket.on ('Send Message', Function (Data, Callback) {var msg = data.trim (); console.log ( User); console.log ('Trim after message:' msg); var name = req.params.posteruname; // reciever var msg = msg; if (name in user) {var message = new chat ({msg Msg msg, sender: req User.username, reciever: name}). Save (function (err, savedMessage) {if (err) {Users [name] .emit ('whisper', {msg: "error, some tried to send you a message but something went wrong", nick: socket. Name)}} other {user [name] .emit (' Whisper ', {receiver: name, sender: req.user.username, msg: msg});}});} and {callback ("something went wrong");}});
This code is not working very well when I try to send a message, it still displays to all users.
Take a look at this. You should specify the rooms that everyone will have a "room", when you send a message with the event and specify in the room that the right user will get it.
Your user
object is just a plain object, don't reflect what I'm talking about.
Comments
Post a Comment