【问题标题】:socket.broadcast.to().emit() vs socket.to().emit()socket.broadcast.to().emit() 与 socket.to().emit()
【发布时间】:2019-12-18 20:50:43
【问题描述】:

有什么区别:

socket.broadcast.to().emit()

socket.to().emit()

socket.io cheat sheet 上面写着

  socket.to('game').emit('nice game', "let's play a game"); // sending to all clients in 'game' room except sender

然后在this tutorial blog 中说:

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)

所以现在我真的很困惑。

  1. 其中一个解释有误吗?

  2. 仅当他们在“游戏”房间(频道)中时发送给发送者客户端是什么意思?这是否意味着如果是群聊,只有一个人(消息的发送者)会收到它?这是什么意思?

  3. 如果socket.io cheatsheet 是准确的,如果发送到“游戏”房间中除发件人之外的所有客户端通过调用以下方式完成,.broadcast.emit() 的目的是什么:@ 987654326@? socket.broadcast.emit() 是否仅用于发送到同一命名空间上的所有客户端(发送者除外)?但是,如果您想发送给特定房间中的所有客户(发件人除外),您使用socket.to().emit() 而不是socket.broadcast.to().emit()

谢谢。

【问题讨论】:

    标签: sockets websocket socket.io


    【解决方案1】:

    在“https://dev.to/moz5691/socketio-for-simple-chatting---1k8n

    socket.to('game').emit('message', '享受游戏'); //发送给发送者客户端,仅当他们在“游戏”房间(频道)中”。

    此特定发射仅在游戏室通道中从服务器到发送者客户端。

    然而广播, socket.broadcast.to('game').emit('message', 'nice game'); 发送到“游戏”房间(频道)中的所有客户端,发送者除外。

    注意到他解释的以下段落中还有一个错误:

    客户端 --> 发送消息 --> 服务器

    服务器

    应该是客户端

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 2018-04-26
      • 1970-01-01
      • 2016-08-08
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多