2017-11-29 06:52:47 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const messaging = (function() {
|
2017-12-10 09:44:06 +08:00
|
|
|
const changesToPushCountEl = $("#changes-to-push-count");
|
2017-12-02 11:28:22 +08:00
|
|
|
let ws = null;
|
|
|
|
|
|
|
|
function logError(message) {
|
2017-12-07 09:11:45 +08:00
|
|
|
console.trace(message);
|
2017-12-02 11:28:22 +08:00
|
|
|
|
|
|
|
if (ws && ws.readyState === 1) {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: 'log-error',
|
|
|
|
error: message
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2017-11-29 06:52:47 +08:00
|
|
|
|
2017-12-02 11:28:22 +08:00
|
|
|
function messageHandler(event) {
|
2017-11-29 06:52:47 +08:00
|
|
|
const message = JSON.parse(event.data);
|
|
|
|
|
|
|
|
if (message.type === 'sync') {
|
|
|
|
lastPingTs = new Date().getTime();
|
|
|
|
const data = message.data;
|
|
|
|
|
|
|
|
if (data.notes_tree) {
|
|
|
|
console.log("Reloading tree because of background changes");
|
|
|
|
|
|
|
|
noteTree.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.notes && data.notes.includes(noteEditor.getCurrentNoteId())) {
|
|
|
|
showMessage('Reloading note because background change');
|
|
|
|
|
|
|
|
noteEditor.reload();
|
|
|
|
}
|
|
|
|
|
2017-12-03 23:42:23 +08:00
|
|
|
if (data.recent_notes) {
|
|
|
|
console.log("Reloading recent notes because of background changes");
|
|
|
|
|
|
|
|
recentNotes.reload();
|
|
|
|
}
|
|
|
|
|
2017-11-29 06:52:47 +08:00
|
|
|
changesToPushCountEl.html(message.changesToPushCount);
|
|
|
|
}
|
2017-12-13 12:47:17 +08:00
|
|
|
else if (message.type === 'sync-hash-check-failed') {
|
|
|
|
showError("Sync check failed!", 60000);
|
|
|
|
}
|
2017-11-29 06:52:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function connectWebSocket() {
|
2017-12-03 23:12:16 +08:00
|
|
|
const protocol = document.location.protocol === 'https:' ? 'wss' : 'ws';
|
|
|
|
|
2017-11-29 06:52:47 +08:00
|
|
|
// use wss for secure messaging
|
2017-12-03 23:12:16 +08:00
|
|
|
ws = new WebSocket(protocol + "://" + location.host);
|
|
|
|
ws.onopen = event => console.log("Connected to server with WebSocket");
|
2017-11-29 06:52:47 +08:00
|
|
|
ws.onmessage = messageHandler;
|
|
|
|
ws.onclose = function(){
|
|
|
|
// Try to reconnect in 5 seconds
|
|
|
|
setTimeout(() => connectWebSocket(), 5000);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
connectWebSocket();
|
|
|
|
|
|
|
|
let lastPingTs = new Date().getTime();
|
|
|
|
let connectionBrokenNotification = null;
|
|
|
|
|
|
|
|
setInterval(async () => {
|
|
|
|
if (new Date().getTime() - lastPingTs > 5000) {
|
|
|
|
if (!connectionBrokenNotification) {
|
|
|
|
connectionBrokenNotification = $.notify({
|
|
|
|
// options
|
|
|
|
message: "Lost connection to server"
|
|
|
|
},{
|
|
|
|
// settings
|
|
|
|
type: 'danger',
|
|
|
|
delay: 100000000 // keep it until we explicitly close it
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (connectionBrokenNotification) {
|
|
|
|
await connectionBrokenNotification.close();
|
|
|
|
connectionBrokenNotification = null;
|
|
|
|
|
|
|
|
showMessage("Re-connected to server");
|
|
|
|
}
|
|
|
|
}, 3000);
|
2017-12-02 11:28:22 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
logError
|
|
|
|
};
|
2017-11-29 06:52:47 +08:00
|
|
|
})();
|