Sockets 套接字向特定用户发出

Sockets 套接字向特定用户发出,sockets,express,Sockets,Express,我在同一主题上发现了几个问题,但没有一个对我有用。我想这是因为我在这个过程中遗漏了一些东西: 我想向特定用户发送消息。 我使用: 这是我的密码: 服务器(如您所见,我使用mongoose处理会话): 然后是socketRouter函数。我将用户配置文件的socketId存储在mongo数据存储中,以使用emit来瞄准用户 exports = module.exports = (io) => { io.sockets.on('connection', (socket) => {

我在同一主题上发现了几个问题,但没有一个对我有用。我想这是因为我在这个过程中遗漏了一些东西:

我想向特定用户发送消息。 我使用:

这是我的密码:

服务器(如您所见,我使用mongoose处理会话):

然后是socketRouter函数。我将用户配置文件的socketId存储在mongo数据存储中,以使用emit来瞄准用户

exports = module.exports = (io) => {
  io.sockets.on('connection', (socket) => {
    Users.findById(socket.decoded_token.sub, (err, user) => {
      if (user) {
        // if the user exists, save the socket Id in the user collection
        Users.update({_id: user._id}, {$set: {socketId: socket.id}}, (err2, result) => {

          // ------ PROTECTED ROUTES ------ //

          // MOBILE CALLS
          ...

          // DASHBOARD CALLS
          socket.on('forceNotif', (data) => Notif.force(data, io, socket));

          // ------------------------------ //
        });
      }
    });
    socket.on('disconnect', ()=> {
      Users.update({_id: socket.decoded_token.sub}, {$set: {socketId: null}});
    });
  });
“forceNotif”调用的函数。在这里,我期待一种不同的行为。我希望套接字将值发送给特定用户(与发送请求的用户不同)。我从MongoDb检索到socketId,它是准确的。然后我想用它来达到我的目的。在网络上提出了几种不同的主张。我测试了以下各项:

exports.force = (par, io, socket) => {
    // retrieve the socket id of the user to aim
  Users.findOne({_id: par.id}, {socketId: 1, pseudo: 1}, (err, res) => {
    console.log('--------------------------------------------------');
    console.log('err: ', err);
    console.log('pseudo: ', res.pseudo);
    console.log('socketId: ', res.socketId);
    // emit the value to a specific user


    // not working

    io.sockets.to(res.socket).emit('notifExtUpdate', {val: 'TO'});
    io.to(res.socket).emit('notifExtUpdate', {val: 'TO'});
    io.broadcast.to(res.socket).emit('notifExtUpdate', {val: 'TO'});
    socket.broadcast.to(res.socket).emit('notifExtUpdate', {val: 'TO'});
    socket.to(res.socket).emit('notifExtUpdate', {val: 'TO'});


    // working well, but not my purpose

    io.emit('notifExtUpdate', {val: 'BROADCAST'});
    socket.broadcast.emit('notifExtUpdate', {val: 'BROADCAST'});
  });
};

我希望有人能帮助我:-)

所以我找到了解决方案: 插座上写着“每个插座自动连接一个由id标识的房间”。但由于某些原因,我仍然不明白,当我以以下方式发射时(socketId在mongoDb中存储、检索和检查),什么都没有发生:

socket.broadcast.to(socketId).emit('blabla', msg);
但最后,我通过以下方式手动将用户加入到私人房间:

  io.on('connection', (socket) => {
    Users.findById(socket.decoded_token.sub, (err, user) => {
      if (user) {
        // create a room for every user
        socket.join(user._id);

        socket.on('exemple', (data) => functionExemple(data, io));
        ...
然后我可以向FunctionExample中的特定用户发出如下信息(其中targetId是集合中用户的_id):

我希望它能帮助某人:-)

socket.broadcast.to(socketId).emit('blabla', msg);
  io.on('connection', (socket) => {
    Users.findById(socket.decoded_token.sub, (err, user) => {
      if (user) {
        // create a room for every user
        socket.join(user._id);

        socket.on('exemple', (data) => functionExemple(data, io));
        ...
exports.functionExemple= (par, io) => {
  returnEmit.to(targetId).emit('blabla', msg);
};