向发件人以外的所有客户发送回复 [英] Send response to all clients except sender

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

问题描述

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

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);
});

我是否必须通过发送带有消息的client-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');

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

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