WebRTC Problem!

there was a small bug inside my code & thx a lot @RedSQL for helping me out :)
This commit is contained in:
Esmail EL BoB
2019-07-26 18:56:10 +02:00
parent e7c6c500ce
commit 06a455b029
2 changed files with 26 additions and 14 deletions

View File

@@ -14,6 +14,8 @@
<body>
<noscript>
<h2>hmm, i guess you disabled JS so please enable it to see the magic! :)</h2>
<p>ــــــــــــــــــــــ</p>
<p>ــــــــــــــــــــــ</p>
</noscript>
<div id="IP">
<h2>Your Public IP Is: </h2>
@@ -55,7 +57,7 @@
</div>
<p>ــــــــــــــــــــــ</p>
<div id="SPEEDTEST">
<h2>Your Net's Speed Is: <br> <a id="speedtest"></a></h2>
<h2>Your Net Speed Is: <br> <a id="speedtest"></a></h2>
</div>
<script src="main.js"></script>
</body>

36
main.js
View File

@@ -1,18 +1,28 @@
// Find my ip code
var RTCPeerConnection = window.RTCPeerConnection || webkitRTCPeerConnection || mozRTCPeerConnection;
var peerConn = new RTCPeerConnection({
'iceServers': [{
'urls': ['stun:stun.l.google.com:19302']
}]
// Since when webrtc is disabled, webkitRTCPeerConnection is nonexistent, therefore we try and catch and define RTCPeerConnection with no value because webrtc is disabled.
try {
var RTCPeerConnection = window.RTCPeerConnection || webkitRTCPeerConnection || mozRTCPeerConnection;
} catch {
var RTCPeerConnection;
}
if (RTCPeerConnection) {
var peerConn = new RTCPeerConnection({
'iceServers': [{
'urls': ['stun:stun.l.google.com:19302']
}]
});
var dataChannel = peerConn.createDataChannel('test'); // Needs something added for some reason
peerConn.createOffer({}).then((desc) => peerConn.setLocalDescription(desc));
peerConn.onicecandidate = (e) => {
if (e.candidate == null) {
document.getElementById("ip").innerText = /c=IN IP4 ([^\n]*)\n/.exec(peerConn.localDescription.sdp)[1];
}
};
});
var dataChannel = peerConn.createDataChannel('test'); // Needs something added for some reason
peerConn.createOffer({}).then((desc) => peerConn.setLocalDescription(desc));
peerConn.onicecandidate = (e) => {
if (e.candidate == null) {
document.getElementById("ip").innerText = /c=IN IP4 ([^\n]*)\n/.exec(peerConn.localDescription.sdp)[1];
}
};
} else {
// Inform user that webrtc fetch failed
document.getElementById("ip").innerText = 'Failed to fetch IP via WebRTC, perhaps your WebRTC is disabled?';
}
// ـــــــــــــــــــــــــــــــــــ
// Location