-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_internet_connection.js
80 lines (69 loc) · 3.07 KB
/
check_internet_connection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Code to detect if internet connection exists
* Feel free to pick up the stuff you need, use it, modify it and redistribute
*/
YourApp = function(container) {
// Get your constructor stuff here
};
YourApp.isServerReachable = false;
YourApp.TIMERS = {};
YourApp.POLL_INTERVAL = 30000;
YourApp.YOUR_EVENTS = {};
YourApp.prototype._addListeners = function() {
if (!YourApp.TIMERS.checkServerStatusTimer) {
// ----------------------------------------------------------------------
// Poll your service every 30 seconds by default else it is configurable
$(window).on("online offline", YourApp.checkServerStatus);
// ----------------------------------------------------------------------
YourApp.TIMERS.checkServerStatusTimer = setInterval(YourApp.checkServerStatus, YourApp.POLL_INTERVAL);
// Set the counter while attaching the listener, it produces a NaN if not set to 0.
YourApp.TIMERS.ConnectTriggerCount = 0;
}
YourApp.YOUR_EVENTS.bind("FooConnect", this._reconnectFoo, this);
YourApp.YOUR_EVENTS.bind("FooDisconnect", this._disconnectFoo, this);
};
YourApp.checkServerStatus = function(doStop, doNotTrigger, callback) {
$.ajax({
type: "HEAD",
url: "/public/blank.html",
cache: false,
statusCode: {
0: function() {
if (YourApp.isServerReachable === true) {
if (doStop) {
// Reset the state here - state must be correct for functions triggered by the calee function
YourApp.isServerReachable = false;
if (!doNotTrigger) {
// Reset the trigger counter when disconnected and let retry or auto-reconnect drive the trigger count.
YourApp.TIMERS.ConnectTriggerCount = 0;
YourApp.YOUR_EVENTS.trigger("FooDisconnect");
}
}
else {
return YourApp.checkServerStatus(true);
}
}
YourApp.isServerReachable = false;
},
200: function() {
if (YourApp.isServerReachable === false) {
if (doStop) {
// Reset the state here - state must be correct for functions triggered by the calee function
YourApp.isServerReachable = true;
if (!doNotTrigger) {
// Increment the connect trigger count to keep track of event trigger activity
YourApp.TIMERS.ConnectTriggerCount++;
YourApp.YOUR_EVENTS.trigger("FooConnect");
}
}
else {
return YourApp.checkServerStatus(true);
}
}
YourApp.isServerReachable = true;
}
},
complete: callback
});
};
YourApp.checkServerStatus(true, true);