这里我使用webrc结合stun,通过js验证测试:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script>
// turn 配置
const config = {
iceServers: [{
// urls: "stun:stun.l.google.com:19302" // 这里使用谷歌,线上部署直接替换
}]
};
// 构建
let pc = new RTCPeerConnection(config);
pc.onicecandidate = function(event) {
if(event.candidate)
handleCandidate(event.candidate.candidate);
}
function handleCandidate(candidate) {
if (candidate.indexOf("srflx") != -1) {
console.log(candidate)
var regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
var ip_addr = regex.exec(candidate)[0];
alert("Your public network ip: "+ ip_addr)
}
}
pc.createDataChannel("");
pc.createOffer(function(result){
pc.setLocalDescription(result);
}, function(){});
</script&