0

我在WebRtc使用 javascript 集成视频和聊天时遇到问题。我正在使用这个例子

https://github.com/oney/react-native-webrtc-server/blob/master/index.html

视频工作正常,但发送消息失败也没有接收错误!!!!。作为上述示例中共享的数据通道,这些功能:

Line #115 to 142 of above link
if (pc.textDataChannel) {
  return;
}
var dataChannel = pc.createDataChannel("text");

dataChannel.onerror = function (error) {
};

dataChannel.onmessage = function (event) {
  console.log('open state2')

  var content = document.getElementById('textRoomContent');
  content.innerHTML = content.innerHTML + '<p>' + socketId + ': ' + event.data + '</p>';
};

dataChannel.send = function (event) {
  console.log('open state2')
  exchange(event)
};

dataChannel.onopen = function () {
  console.log('open state')
  var textRoom = document.getElementById('textRoom');
  textRoom.style.display = "block";
};

dataChannel.onclose = function () {
};
pc.textDataChannel = dataChannel;

在发送消息时它的用途:

pc.textDataChannel.send(text);//Line #218 of above link

我到底应该实现什么才能起作用我认为这个发送没什么,这就是为什么消息失败和视频工作

4

1 回答 1

0

您应该在 createOffer 之前调用 createDataChannel。

更简单的方法是在创建 RTCPeerConnection 后立即创建 dataChannel

var pc = new RTCPeerConnection(configuration);
var dataChannel = pc.createDataChannel("text");
...

然后 createOffer 将包含数据通道描述,在 SDP 中您应该看到如下内容:

m=application 9 DTLS/SCTP 5000
...
a=sctpmap:5000 webrtc-datachannel 1024
于 2017-09-17T17:27:57.013 回答