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,6 +235,30 @@ 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;
var ie11workaround;
try{
xhr[i].upload.onprogress;
ie11workaround=false;
}catch(e){
ie11workaround=true;
}
if(ie11workaround){
//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
xhr[i].onload=function(){
totLoaded+=262144;
testStream(i,0);
}
xhr[i].onerror=function(){
//error, abort
failed=true;
try{xhr[i].abort();}catch(e){}
delete(xhr[i]);
}
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(reqsmall);
}else{
//REGULAR version, no workaround
xhr[i].upload.onprogress=function(event){ 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 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 //progress event, add number of new loaded bytes to totLoaded
@ -240,7 +268,7 @@ function ulTest(done){
prevLoaded=event.loaded; prevLoaded=event.loaded;
}.bind(this); }.bind(this);
xhr[i].upload.onload=function(){ xhr[i].upload.onload=function(){
//this stream sent all 20mb of garbage data, start again //this stream sent all the garbage data, start again
testStream(i,0); testStream(i,0);
}.bind(this); }.bind(this);
xhr[i].upload.onerror=function(){ xhr[i].upload.onerror=function(){
@ -253,6 +281,7 @@ function ulTest(done){
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(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