如何在客户端使用JavaScript检查webRTC数据通道兼容性?

如何在客户端使用JavaScript检查webRTC数据通道兼容性?,javascript,html,webrtc,Javascript,Html,Webrtc,WebRTC数据通道只在Firefox中每晚工作。如何在客户端进行检查 代码如下所示 if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits); var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a numbe

WebRTC数据通道只在Firefox中每晚工作。如何在客户端进行检查

代码如下所示

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits); 

 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number

 if (ffversion>=5)

  document.write("You're using FF 5.x or above")

 else if (ffversion>=4)

  document.write("You're using FF 4.x or above")

 else if (ffversion>=3)

  document.write("You're using FF 3.x or above")

 else if (ffversion>=2)

  document.write("You're using FF 2.x")

 else if (ffversion>=1)

  document.write("You're using FF 1.x")
}

else
 document.write("n/a")

您可以简单地测试浏览器当前是否支持要使用的功能。例如:

if (!window.mozRTCPeerConnection)
    // error, browser doesn't support it
如果您感兴趣,这里有一篇有趣的文章:


您基本上可以在Chrome中实现相同的功能,只需使用
webkit
前缀,而不必使用
moz

。谢谢你的帮助。

现在有一个Chrome
RTCDataChannel
演示

这不是非常健壮或完整,但您可以创建一个
webkirtpeerconnection
对象,然后检查它是否有
createDataChannel
成员:

try { // or if (webkitRTCPeerConnection) {...}
  var pc = new webkitRTCPeerConnection(null);
  if (pc && pc.createDataChannel) {
    var dc = pc.createDataChannel("sendDataChannel", {reliable: false});
    if (!!dc) {
      // doSomething()
    } 
  }
} catch (e) {
  // try some other instantiation 
}

检查webrtcsupport包。它似乎是跨浏览器(Chrome&FF)。

如果您不想使用NPM包,可以在这里找到主要逻辑。
另一个检查WebRTC支持的包是使用JavaScript的DetectRTC

var prefix;
var version;
if (window.mozRTCPeerConnection || navigator.mozGetUserMedia) {
  prefix = 'moz';
  version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
} else if (window.webkitRTCPeerConnection || navigator.webkitGetUserMedia) {
  prefix = 'webkit';
  version = navigator.userAgent.match(/Chrom(e|ium)/) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10);
}

if(prefix == 'moz' || prefix == 'webkit' && version > 41){
  console.log("Browser Support WebRTC")
} else {
  console.log("This Browser Not Support WebRTC")
}

最高的答案是不对的

if (window.RTCDataChannel) {
    // the browser support dataChannel
} else {
   // the browser does not support dataChannel
}

如果浏览器支持RTPEERCONNECTION,则并不意味着也支持RTCDataChannel。例如,
RTPEERCONNECTION
实例上的
createDataChannel
将引发异常。

令人惊讶的是,没有人提到检查原型

通过执行以下操作,可以检查createDataChannel是否存在:

if (!window.RTCPeerConnection.prototype.createDataChannel) {
    // Data Channels not supported
}

我听说它在Chrome中也能工作。你为什么不测试一下呢?在Chrome金丝雀中没有演示来测试它。如果你能给我演示一下,那就太好了。。我被从HTML5 rock webRTC重定向到使用Mozilla。在任何一种情况下,我都需要一个JS来检查兼容性问题,这样我就可以开始开发一个原型。然后使用功能检测,而不是UA字符串匹配!!!我使用了if(!window.RTCDataChannel)document.write(“错误,浏览器不支持它”)。。但即使浏览器支持,也不允许出现错误!!它可能是带前缀的,就像?一样,但它不适用于(!window.RTCDataChannel)文档。write(“错误,浏览器不支持它”)通过RTPeerConnection创建数据通道,请参见:和例如。否!支持RTPeerConnection并不意味着它也支持RTCDataChannel。您应该尝试测试
window.RTCDataChannel
而不是实际上,
detect.js
并没有做您想做的事情:它只是一个浏览器嗅探,在web开发中不再使用。因此,您没有检测到webRTC的支持。这张支票是在view source:中的第53行进行的,它与我之前发布的支票完全相同。我正在浏览你的文章,干得好。但是有没有在两个chrome浏览器中传输的演示。因为siml.info/dc位于单个浏览器中。我不确定他们是否使用了任何信号?我知道:两个浏览器之间还没有演示:(.正在处理:)。应该可以按照apprtc.appspot.com(代码位于)的思路修改siml.info/dc演示。谢谢。我等着看。同时,我将努力按照你的步骤学习。那么,有没有像聊天这样的数据传输的例子呢。它在Moz中工作。。。www.easyrtc.com还有——我还没有设法让它工作,但那可能就是我!网站上还有一个文件传输演示。