1.问题回放

  使用如下代码获取局域网IP报错

  (代码来源:https://github.com/diafygi/webrtc-ips 日期:2019-02-16)

Uncaught (in promise) DOMException: Failed to execute 'setLocalDescription' on 'RTCPeerConnection': Failed to parse SessionDescription. a=msid:  Missing track ID in msid attribute.

 //get the IP addresses associated with an account
function getIPs(callback){
var ip_dups = {}; //compatibility for firefox and chrome
var RTCPeerConnection = window.RTCPeerConnection
|| window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection;
var useWebKit = !!window.webkitRTCPeerConnection; //bypass naive webrtc blocking using an iframe
if(!RTCPeerConnection){
//NOTE: you need to have an iframe in the page right above the script tag
//
//<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
//<script>...getIPs called in here...
//
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
} //minimal requirements for data connection
var mediaConstraints = {
optional: [{RtpDataChannels: true}]
}; var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]}; //construct a new RTCPeerConnection
var pc = new RTCPeerConnection(servers, mediaConstraints); function handleCandidate(candidate){
//match just the IP address
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
var ip_addr = ip_regex.exec(candidate)[1]; //remove duplicates
if(ip_dups[ip_addr] === undefined)
callback(ip_addr); ip_dups[ip_addr] = true;
} //listen for candidate events
pc.onicecandidate = function(ice){ //skip non-candidate events
if(ice.candidate)
handleCandidate(ice.candidate.candidate);
}; //create a bogus data channel
pc.createDataChannel(""); //create an offer sdp
pc.createOffer(function(result){ //trigger the stun server request
pc.setLocalDescription(result, function(){}, function(){}); }, function(){}); //wait for a while to let everything done
setTimeout(function(){
//read candidate info from local description
var lines = pc.localDescription.sdp.split('\n'); lines.forEach(function(line){
if(line.indexOf('a=candidate:') === 0)
handleCandidate(line);
});
}, 1000);
} //Test: Print the IP addresses into the console
getIPs(function(ip){console.log(ip);});

2.分析

  代码在此处报错:pc.setLocalDescription(result, function(){}, function(){})

  即此API报错:RTCPeerConnection.setLocalDescription()

  源代码在Firefox和Chrome71正常执行,Chrome升级到72稳定版后代码报错,浏览器debug调试发现Chrome71和Chrome72的offer(代码中是result)的SDP格式/内容发生了变化,因此可以判断是新版本Chrome的WebRTC相关API发生了变化,于是在https://www.chromestatus.com/features找到了解释:

 

  文中提供了此链接:https://docs.google.com/document/d/1-ZfikoUtoJa9k-GZG1daN0BU3IjIanQ_JSscHxQesvU/edit,划重点:

    WebRTC规范仍然在演进,Chrome上目前SDP格式有两种:规范定义的SDP格式(Unified Plan)和Chrome自己定义的SDP格式(Plan B),火狐支持前者,如果不指定SDP格式Chrome72默认使用Unified Plan,这和Chrome71的行为不同,那么如何指定SDP格式呢?

  

Controlling Which SDP Format Is Used

Specifying the sdpSemantics in Your Application

You can override the default behavior by explicitly specifying the sdpSemantics in the RTCPeerConnection constructor, allowing your application to control the SDP format of the client. This flag is Chrome-only; if the browser does not recognize the parameter it is simply ignored.

// Use Unified Plan or Plan B regardless of what the default browser behavior is.

new RTCPeerConnection({sdpSemantics:'unified-plan'});

new RTCPeerConnection({sdpSemantics:'plan-b'});

Unless your application is prepared for both cases, it is recommended that you explicitly set the sdpSemantics to avoid surprises when Chrome’s default behavior changes.

3.解决

  将源代码此行

    var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};

  更改为   

    var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}], sdpSemantics:'plan-b'};

4.其他

  如果Public IP无法解析,请更换stun服务器:

    var servers = {iceServers: [{urls: "stun:stun.l.google.com:19302"}], sdpSemantics:'plan-b'}; 

最新文章

  1. Android重构与设计之路,从整理提示弹窗(SmartAlertPop)开始
  2. JS 原型链
  3. VMware卸载出现“the msi failed”解决办法
  4. CSS 3中边框怎么用
  5. ASP.NET MVC 过滤器详解
  6. 用LR12录制app,用LR11跑场景,无并发数限制,已试验过,可行!
  7. ioshittest的用法
  8. 模拟实现死亡之Ping(Ping of death)
  9. Text Kit入门
  10. Django学习随想(1)
  11. servlet跳转jsp
  12. 我理解的javascript单线程机制
  13. ssm框架中css被拦截
  14. FineReport填报分页设置
  15. ABP+AdminLTE+Bootstrap Table权限管理系统第十一节--bootstrap table之用户管理列表
  16. Python的列表
  17. [原创]消灭eclipse中运行启动的错误:“找不到或无法加载主类”问题
  18. springMVC源码分析--ControllerClassNameHandlerMapping(九)
  19. 观察者模式与.Net Framework中的委托与事件
  20. div “下沉”

热门文章

  1. K3内部表数据名称
  2. ES8(2017)新特性学习
  3. ansible yum 模块 安装 vsftp
  4. Jquery JS 全局变量
  5. IDEA Maven Web项目 clone到本地导入到Eclipse中,启动服务器的时候会出现这个错误:SEVERE: Exception starting filter [hiddenHttpMethodFilter]
  6. 在Windows系统下搭建Redis集群
  7. powerDesigner如何动态显示mysql数据库表结构
  8. Oracle 学习笔记 13 -- 控制用户权限
  9. Transformation in kentico
  10. Oracle11g数据库导入Oracle10g数据库操作笔记