Added experimental workarounds for IE11; Added new example

This commit is contained in:
dosse91 2017-04-26 07:23:36 +02:00
parent eb349648bd
commit 30c595de6e
4 changed files with 55 additions and 26 deletions

View file

@ -8,7 +8,7 @@ This is a very lightweight Speedtest implemented in Javascript, using XMLHttpReq
[Take a Speedtest](http://speedtest.fdossena.com) [Take a Speedtest](http://speedtest.fdossena.com)
## Compatibility ## Compatibility
Only modern browsers are supported (Edge 12+) Only modern browsers are supported (IE11, latest Edge, latest Chrome, latest Firefox, latest Safari)
## Requirements ## Requirements
- A reasonably fast web server. PHP is optional but recommended (see doc.pdf for details) - A reasonably fast web server. PHP is optional but recommended (see doc.pdf for details)

BIN
doc.pdf

Binary file not shown.

View file

@ -211,12 +211,16 @@ function dlTest(done){
}.bind(this),200); }.bind(this),200);
} }
//upload test, calls done function whent it's over //upload test, calls done function whent it's over
//garbage data for upload test (1mb of random bytes repeated 20 times, for a total of 20mb) //garbage data for upload test
var r=new ArrayBuffer(1048576); var r=new ArrayBuffer(1048576);
try{r=new Float32Array(r);for(var i=0;i<r.length;i++)r[i]=Math.random();}catch(e){} try{r=new Float32Array(r);for(var i=0;i<r.length;i++)r[i]=Math.random();}catch(e){}
var req=[]; var req=[],reqsmall=[];
for(var i=0;i<20;i++) req.push(r); for(var i=0;i<20;i++) req.push(r);
req=new Blob(req); req=new Blob(req);
r=new ArrayBuffer(262144);
try{r=new Float32Array(r);for(var i=0;i<r.length;i++)r[i]=Math.random();}catch(e){}
reqsmall.push(r);
reqsmall=new Blob(reqsmall);
var ulCalled=false; //used to prevent multiple accidental calls to ulTest var ulCalled=false; //used to prevent multiple accidental calls to ulTest
function ulTest(done){ function ulTest(done){
if(ulCalled) return; else ulCalled=true; //ulTest already called? if(ulCalled) return; else ulCalled=true; //ulTest already called?
@ -231,28 +235,53 @@ function ulTest(done){
var prevLoaded=0; //number of bytes transmitted last time onprogress was called var prevLoaded=0; //number of bytes transmitted last time onprogress was called
var x=new XMLHttpRequest(); var x=new XMLHttpRequest();
xhr[i]=x; xhr[i]=x;
xhr[i].upload.onprogress=function(event){ var ie11workaround;
if(testStatus!=3){try{x.abort();}catch(e){}} //just in case this XHR is still running after the upload test try{
//progress event, add number of new loaded bytes to totLoaded xhr[i].upload.onprogress;
var loadDiff=event.loaded<=0?0:(event.loaded-prevLoaded); ie11workaround=false;
if(isNaN(loadDiff)||!isFinite(loadDiff)||loadDiff<0) return; //just in case }catch(e){
totLoaded+=loadDiff; ie11workaround=true;
prevLoaded=event.loaded; }
}.bind(this); if(ie11workaround){
xhr[i].upload.onload=function(){ //IE11 workarond: xhr.upload does not work properly, therefore we send a bunch of small 256k requests and use the onload event as progress. This is not precise, especially on fast connections
//this stream sent all 20mb of garbage data, start again xhr[i].onload=function(){
testStream(i,0); totLoaded+=262144;
}.bind(this); testStream(i,0);
xhr[i].upload.onerror=function(){ }
//error, abort xhr[i].onerror=function(){
failed=true; //error, abort
try{xhr[i].abort();}catch(e){} failed=true;
delete(xhr[i]); try{xhr[i].abort();}catch(e){}
}.bind(this); delete(xhr[i]);
//send xhr }
xhr[i].open("POST",settings.url_ul+"?r="+Math.random(),true); //random string to prevent caching xhr[i].open("POST",settings.url_ul+"?r="+Math.random(),true); //random string to prevent caching
xhr[i].setRequestHeader('Content-Encoding','identity'); //disable compression (some browsers may refuse it, but data is incompressible anyway) xhr[i].setRequestHeader('Content-Encoding','identity'); //disable compression (some browsers may refuse it, but data is incompressible anyway)
xhr[i].send(req); xhr[i].send(reqsmall);
}else{
//REGULAR version, no workaround
xhr[i].upload.onprogress=function(event){
if(testStatus!=3){try{x.abort();}catch(e){}} //just in case this XHR is still running after the upload test
//progress event, add number of new loaded bytes to totLoaded
var loadDiff=event.loaded<=0?0:(event.loaded-prevLoaded);
if(isNaN(loadDiff)||!isFinite(loadDiff)||loadDiff<0) return; //just in case
totLoaded+=loadDiff;
prevLoaded=event.loaded;
}.bind(this);
xhr[i].upload.onload=function(){
//this stream sent all the garbage data, start again
testStream(i,0);
}.bind(this);
xhr[i].upload.onerror=function(){
//error, abort
failed=true;
try{xhr[i].abort();}catch(e){}
delete(xhr[i]);
}.bind(this);
//send xhr
xhr[i].open("POST",settings.url_ul+"?r="+Math.random(),true); //random string to prevent caching
xhr[i].setRequestHeader('Content-Encoding','identity'); //disable compression (some browsers may refuse it, but data is incompressible anyway)
xhr[i].send(req);
}
}.bind(this),1); }.bind(this),1);
}.bind(this); }.bind(this);
//open streams //open streams

File diff suppressed because one or more lines are too long