Improved client IP detection; Updated examples

This commit is contained in:
dosse91 2017-02-26 12:15:51 +01:00
parent 0e83593adb
commit e5612b4f10
6 changed files with 39 additions and 9 deletions

View file

@ -5,6 +5,8 @@
</head>
<body>
<h1>Speedtest</h1>
<h4>IP Address</h4>
<div id="ip"></div>
<h4>Download</h4>
<div id="download"></div>
<h4>Upload</h4>
@ -19,6 +21,7 @@
document.getElementById("download").innerHTML=data[1]+" Mbit/s";
document.getElementById("upload").innerHTML=data[2]+" Mbit/s";
document.getElementById("ping").innerHTML=data[3]+" ms";
document.getElementById("ip").innerHTML=data[4];
}
w.postMessage("start"); //start the speedtest (default params. keep garbage.php and empty.dat in the same directory as the js file)
</script>

View file

@ -49,13 +49,14 @@
<div class="meter">&nbsp;<span id="ping"></span>&nbsp;</div>
<div class="meterUnit">ms</div>
</div>
<div id="ip"></div>
<script type="text/javascript">
var w=new Worker("speedtest_worker.js");
var interval=setInterval(function(){w.postMessage("status");}.bind(this),100);
w.onmessage=function(event){
var data=event.data.split(";");
var status=Number(data[0]);
var dl=document.getElementById("download"),ul=document.getElementById("upload"),ping=document.getElementById("ping");
var dl=document.getElementById("download"),ul=document.getElementById("upload"),ping=document.getElementById("ping"),ip=document.getElementById("ip");
dl.className=status==1?"flash":"";ul.className=status==2?"flash":"";ping.className=status==3?"flash":"";
if(status>=4){
clearInterval(interval);
@ -63,6 +64,7 @@
dl.innerHTML=data[1];
ul.innerHTML=data[2];
ping.innerHTML=data[3];
ip.innerHTML="Your IP: "+data[4];
}.bind(this);
w.postMessage('start {"url_dl":"garbage.php","url_ul":"empty.dat","url_ping":"empty.dat","time_dl":"10","time_ul":"15","count_ping":"30"}'); //start with custom parameters. paths are relative to js file. you can omit parameters that you don't want to change
</script>

View file

@ -37,6 +37,10 @@
color:#000000;
text-decoration:none;
}
#ip{
margin:0.8em 0;
font-size:1.2em;
}
</style>
<script type="text/javascript">
var w=null;
@ -49,7 +53,7 @@
w.onmessage=function(event){
var data=event.data.split(";");
var status=Number(data[0]);
var dl=document.getElementById("download"),ul=document.getElementById("upload"),ping=document.getElementById("ping");
var dl=document.getElementById("download"),ul=document.getElementById("upload"),ping=document.getElementById("ping"),ip=document.getElementById("ip");
dl.className=status==1?"flash":"";ul.className=status==2?"flash":"";ping.className=status==3?"flash":"";
if(status>=4){
clearInterval(interval);
@ -63,6 +67,7 @@
dl.innerHTML=data[1];
ul.innerHTML=data[2];
ping.innerHTML=data[3];
ip.innerHTML=data[4];
}.bind(this);
w.postMessage("start");
}
@ -74,6 +79,7 @@
<body>
<h1>Speedtest</h1>
<div id="testArea" style="display:none">
<div id="ip">Please wait...</div>
<div class="test">
<div class="testName">Download</div>
<div class="meter">&nbsp;<span id="download"></span>&nbsp;</div>

View file

@ -53,6 +53,11 @@
font-family: 'Roboto';
}
#ip{
margin:1em 0;
font-size:1.2em;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/justgage/1.2.2/justgage.min.js"></script>
@ -76,8 +81,9 @@
updateGauge(ggdl, data[1]);
updateGauge(ggul, data[2]);
updateGauge(ggping, data[3]);
document.getElementById("ip").innerHTML="Your IP: "+data[4];
}.bind(this);
w.postMessage('start {"time_ul":"10", "time_dl":"10", "count_ping":"20", "url_dl":"garbage.php","url_ul":"empty.dat","url_ping":"empty.dat"}');
w.postMessage('start {"time_ul":"10", "time_dl":"10", "count_ping":"20", "url_dl":"garbage.php","url_ul":"empty.dat","url_ping":"empty.dat","url_getIp":"getIP.php"}');
}
function abortTest(){
if(w)w.postMessage("abort");
@ -161,6 +167,7 @@
<div class="meter" id="ggul"></div>
<div class="meter" id="ggping"></div>
</div>
<div id="ip"></div>
<div>
<a href="javascript:runTest()" id="startBtn" class="button">Start</a>
<a href="javascript:abortTest()" id="abortBtn" class="button" style="display:none;">Abort</a>

View file

@ -1,4 +1,3 @@
<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo "getIP({\"ip\":\"$ip\"});";
echo $_SERVER['REMOTE_ADDR'];
?>

View file

@ -1,10 +1,10 @@
var testStatus=0,dlStatus="",ulStatus="",pingStatus="";
var settings={time_ul:15,time_dl:15,count_ping:35,url_dl:"garbage.php",url_ul:"empty.dat",url_ping:"empty.dat"};
var testStatus=0,dlStatus="",ulStatus="",pingStatus="",clientIp="";
var settings={time_ul:15,time_dl:15,count_ping:35,url_dl:"garbage.php",url_ul:"empty.dat",url_ping:"empty.dat",url_getIp:"getIP.php"};
var xhr=null;
this.addEventListener('message', function(e){
var params=e.data.split(" ");
if(params[0]=="status"){
postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus);
postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus+";"+clientIp);
}
if(params[0]=="start"&&testStatus==0){
testStatus=1;
@ -13,17 +13,30 @@ this.addEventListener('message', function(e){
if(typeof s.url_dl != "undefined") settings.url_dl=s.url_dl;
if(typeof s.url_ul != "undefined") settings.url_ul=s.url_ul;
if(typeof s.url_ping != "undefined") settings.url_ping=s.url_ping;
if(typeof s.url_getIp != "undefined") settings.url_getIp=s.url_getIp;
if(typeof s.time_dl != "undefined") settings.time_dl=s.time_dl;
if(typeof s.time_ul != "undefined") settings.time_ul=s.time_ul;
if(typeof s.count_ping != "undefined") settings.count_ping=s.count_ping;
}catch(e){}
dlTest(function(){testStatus=2;ulTest(function(){testStatus=3;pingTest(function(){testStatus=4;});});});
getIp(function(){dlTest(function(){testStatus=2;ulTest(function(){testStatus=3;pingTest(function(){testStatus=4;});});})});
}
if(params[0]=="abort"){
try{if(xhr)xhr.abort();}catch(e){}
testStatus=5;dlStatus="";ulStatus="";pingStatus="";
}
});
function getIp(done){
xhr=new XMLHttpRequest();
xhr.onload=function(){
clientIp=xhr.responseText;
done();
}
xhr.onerror=function(){
done();
}
xhr.open("GET",settings.url_getIp+"?r="+Math.random(),true);
xhr.send();
}
var dlCalled=false;
function dlTest(done){
if(dlCalled) return; else dlCalled=true;