PDA

View Full Version : Ext.Bandwidth



fpiguy
26 Jun 2009, 8:35 AM
I don't know if this would be useful to anyone, but I thought I would post it anyway. I didn't see this extension when I was searching for it here, so I fashioned one.

This class detects your internet speed (a rough estimate) by an ajax call to a file on your server and then stores the information (Kbps, description, isHighSpeed bool). In my case, I'm using a 500x500 blank jpg image. It's attached.

It's a Singleton Pattern modeled after Ext.History. It does rely on Ext.Ajax to make the call.

To initiate the class use: Ext.Bandwidth.init();

Variables:

(int) speed
(int) type
(bool) isHighSpeed


Methods:

init( [Boolean onReady], [Object scope] )

returns void


getType()

returns [String type]


getSpeed()

returns [Integer speed]


fullDetails()

returns [Object {speed, type, isHighSpeed}]


debug()

returns js alert box with all data shown


Events

ready


Here's the js class:




Ext.Bandwidth = (function(){
var d = new Date();
var filepath = 'images/BandwidthTest.jpg';
var speed = 0, isHighSpeed = false, type = false, ready = false;

function interpretResults(kb){
speed = kb*100;
if(kb == 0){
type = "No Connection";
}
else if(kb > 0 && kb < 3){
type = "28k Modem";
}
else if (kb >3 && kb < 6){
type = "56k Modem";
}
else if(kb > 6 && kb < 100){
isHighSpeed = true;
type = "High Bandwidth";
}
else if(kb >= 100){
isHighSpeed = true;
type = "LAN Connection";
}

ready = true;
Ext.Bandwidth.fireEvent('ready', Ext.Bandwidth);
}

function detectSpeed(){
Ext.Ajax.request({
url: filepath,
params: {
start: d.getTime()
},
method: 'GET',
callback: function(options, success, response){
var d2 = new Date();
var time = Math.round((d2.getTime()-options.params.start)/10/100);
var connSpeed = Math.round(parseInt(response.getResponseHeader['Content-Length'])/time/1000);
interpretResults(connSpeed);
},
scope: this
});
}

return {
init: function(onReady, scope){
if(ready){
Ext.callback(onReady, scope, [this]);
return;
}
if(!Ext.isReady){
Ext.onReady(function(){
Ext.Bandwidth.init(onReady, scope);
});
return;
}

this.addEvents('ready');

if(onReady){
this.on('ready', onReady, scope);
}

detectSpeed();

},
IsHighSpeed: function(){
return isHighSpeed;
},

getSpeed: function(){
return speed;
},

getType: function(){
return type;
},

fullDetails: function(){
return {
speed: this.getSpeed(),
isHighSpeed: this.IsHighSpeed(),
type: this.getType()
};
},
debug: function(){
alert("Kbps: "+this.getSpeed()+" \n type: "+ this.getType()+"\n isHighSpeed: "+ this.IsHighSpeed());
}
};
})();
Ext.apply(Ext.Bandwidth, new Ext.util.Observable());


The only problem is that I don't have a low bandwidth connection, so if any of you out there do and happen to test it, let me know what the results where.

moegal
7 Jul 2009, 8:47 AM
This is really nice. I have not had a chance to test it yet thought. Nice work!

Marty

fpiguy
3 Aug 2009, 7:39 AM
Thanks!