通过WebRTC捕获IP的JavaScript(异步/等待问题)
我提出了通过WebRTC捕获IP的代码。 但我遇到了这个问题,我不能将IPs作为变量返回,我只能在控制台中打印IPs,在警报中显示它们,或者在HTML页面上显示它们。但我需要从函数返回IPs作为变量 我认为问题在于异步/等待。我不是JavaScript开发人员,要花上一整天的时间才能找到放置通过WebRTC捕获IP的JavaScript(异步/等待问题),javascript,security,webrtc,Javascript,Security,Webrtc,我提出了通过WebRTC捕获IP的代码。 但我遇到了这个问题,我不能将IPs作为变量返回,我只能在控制台中打印IPs,在警报中显示它们,或者在HTML页面上显示它们。但我需要从函数返回IPs作为变量 我认为问题在于异步/等待。我不是JavaScript开发人员,要花上一整天的时间才能找到放置wait的地方 请帮我解决这个问题。 多谢各位 function findIPsWithWebRTC() { var myPeerConnection = window.RTCPeerConnecti
wait
的地方
请帮我解决这个问题。
多谢各位
function findIPsWithWebRTC() {
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({iceServers: [{urls: "stun:stun.l.google.com:19302"}]}),
noop = function() {},
IPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key;
function ipIterate(ip) {
if (!IPs[ip]) console.log('got ip: ', ip);
IPs[ip] = true;
}
pc.createDataChannel("");
pc.createOffer(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(ipIterate);
});
pc.setLocalDescription(sdp, noop, noop);
}, noop);
pc.onicecandidate = function(ice) {
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
};
console.log("ips: " + JSON.stringify(IPs));
return {
"source": "WebRTC",
"name": "IPs",
"value": JSON.stringify(IPs)
}
}
alert(findIPsWithWebRTC().value);
函数findipswithwebtc(){
var myPeerConnection=window.rtpeerconnection | | window.mozrtpeerconnection | | window.webkirtpeerconnection;
var pc=newmypeerconnection({iceServers:[{url:stun:stun.l.google.com:19302“}]}),
noop=函数(){},
IPs={},
ipRegex=/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
钥匙
函数ipIterate(ip){
if(!IPs[ip])console.log('got ip:',ip);
IPs[ip]=真;
}
pc.createDataChannel(“”);
pc.createOffer(功能(sdp){
sdp.sdp.split('\n').forEach(函数(行){
if(line.indexOf('candidate')<0)返回;
line.match(ipRegex).forEach(ipIterate);
});
pc.setLocalDescription(sdp、noop、noop);
},noop);
pc.onicecandidate=函数(ice){
如果(!ice | |!ice.candidate | |!ice.candidate.candidate | |!ice.candidate.candidate.match(ipRegex))返回;
ice.candidate.candidate.match(ipRegex.forEach)(ipIterate);
};
log(“ips:+JSON.stringify(ips));
返回{
“来源”:“WebRTC”,
“名称”:“IPs”,
“值”:JSON.stringify(IPs)
}
}
警报(findipswithwebtc().value);
我可以在控制台中输出IPs,但不能将IPs作为函数的值返回
findipswithwebtc
无法返回IP,因为IP是异步提供给代码的。同步函数不能返回异步进程的结果
相反,您的findipswithwebtc
应该返回一个承诺,该承诺将通过您当前尝试返回的对象来实现。看起来您在onicecandidate
(或者可能是对createOffer
?)的回调中获得了IP,因此(请参见***
):
…或在非异步
函数中,使用然后
和捕获
:
findIPsWithWebRTC()
.then(ipInfo => {
// ...use `ipInfo`...
})
.catch(error => {
// Handle/report error
});
在这两种情况下,如果使用结果的函数不是顶级使用者,则通常将错误处理留给它。在
async
示例中,您可以通过不使用try
/catch
(拒绝会自动传播到调用方)来实现。在非异步
示例中,您可以通过返回调用然后
的结果,并且没有捕获
findipswithwebtc
无法返回IP,因为IP是异步提供给代码的。同步函数不能返回异步进程的结果
相反,您的findipswithwebtc
应该返回一个承诺,该承诺将通过您当前尝试返回的对象来实现。看起来您在onicecandidate
(或者可能是对createOffer
?)的回调中获得了IP,因此(请参见***
):
…或在非异步
函数中,使用然后
和捕获
:
findIPsWithWebRTC()
.then(ipInfo => {
// ...use `ipInfo`...
})
.catch(error => {
// Handle/report error
});
在这两种情况下,如果使用结果的函数不是顶级使用者,则通常将错误处理留给它。在async
示例中,您可以通过不使用try
/catch
(拒绝会自动传播到调用方)来实现。在非异步
示例中,您可以通过返回调用的结果,然后
并且没有catch
来执行此操作.createOffer()
调用是异步的。它立即返回;您传递的回调函数稍后会在建立连接时调用。.createOffer()
调用是异步的。它立即返回;稍后在建立连接时调用您传递的回调函数。
findIPsWithWebRTC()
.then(ipInfo => {
// ...use `ipInfo`...
})
.catch(error => {
// Handle/report error
});