程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将响应发送给除发件人以外的所有客户端大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决将响应发送给除发件人以外的所有客户端?

开发过程中遇到将响应发送给除发件人以外的所有客户端的问题如何解决?下面主要结合日常开发的经验,给出你关于将响应发送给除发件人以外的所有客户端的解决方法建议,希望对你解决将响应发送给除发件人以外的所有客户端有所启发或帮助;

这是我的列表 (已更新为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) => {});

解决方法

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

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

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

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

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

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

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

大佬总结

以上是大佬教程为你收集整理的将响应发送给除发件人以外的所有客户端全部内容,希望文章能够帮你解决将响应发送给除发件人以外的所有客户端所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。