我们如何使用Javascript代码查找IP地址?

问题描述 投票:0回答:1

const PeerConnection = new RTCPeerConnection({iceServers: [] });

 peerConnection.createDataChannel('');

 peerConnection.createOffer()
.then((offer) => peerConnection.setLocalDescription(offer))
.catch((error) => console.log(error));

peerConnection.onicecandidate = (event) => {
if (event.candidate) {
  console.log(`Client's IP address is ${event.candidate.address}`);
 }

};`

javascript angular frontend operating-system ip-address
1个回答
0
投票

从候选属性中提取IP地址

const ipAddress = event.candidate.candidate.split(' ')[4];

打印IP地址

console.log('IP address:', ipAddress);
© www.soinside.com 2019 - 2024. All rights reserved.