socket.broadcast.to().emit() vs socket.to().emit() [英] socket.broadcast.to().emit() vs socket.to().emit()
问题描述
有什么区别:
socket.broadcast.to().emit()
socket.broadcast.to().emit()
和
socket.to().emit()
socket.to().emit()
在 socket.io 备忘单上写着
socket.to('game').emit('nice game', "let's play a game"); // sending to all clients in 'game' room except sender
然后在本教程博客中说:
socket.broadcast.to('game').emit('message', 'nice game'); //sending to all clients in 'game' room(channel) except sender
socket.to('game').emit('message', 'enjoy the game'); //sending to sender client, only if they are in 'game' room(channel)
所以现在我真的很困惑.
So now I am really confused.
其中一种解释是错误的吗?
Is one of this explanations wrong?
发送给发件人客户端,仅当他们在游戏"房间(频道)中时是什么意思?这是否意味着如果是群聊,只有一个人(消息的发送者)会收到它?这是什么意思?
What does sending to sender client, only if they are in 'game' room(channel) mean? Does it mean if it is a group chat only one person(the sender of the message) will receive it? What does it mean?
如果 socket.io 备忘单是准确的,目的是什么.broadcast.emit() 如果发送到'游戏'房间中除发送者之外的所有客户端是通过调用完成的:socket.to('game').emit();
?socket.broadcast.emit()
是否仅用于发送给同一命名空间上的所有客户端(发送方除外)?但是,如果您想发送给特定房间中的所有客户端(发送者除外),您可以使用 socket.to().emit()
而不是 socket.broadcast.to().emit()
?
If the socket.io cheatsheet is accurate, what is the purpose of .broadcast.emit() if sending to all clients in 'game' room except sender is done by calling: socket.to('game').emit();
? Is socket.broadcast.emit()
only used to send to all clients on the same namespace(except the sender)? But if you want to send to all clients in a particular room (except the sender) you use socket.to().emit()
instead of socket.broadcast.to().emit()
?
谢谢.
推荐答案
On "https://dev.to/moz5691/socketio-for-simple-chatting---1k8n
socket.to('game').emit('message', '享受游戏');//发送给发送方客户端,仅当他们在'游戏'房间(频道)".
socket.to('game').emit('message', 'enjoy the game'); //sending to sender client, only if they are in 'game' room(channel)".
这个特定的发射仅在游戏室频道中从服务器到发送方客户端.
This particular emit is from server to the sender client only in the game room channel.
然而广播,socket.broadcast.to('game').emit('message', '不错的游戏');发送给游戏"房间(频道)中除发送者之外的所有客户端.
However the broadcast, socket.broadcast.to('game').emit('message', 'nice game'); Sends to all clients in 'game' room(channel) except sender.
注意到他解释的以下段落中还有一个错误:
Noticed there is one more error in the following paragraph where he explains:
客户端 --> sendMesssage --> 服务器
Client --> sendMesssage --> Server
服务器 <-- 接收消息 -- 服务器
Server <-- receiveMessage -- Server
应该是Client <-- receiveMessage -- Server.
It should be Client <-- receiveMessage -- Server.
希望有所帮助.
这篇关于socket.broadcast.to().emit() vs socket.to().emit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!