| 1 |
2 |
- |
;(function($) {
|
| 2 |
|
|
$.NetworkConnection = function (settings) {
|
| 3 |
|
|
$.NetworkConnection = new NetworkConnection(settings);
|
| 4 |
|
|
}
|
| 5 |
|
|
|
| 6 |
|
|
function NetworkConnection(settings) {
|
| 7 |
|
|
this.pingURL = "hello.txt";
|
| 8 |
|
|
this.pingTimeout = 2000;
|
| 9 |
|
|
this.pingInterval = this.pingTimeout + 100;
|
| 10 |
|
|
this.connectionStatus = null;
|
| 11 |
|
|
this.timeout = null;
|
| 12 |
|
|
this.lastTextStatus = null;
|
| 13 |
|
|
this.lastErrorThrown = null;
|
| 14 |
|
|
this.testCount = 0;
|
| 15 |
|
|
|
| 16 |
|
|
$.extend(this, settings);
|
| 17 |
|
|
|
| 18 |
|
|
this.isConnected = function () {
|
| 19 |
|
|
var self = this;
|
| 20 |
|
|
return (self.connectionStatus == 'connected') ? true : false;
|
| 21 |
|
|
}
|
| 22 |
|
|
|
| 23 |
|
|
this.run = function () {
|
| 24 |
|
|
var self = this;
|
| 25 |
|
|
|
| 26 |
|
|
self.doTest();
|
| 27 |
|
|
|
| 28 |
|
|
self.timeout = setInterval(function () {
|
| 29 |
|
|
self.doTest();
|
| 30 |
|
|
}, self.pingInterval)
|
| 31 |
|
|
}
|
| 32 |
|
|
|
| 33 |
|
|
this.doTest = function () {
|
| 34 |
|
|
var self = this;
|
| 35 |
|
|
var objTest = {
|
| 36 |
|
|
testNumber : self.testCount++,
|
| 37 |
|
|
startTime : (new Date()).getTime()
|
| 38 |
|
|
};
|
| 39 |
|
|
|
| 40 |
|
|
self.beforetest && self.beforetest(objTest);
|
| 41 |
|
|
|
| 42 |
|
|
$.ajax({
|
| 43 |
|
|
url : self.pingURL,
|
| 44 |
|
|
type : 'GET',
|
| 45 |
|
|
timeout : self.pingTimeout,
|
| 46 |
|
|
dataType : 'text',
|
| 47 |
|
|
cache : false,
|
| 48 |
|
|
success : function (data, textStatus) {
|
| 49 |
|
|
objTest.endTime = (new Date()).getTime();
|
| 50 |
|
|
objTest.rtt = objTest.endTime - objTest.startTime;
|
| 51 |
|
|
objTest.textStatus = textStatus;
|
| 52 |
|
|
|
| 53 |
|
|
self.lastTextStatus = textStatus
|
| 54 |
|
|
self.lastErrorThrown = null;
|
| 55 |
|
|
|
| 56 |
|
|
self.connectionStatus != 'connected' && self.gainedConnection && self.gainedConnection();
|
| 57 |
|
|
self.connectionStatus = objTest.connectionStatus = 'connected';
|
| 58 |
|
|
|
| 59 |
|
|
self.success && self.success(objTest);
|
| 60 |
|
|
self.complete && self.complete(objTest);
|
| 61 |
|
|
},
|
| 62 |
|
|
error : function (XMLHttpRequest, textStatus, errorThrown) {
|
| 63 |
|
|
objTest.endTime = (new Date()).getTime();
|
| 64 |
|
|
objTest.rtt = objTest.endTime - objTest.startTime;
|
| 65 |
|
|
objTest.textStatus = textStatus;
|
| 66 |
|
|
objTest.errorThrown = errorThrown;
|
| 67 |
|
|
|
| 68 |
|
|
self.lastTextStatus = textStatus
|
| 69 |
|
|
self.lastErrorThrown = errorThrown;
|
| 70 |
|
|
|
| 71 |
|
|
self.connectionStatus != 'disconnected' && self.lostConnection && self.lostConnection();
|
| 72 |
|
|
self.connectionStatus = objTest.connectionStatus = 'disconnected';
|
| 73 |
|
|
self.fail && self.fail(objTest);
|
| 74 |
|
|
self.complete && self.complete(objTest);
|
| 75 |
|
|
}
|
| 76 |
|
|
})
|
| 77 |
|
|
}
|
| 78 |
|
|
|
| 79 |
|
|
this.run();
|
| 80 |
|
|
}
|
| 81 |
|
|
})(jQuery);
|