Skip to content

Commit

Permalink
More tweaks to messaging to try and keep a more stable connection
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister committed Oct 5, 2022
1 parent 2570595 commit acd9670
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/366x366/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ console.log(`Application ID: ${appbit.applicationId}`);
console.log(`Build ID: ${appbit.buildId}`);

settings.applySettings();
msgq.send("app-launch", {});
msgq.send("send-settings", {});

msgq.onmessage = (messageKey, message) => {
Expand Down
25 changes: 13 additions & 12 deletions src/366x366/app/msgq.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import { peerSocket } from "messaging";
// Initialize
//====================================================================================================

let debugMessages = false;
let debugMessages = true;
let queue = [];
let waitingForReceipt = false;
let lastSend = null;
let retryTimeout = null;
let delayedProcessCallTimeout = null;
let aliveType = "msgq-alive-app";
let socketClosedSince = null;

if (peerSocket.readyState != peerSocket.OPEN) {
socketClosedSince = Date.now();
}

enqueue(aliveType, {}, 30000);
enqueue(aliveType, {});
setInterval(function () {
enqueue(aliveType, {}, 30000);
enqueue(aliveType, {});
}, 120000);

//====================================================================================================
Expand Down Expand Up @@ -101,9 +101,9 @@ function dequeue(id, messageKey) {
//====================================================================================================

function process() {
if (retryTimeout != null) {
clearTimeout(retryTimeout);
retryTimeout = null;
if (delayedProcessCallTimeout != null) {
clearTimeout(delayedProcessCallTimeout);
delayedProcessCallTimeout = null;
}

if (queue.length === 0) {
Expand All @@ -122,16 +122,16 @@ function process() {
if (peerSocket.readyState != peerSocket.OPEN) {
if (socketClosedSince != null) {
var socketClosedDuration = Date.now() - socketClosedSince;
if (socketClosedDuration > 180000) {
console.log(`Socket not open for over 3 minutes, killing app`);
if (socketClosedDuration > 600000) {
console.log(`Socket not open for over 10 minutes, killing app`);
appbit.exit();
}
} else {
socketClosedSince = Date.now();
}

console.log(`Socket not open, call process again in 1 seconds`);
retryTimeout = setTimeout(process, 1000);
console.log(`Socket not open, call process again in 5 seconds`);
delayedProcessCallTimeout = setTimeout(process, 5000);
return;
}

Expand All @@ -149,11 +149,12 @@ function process() {
console.log(`Sending message ${queueItem.id} - ${queueItem.messageKey} - ${JSON.stringify(queueItem.message)}`);
}
peerSocket.send({ msgqType: "msgq_message", msgqMessage: queueItem });
delayedProcessCallTimeout = setTimeout(process, 15000);
} catch (e) {
waitingForReceipt = false;
console.warn(e.message);
console.log(`Socket send error, call process again in 2 seconds`);
retryTimeout = setTimeout(process, 2000);
delayedProcessCallTimeout = setTimeout(process, 2000);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/366x366/app/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function sendPing(force = false) {
updateConnectionIndicator();
if (force || (phoneEl.style.display === "inline" && lastPingAge >= 60000)) {
try {
msgq.send("ping", {}, 60000);
msgq.send("ping", {});
lastPingSent = Date.now();
} catch (e) {
console.error(e.message);
Expand Down
10 changes: 1 addition & 9 deletions src/366x366/app/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,7 @@ export function fetchWeather() {
sendUnit = units.temperature ? units.temperature : "C";
}

msgq.send(
"weather",
{
unit: sendUnit,
},
{
timeout: 30000,
}
);
msgq.send("weather", { unit: sendUnit });
weatherLastRequest = Date.now();
} catch (e) {
console.error(e.message);
Expand Down
13 changes: 7 additions & 6 deletions src/366x366/companion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ weatherConditions[42] = "weather_CloudyWithThunderstormsNight_36px.png";
weatherConditions[43] = "weather_CloudyWithSleetNight_36px.png";
weatherConditions[44] = "weather_CloudyWithSnowNight_36px.png";

//Wake every 15 minutes
console.log("Set companion wake interval to 10 minutes");
companion.wakeInterval = 600000;
//Wake every 5 minutes
console.log("Set companion wake interval to 5 minutes");
companion.wakeInterval = 300000;

// Monitor for significant changes in physical location
console.log("Enable monitoring of significant location changes");
Expand All @@ -81,6 +81,7 @@ console.log(`Companion launch reason: ${JSON.stringify(companion.launchReasons)}
if (companion.launchReasons.locationChanged) {
locationChange(true);
}
msgq.send("companion-launch", companion.launchReasons);

// Settings have been changed
settingsStorage.addEventListener("change", (evt) => {
Expand Down Expand Up @@ -198,7 +199,7 @@ function sendWeather(unit) {
location: data.locations[0].name,
};
//console.log(`Weather:${JSON.stringify(sendData)}`);
msgq.send("weather", sendData, 30000);
msgq.send("weather", sendData);
}
})
.catch((ex) => {
Expand All @@ -209,12 +210,12 @@ function sendWeather(unit) {
condition: -1,
image: "weather_36px.png",
};
msgq.send("weather", sendData, 30000);
msgq.send("weather", sendData);
});
}

function sendPong() {
msgq.send("pong", {}, 60000);
msgq.send("pong", {});
}

function locationChange(initial) {
Expand Down
25 changes: 13 additions & 12 deletions src/366x366/companion/msgq.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import { peerSocket } from "messaging";
// Initialize
//====================================================================================================

let debugMessages = false;
let debugMessages = true;
let queue = [];
let waitingForReceipt = false;
let lastSend = null;
let retryTimeout = null;
let delayedProcessCallTimeout = null;
let aliveType = "msgq-alive-companion";
let socketClosedSince = null;

if (peerSocket.readyState != peerSocket.OPEN) {
socketClosedSince = Date.now();
}

enqueue(aliveType, {}, 30000);
enqueue(aliveType, {});
setInterval(function () {
enqueue(aliveType, {}, 30000);
enqueue(aliveType, {});
}, 120000);

//====================================================================================================
Expand Down Expand Up @@ -100,9 +100,9 @@ function dequeue(id, messageKey) {
//====================================================================================================

function process() {
if (retryTimeout != null) {
clearTimeout(retryTimeout);
retryTimeout = null;
if (delayedProcessCallTimeout != null) {
clearTimeout(delayedProcessCallTimeout);
delayedProcessCallTimeout = null;
}

if (queue.length === 0) {
Expand All @@ -121,15 +121,15 @@ function process() {
if (peerSocket.readyState != peerSocket.OPEN) {
if (socketClosedSince != null) {
var socketClosedDuration = Date.now() - socketClosedSince;
if (socketClosedDuration > 180000) {
console.log(`Socket not open for over 3 minutes`);
if (socketClosedDuration > 600000) {
console.log(`Socket not open for over 10 minutes`);
}
} else {
socketClosedSince = Date.now();
}

console.log(`Socket not open, call process again in 1 seconds`);
retryTimeout = setTimeout(process, 1000);
console.log(`Socket not open, call process again in 5 seconds`);
delayedProcessCallTimeout = setTimeout(process, 5000);
return;
}

Expand All @@ -147,11 +147,12 @@ function process() {
console.log(`Sending message ${queueItem.id} - ${queueItem.messageKey} - ${JSON.stringify(queueItem.message)}`);
}
peerSocket.send({ msgqType: "msgq_message", msgqMessage: queueItem });
delayedProcessCallTimeout = setTimeout(process, 15000);
} catch (e) {
waitingForReceipt = false;
console.warn(e.message);
console.log(`Socket send error, call process again in 2 seconds`);
retryTimeout = setTimeout(process, 2000);
delayedProcessCallTimeout = setTimeout(process, 2000);
}
}
}
Expand Down

0 comments on commit acd9670

Please sign in to comment.