From 91c2f9e7cb5fe74a45ee65d474fa77d02a63ea87 Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 28 Nov 2017 17:52:47 -0500 Subject: [PATCH] websocket stuff separated into messaging.js --- public/javascripts/init.js | 67 +------------------------------- public/javascripts/messaging.js | 68 +++++++++++++++++++++++++++++++++ services/notes.js | 2 - views/index.ejs | 1 + 4 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 public/javascripts/messaging.js diff --git a/public/javascripts/init.js b/public/javascripts/init.js index 60d3b0ee0..1cb4b3eb9 100644 --- a/public/javascripts/init.js +++ b/public/javascripts/init.js @@ -139,69 +139,4 @@ function initAjax() { }); } -initAjax(); - -function messageHandler(event) { - console.log(event.data); - - 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(); - } - - const changesToPushCountEl = $("#changesToPushCount"); - changesToPushCountEl.html(message.changesToPushCount); - } -} - -let ws = null; - -function connectWebSocket() { - // use wss for secure messaging - ws = new WebSocket("ws://" + location.host); - ws.onopen = function (event) {}; - 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); \ No newline at end of file +initAjax(); \ No newline at end of file diff --git a/public/javascripts/messaging.js b/public/javascripts/messaging.js new file mode 100644 index 000000000..2e2b77167 --- /dev/null +++ b/public/javascripts/messaging.js @@ -0,0 +1,68 @@ +"use strict"; + +const messaging = (function() { + function messageHandler(event) { + console.log(event.data); + + 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(); + } + + const changesToPushCountEl = $("#changesToPushCount"); + changesToPushCountEl.html(message.changesToPushCount); + } + } + + let ws = null; + + function connectWebSocket() { + // use wss for secure messaging + ws = new WebSocket("ws://" + location.host); + ws.onopen = function (event) {}; + 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); +})(); \ No newline at end of file diff --git a/services/notes.js b/services/notes.js index 45bac7e7e..664e22769 100644 --- a/services/notes.js +++ b/services/notes.js @@ -137,8 +137,6 @@ async function updateNote(noteId, newNote, ctx) { await encryptNote(newNote, ctx); } - const origNoteDetail = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]); - const now = utils.nowTimestamp(); const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval')); diff --git a/views/index.ejs b/views/index.ejs index a29a5167f..ff03ad98d 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -327,6 +327,7 @@ + \ No newline at end of file