【问题标题】:WebRTC channelMessage not recieving first messageWebRTC channelMessage 没有收到第一条消息
【发布时间】:2020-05-12 21:09:51
【问题描述】:

使用 WebRTC sendDirectlyToAll 发送消息时,第一次永远不会收到消息,但之后每次都会收到消息。 我现在已经将代码剥离到一个非常简单的状态,但它仍然是一样的。有人知道为什么会这样吗?

代码如下:

var webrtc = new SimpleWebRTC({
  localVideoEl: 'localVideo',
  remoteVideosEl: 'remoteVideos',
  autoRequestMedia: false,
  media: {
    video: true,
    audio: false
  },
  localVideo: {
    autoplay: true,
    mirror: true,
    muted: true
  }
});

$("#chat-send-button").on("click", function (e) {
  sendMessage();
});

function sendMessage() {
  console.log("sendMessage");
  const chatMessage = $("#chat-message-input");
  webrtc.sendDirectlyToAll(
    "chat",
    "info", {
      "chatmessage": chatMessage.val()
    }
  )
  chatMessage.val("");
}

webrtc.on("channelMessage", function (peer, channel, data) {
  console.log(peer);
  console.log(channel);
  console.log("data", data);
  $("#chat-message-container").text(data.payload.chatmessage);
});

【问题讨论】:

    标签: javascript webrtc chat


    【解决方案1】:

    您可能需要在允许用户发送消息之前建立 WebRTC 连接 - 您是否使用文档 https://github.com/SimpleWebRTC/SimpleWebRTC#3-tell-it-to-join-a-room-when-ready 中描述的 readyToCall 事件

    (指向可编辑的可运行代码 sn-p 的链接可能会有所帮助)

    【讨论】:

      猜你喜欢
      • 2012-10-11
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多