基本WebRTC DataChannel MCVE无法在Chrome中运行

时间:2016-02-17 18:27:46

标签: javascript google-chrome webrtc

我正在开发一个使用WebRTC DataChannel的网络应用,但我无法在Chrome中打开频道。我已将其简化为一个简单的测试,在同一页面上打开两个RTCPeerConnection实例,并在代码中直接发送信号(jsfiddle):

'use strict';

var RTCPC_CONFIG = {
  'iceServers': [
    { 'urls': 'stun:stun.l.google.com:19302' },
  ]
};

var RTCPC_OPTIONAL = { optional: [{ RtpDataChannels: true }] };

var DATACHANNEL_OPTIONS = {
};

// Create offering peer and data channel.
var offPeer = new RTCPeerConnection(RTCPC_CONFIG, RTCPC_OPTIONAL);
var offChan = offPeer.createDataChannel('off', DATACHANNEL_OPTIONS);

offChan.onopen = function() {
  console.log('***** offering channel open *****');
};

// Create answering peer and data channel.
var ansPeer = new RTCPeerConnection(RTCPC_CONFIG, RTCPC_OPTIONAL);
var ansChan = ansPeer.createDataChannel('ans', DATACHANNEL_OPTIONS);

ansChan.onopen = function() {
  console.log('***** answering channel open *****');
};

// Create the WebRTC offer.
var exchangeDescriptions = offPeer.createOffer().then(function(offer) {
  offPeer.setLocalDescription(offer);
  console.log('offer\n' + JSON.stringify(offer, null, 2));

  // Set the offer on the other peer.
  return ansPeer.setRemoteDescription(offer);
}).then(function() {
  // Create the WebRTC answer.
  return ansPeer.createAnswer();
}).then(function(answer) {
  ansPeer.setLocalDescription(answer);
  console.log('answer\n' + JSON.stringify(answer, null, 2));

  // Set the answer on the other peer.
  return offPeer.setRemoteDescription(answer);
});

// Collect and exchange ICE candidates.
offPeer.onicecandidate = function(e) {
  if (e.candidate) {
    var ice = e.candidate;
    exchangeDescriptions.then(function() {
      // Add ICE candidate to other peer.
      console.log('offer ICE\n' + JSON.stringify(e.candidate, null, 2));
      ansPeer.addIceCandidate(ice);
    });
  }
  else
    console.log('offer ICE done');
};

ansPeer.onicecandidate = function(e) {
  if (e.candidate) {
    var ice = e.candidate;
    exchangeDescriptions.then(function() {
      // Add ICE candidate to other peer.
      console.log('answer ICE\n' + JSON.stringify(e.candidate, null, 2));
      offPeer.addIceCandidate(ice);
    });
  }
  else
    console.log('answer ICE done');
};

请注意,jsfiddle page我正在使用WebRTC adapter将浏览器API填充到规范中。

当您打开Javascript控制台运行此脚本时,我希望在所有日志记录中看到DataChannel报告打开:

***** offering channel open *****
***** answering channel open *****

我确实在Firefox 43上看到了这一点,但在Chrome 48(稳定版)或50版(金丝雀版)上看不到。

Chrome出错了哪里?我尝试过使用DataChannel的其他示例,但有些工作,所以我知道Chrome支持它。示例代码通常使用旧的或特定于浏览器的API,我宁愿不复制。据我所知,我正在做同样的事情,但事实显然并非如此。

1 个答案:

答案 0 :(得分:1)

啊哈,问题似乎是我从几个网站上的示例代码中选择的这个设置:

var RTCPC_OPTIONAL = { optional: [{ RtpDataChannels: true }] };

显然RtpDataChannels是遗留的Chrome内容no longer supported。如果我从RTCPeerConnection构造函数中删除那个额外的参数,那么things work on Chrome

我在Chrome上filed an issue