向除发送者以外的所有客户端发送响应 [英] Send response to all clients except sender

查看:21
本文介绍了向除发送者以外的所有客户端发送响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要向所有客户端发送内容,请使用:

To send something to all clients, you use:

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

要从客户那里接收信息,您可以使用:

To receive from clients, you use:

socket.on('cursor', function(data) {
  ...
});

如何将两者结合起来,以便在服务器上从客户端接收消息时,将该消息发送给除发送消息的用户之外的所有用户?

How can I combine the two so that when recieving a message on the server from a client, I send that message to all users except the one sending the message?

socket.on('cursor', function(data) {
  io.sockets.emit('response', data);
});

我是否必须通过发送带有消息的客户端 ID 然后在客户端检查来破解它,还是有更简单的方法?

Do I have to hack it around by sending the client-id with the message and then checking on the client-side or is there an easier way?

推荐答案

这是我的列表(更新为 1.0):

// sending to sender-client only
socket.emit('message', "this is a test");

// sending to all clients, include sender
io.emit('message', "this is a test");

// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");

// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');

// sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'cool game');

// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');

// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');

// sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'for your eyes only');

// list socketid
for (var socketid in io.sockets.sockets) {}
 OR
Object.keys(io.sockets.sockets).forEach((socketid) => {});

这篇关于向除发送者以外的所有客户端发送响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆