在以下方面有何区别:
socket.broadcast.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)所以现在我真的很困惑。
socket.to('game').emit();?socket.broadcast.emit()是否仅用于向同一名称空间中的所有客户端发送(发送方除外)?但是,如果您想发送到特定房间中的所有客户端(发送方除外),则使用socket.to().emit()而不是socket.broadcast.to().emit()。谢谢。
发布于 2020-01-15 05:54:39
关于"https://dev.to/moz5691/socketio-for-simple-chatting---1k8n“
Socket.to(‘游戏’).emit(‘消息’,‘享受游戏’);//发送到发送客户端,只有当他们在‘游戏’房间(频道)“。
此特定发出仅在游戏室通道中从服务器发送到发送方客户端。
然而,广播,socket.broadcast.to(‘游戏’).emit(‘message’,‘不错的游戏’);发送到‘游戏’房间(频道)的所有客户端,除了发送者。
注意到以下段落中还有一个错误,他解释道:
客户端-> sendMesssage ->服务器
服务器<-- receiveMessage --服务器
它应该是客户端<-- receiveMessage --服务器。
希望这能有所帮助。
https://stackoverflow.com/questions/57458399
复制相似问题