【问题标题】:Emit message to certain socket id in socket 2.0向套接字 2.0 中的特定套接字 id 发送消息
【发布时间】:2018-02-23 16:43:00
【问题描述】:

我正在使用套接字 2.0 进行聊天,我想将消息从服​​务器端发送到我存储在数组中的某个套接字 ID。我该怎么做,我在 socket.io 文档中看不清楚。

【问题讨论】:

    标签: javascript socket.io


    【解决方案1】:

    发件人:https://socket.io/docs/emit-cheatsheet/

    // sending to individual socketid (private message)
      socket.to(<socketid>).emit('hey', 'I just met you');
    

    【讨论】:

    • 如果 OP 还没有碰巧在 socket.io 事件中,因此 OP 可能想要 io.to(&lt;socketid&gt;).emit(),因此手边没有 socket 对象。
    • 正确,to 方法应该对套接字实例和框架实例都可用。
    猜你喜欢
    • 2014-08-26
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多