最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How can I check webRTC datachannel compatibility using JavaScript in client side? - Stack Overflow

programmeradmin3浏览0评论

WebRTC datachannel works only in Firefox nightly. How can I check it in client side?

Code is shown as follows;

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")

WebRTC datachannel works only in Firefox nightly. How can I check it in client side?

Code is shown as follows;

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")
Share Improve this question edited Feb 8, 2013 at 14:17 sam46 372 silver badges7 bronze badges asked Feb 8, 2013 at 14:09 usercodeusercode 3091 gold badge4 silver badges20 bronze badges 6
  • I have heard it works in Chrome as well. Why don't you test for that? – Bergi Commented Feb 8, 2013 at 14:11
  • Well there is no Demo to test it in Chrome Canary. Would be nice though if you provide me with the demo.. I was redirected from the HTML5 rock webRTC to use Mozilla. In either case, I need a JS to check the compatibility issues, so that I can start developing a prototype. – usercode Commented Feb 8, 2013 at 14:16
  • Then use feature detection, not UA String matching!!! – Bergi Commented Feb 8, 2013 at 14:23
  • I used if (!window.RTCDataChannel) document.write("error, browser doesn't support it") .. But its not working allowing a error even though browser supports it !! – usercode Commented Feb 8, 2013 at 14:27
  • Maybe it's prefixed, just as the RTCPeerConnection? – Bergi Commented Feb 8, 2013 at 14:40
 |  Show 1 more comment

8 Answers 8

Reset to default 7

You can simply test if the browser currently supports the features you're going to use. For example:

if (!window.mozRTCPeerConnection)
    // error, browser doesn't support it

If you're interested, here an interesting article: Hello Chrome, it’s Firefox calling!

You basically can implement the same feature in Chrome just using webkit prefix instead of moz.

There's a Chrome RTCDataChannel demo now at simpl.info/dc.

This isn't very robust or complete, but you could create a webkitRTCPeerConnection object and then check if it has a createDataChannel member:

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 
}

Check the webrtcsupport package. It seems to be cross-browser (Chrome & FF). https://www.npmjs.org/package/webrtcsupport

If you don't wanna use the NPM package, the main logic can be found here. https://github.com/HenrikJoreteg/webrtcsupport/blob/master/index-browser.js

Using JavaScript

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")
}

The top answer is not right.

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

If browser support RTCPeerConnection, it not means also support for RTCDataChannel。Such as Edge, createDataChannel on instance of RTCPeerConnection will throw an exception.

Surprised no one mentions checking the prototype.

You can check if createDataChannel exists by doing this:

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

I just figured out. Thanks for all your help. http://mozilla.github.com/webrtc-landing/

Another package that checks for WebRTC support is DetectRTC https://github.com/muaz-khan/DetectRTC

发布评论

评论列表(0)

  1. 暂无评论