Fixed RTCConfiguration error only in Chrome

I use WebRTC and understand that it is not supported in all browsers. However, Chrome and Firefox support it (in new versions, I have the latest versions installed) if you have the correct prefix for certain variables. For example, I have the following for PeerConnection for cross-browser support:

var PeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; 

Now that it should be supported by a cross browser, I have the following code:

 var servers = { iceservers: [ {url: "stun:23.21.150.121"}, {url: "stun:stun.1.google.com:19302"} ] }; var pc = PeerConnection(servers); 

But in Chrome, it gets an error on the last line ( var pc = PeerConnection(servers); ). Mistake:

 Failed to construct 'RTCPeerConnection': Malformed RTCConfiguration"} 

Obviously, Chrome doesn't like my configuration option in the PeerConnection declaration. But my question is: why am I getting this error and how does it only happen in Chrome? (FireFox works great)

+7
javascript google-chrome webrtc
source share
2 answers

Well, the decision is actually quiet. The servers object must be created using iceServers in a camel case. Also, you forgot your new keyword when creating the connection, but this is probably a typo in the question.

Like this:

 var servers = { iceServers: [ {url: "stun:23.21.150.121"}, {url: "stun:stun.1.google.com:19302"} ] }; var pc = new PeerConnection(servers); 

And the whole thing about lower case and camel case works great in FireFox. Thus, its change should not change how it works, but for Chrome it should be a camel case.

+6
source share

See Ben's answer. JavaScript is case sensitive, and "iceServers" is the correct spelling from the mediacapture specification .

I wanted to clarify that all subordinate iceservers do not actually work in Firefox, since your STUN servers are ignored. Firefox uses its default STUN server when it does not see it, so it works, but does not use the STUN servers that you think.

+2
source share

All Articles