简体   繁体   English

如何使用 Socket.io 向 node.js 中的特定客户端发送事件/消息?

[英]How to send events/messages to specific clients in node.js using Socket.io?

i'm working on a Node.js app in which we are using socket.io for sending and receiving events/messages to clients.我正在开发一个 Node.js 应用程序,其中我们使用 socket.io 向客户端发送和接收事件/消息。 Initially it is sending events to clients using the following statement.最初,它使用以下语句向客户端发送事件。

io.sockets.emit('SequenceLoad', data); io.sockets.emit('SequenceLoad', data);

But now i want to send events only to specific clients using client specific sockets.但是现在我只想使用客户端特定的套接字将事件发送到特定的客户端。 I have searched alot regarding this, found some ways but none of them seems to work, found on various SO posts.我已经对此进行了大量搜索,找到了一些方法,但在各种 SO 帖子中找到了它们似乎都不起作用。

Here is the code that i have tried:这是我尝试过的代码:

const io = require('socket.io')(http);
var socketsCollection = [];
io.on('connection', function (socket) {

  //Saving socket id's for sending events to specific sockets based on their ids
  socketsCollection.push(socket.id);
  socket.on('SequenceLoadServer', function (data) {
     // logger.info('Socket Server | Event: sequenceLoadServer with socket server. \n Data:'+ data);
     console.log("In APP.js for SequenceLoadServer event");
     console.log("Socket Id :" + socket.id);
     var socketId = null;
     setTimeout(function () {
        for (var item of socketsCollection) {
           if(item == socket.id){
              socketId= item;
              break;   
           }
        }
        console.log("Socket after searching Id :" + socketId);
        io.sockets.emit('SequenceLoad', data);   -> This works at the moment

        //Tried the following ways as well but none of them worked 

        //io.sockets.connected[socketId].emit('SequenceLoad', data);
        //io.sockets.socket(socketId).emit('SequenceLoad', data); 
        //io.to(socketId).emit('SequenceLoad', data);
        //io.to('/#' + socketId).emit('SequenceLoad', data);
        //io.to(`${socketId}`).emit('SequenceLoad', data);

     }, 1000 * 3);

  });
}

Version of socket.io in my project:我的项目中的 socket.io 版本:

"socket.io": "^2.2.0",
"socket.io-client": "^2.2.0"

Any help would great, correct me if i'm doing wrong because i'm new to it.任何帮助都会很棒,如果我做错了,请纠正我,因为我是新手。 Thanks谢谢

Given you have the socket instance, you can send to a specific client directly by: 鉴于您有套接字实例,您可以通过以下方式直接发送给特定客户端:

io.to(`${socket.id}`).emit(...);

See Emit cheatsheet Emit cheatsheet

You could keep an object of all sockets connected, where the key is the socket's id and the value can be the socket itself: 您可以保持所有套接字的对象连接,其中键是套接字的id ,值可以是socket本身:

let SOCKET_LIST = {};
io.on('connection', function (socket) {
  SOCKET_LIST[socket.id] = socket;
  // more code...
  // retrieving the socket
  selected_socket = SOCKET_LIST[id];
  selected_socket.emit('SequenceLoad', data);

  // Ensure that you delete the socket once it has disconnected:
  socket.on('disconnect', function() {
    delete SOCKET_LIST[socket.id];
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM