Wake detector
This commit is contained in:
parent
c5752bff91
commit
393dacde9b
38
index.js
38
index.js
|
@ -82,6 +82,35 @@ JQueryEventRouter.prototype.handleEvent = function (e) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
// Wake detector - notices when something (such as
|
||||||
|
// suspension/sleeping!) has caused periodic activities to be
|
||||||
|
// interrupted, and warns others about it
|
||||||
|
// Inspired by http://blog.alexmaccaw.com/javascript-wake-event
|
||||||
|
|
||||||
|
function WakeDetector(period) {
|
||||||
|
this.message = "wake";
|
||||||
|
this.period = period || 10000;
|
||||||
|
this.mostRecentTrigger = +(new Date());
|
||||||
|
this.timerId = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
WakeDetector.prototype.boot = function () {
|
||||||
|
var self = this;
|
||||||
|
World.updateRoutes([pub(this.message)]);
|
||||||
|
this.timerId = setInterval(World.wrap(function () { self.trigger(); }), this.period);
|
||||||
|
};
|
||||||
|
|
||||||
|
WakeDetector.prototype.handleEvent = function (e) {};
|
||||||
|
|
||||||
|
WakeDetector.prototype.trigger = function () {
|
||||||
|
var now = +(new Date());
|
||||||
|
if (now - this.mostRecentTrigger > this.period * 1.5) {
|
||||||
|
World.send(this.message);
|
||||||
|
}
|
||||||
|
this.mostRecentTrigger = now;
|
||||||
|
};
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
// WebSocket client driver
|
// WebSocket client driver
|
||||||
|
|
||||||
|
@ -176,6 +205,7 @@ WebSocketConnection.prototype.handleEvent = function (e) {
|
||||||
|
|
||||||
WebSocketConnection.prototype.forceclose = function () {
|
WebSocketConnection.prototype.forceclose = function () {
|
||||||
if (this.sock) {
|
if (this.sock) {
|
||||||
|
console.log("WebSocketConnection.forceclose called");
|
||||||
this.sock.close();
|
this.sock.close();
|
||||||
this.sock = null;
|
this.sock = null;
|
||||||
}
|
}
|
||||||
|
@ -306,6 +336,7 @@ $(document).ready(function () {
|
||||||
console.log('starting ground boot');
|
console.log('starting ground boot');
|
||||||
// World.spawn(new Spy());
|
// World.spawn(new Spy());
|
||||||
spawnJQueryDriver();
|
spawnJQueryDriver();
|
||||||
|
World.spawn(new WakeDetector());
|
||||||
var wsconn = new WebSocketConnection("broker", $("#wsurl").val(), true);
|
var wsconn = new WebSocketConnection("broker", $("#wsurl").val(), true);
|
||||||
World.spawn(wsconn);
|
World.spawn(wsconn);
|
||||||
World.spawn({
|
World.spawn({
|
||||||
|
@ -336,7 +367,8 @@ $(document).ready(function () {
|
||||||
nym: function () { return $("#nym").val(); },
|
nym: function () { return $("#nym").val(); },
|
||||||
currentStatus: function () { return $("#status").val(); },
|
currentStatus: function () { return $("#status").val(); },
|
||||||
subscriptions: function () {
|
subscriptions: function () {
|
||||||
return [sub(["jQuery", "#send_chat", "click", __]),
|
return [sub("wake"),
|
||||||
|
sub(["jQuery", "#send_chat", "click", __]),
|
||||||
sub(["jQuery", "#nym", "change", __]),
|
sub(["jQuery", "#nym", "change", __]),
|
||||||
sub(["jQuery", "#status", "change", __]),
|
sub(["jQuery", "#status", "change", __]),
|
||||||
sub(["jQuery", "#wsurl", "change", __]),
|
sub(["jQuery", "#wsurl", "change", __]),
|
||||||
|
@ -352,6 +384,10 @@ $(document).ready(function () {
|
||||||
updateNymList(e.routes);
|
updateNymList(e.routes);
|
||||||
break;
|
break;
|
||||||
case "message":
|
case "message":
|
||||||
|
if (e.message === "wake") {
|
||||||
|
wsconn.forceclose();
|
||||||
|
return;
|
||||||
|
}
|
||||||
switch (e.message[0]) {
|
switch (e.message[0]) {
|
||||||
case "jQuery":
|
case "jQuery":
|
||||||
switch (e.message[1]) {
|
switch (e.message[1]) {
|
||||||
|
|
Loading…
Reference in New Issue