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

Hello, this emits to all the sockets, but each socket gets the same data, is it possible for me to emit to them all, but give each of them different data?

有帮助吗?

解决方案

As far as I know this is not possible. It will depend on what you want to do really (and how). First thing that came to my mind as a alternative is identifying your users by socket.id and then send them the data accordingly.

Example:

io.sockets.clients().forEach(function (socket) {
    if(socket.id == 1337) data = {test: "test1"};
    if(socket.id == 404) data = {test: "test2"};

    socket.emit('data',data);
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top