Skip to content

Commit

Permalink
Added experimental workarounds for IE11; Added new example
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfintel committed Apr 26, 2017
1 parent eb34964 commit 30c595d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This is a very lightweight Speedtest implemented in Javascript, using XMLHttpReq
[Take a Speedtest](http://speedtest.fdossena.com)

## Compatibility
Only modern browsers are supported (Edge 12+)
Only modern browsers are supported (IE11, latest Edge, latest Chrome, latest Firefox, latest Safari)

## Requirements
- A reasonably fast web server. PHP is optional but recommended (see doc.pdf for details)
Expand Down
Binary file modified doc.pdf
Binary file not shown.
77 changes: 53 additions & 24 deletions speedtest_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,16 @@ function dlTest(done){
}.bind(this),200);
}
//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);
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);
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
function ulTest(done){
if(ulCalled) return; else ulCalled=true; //ulTest already called?
Expand All @@ -231,28 +235,53 @@ function ulTest(done){
var prevLoaded=0; //number of bytes transmitted last time onprogress was called
var x=new XMLHttpRequest();
xhr[i]=x;
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 20mb of 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);
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){
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);
//open streams
Expand Down
2 changes: 1 addition & 1 deletion speedtest_worker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 30c595d

Please sign in to comment.