From 992238f0b35e7417a78b514fa0e16575bd97a6ac Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 25 Nov 2017 17:43:05 -0500 Subject: [PATCH] websocket reimplementation of status requests --- bin/www | 17 ++++++++++------- package.json | 3 ++- public/javascripts/init.js | 29 ++++++++++++++++++++++++++++- public/javascripts/status.js | 22 +++++++++++----------- services/messaging.js | 26 ++++++++++++++++++++++++++ services/sync_table.js | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 113 insertions(+), 20 deletions(-) create mode 100644 services/messaging.js diff --git a/bin/www b/bin/www index b08686707..d24928e78 100755 --- a/bin/www +++ b/bin/www @@ -16,6 +16,7 @@ const https = require('https'); const config = require('../services/config'); const log = require('../services/log'); const app_info = require('../services/app_info'); +const messaging = require('../services/messaging'); const port = normalizePort(config['Network']['port'] || '3000'); app.set('port', port); @@ -23,7 +24,7 @@ app.set('port', port); /** * Create HTTP server. */ -let server; +let httpServer; if (config['Network']['https']) { const options = { @@ -31,12 +32,12 @@ if (config['Network']['https']) { cert: fs.readFileSync(config['Network']['certPath']) }; - server = https.createServer(options, app); + httpServer = https.createServer(options, app); log.info("App HTTPS server starting up at port " + port); } else { - server = http.createServer(app); + httpServer = http.createServer(app); log.info("App HTTP server starting up at port " + port); } @@ -47,9 +48,11 @@ log.info(JSON.stringify(app_info, null, 2)); * Listen on provided port, on all network interfaces. */ -server.listen(port); -server.on('error', onError); -server.on('listening', onListening); +httpServer.listen(port); +httpServer.on('error', onError); +httpServer.on('listening', onListening); + +messaging.init(httpServer); /** * Normalize a port into a number, string, or false. @@ -106,7 +109,7 @@ function onError(error) { */ function onListening() { - const addr = server.address(); + const addr = httpServer.address(); const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; diff --git a/package.json b/package.json index bab7661da..cb353148e 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "body-parser": "~1.18.2", "cookie-parser": "~1.4.3", "debug": "~3.1.0", + "devtron": "^1.4.0", "ejs": "~2.5.7", "electron": "^1.8.2-beta.2", "electron-debug": "^1.0.0", @@ -32,7 +33,7 @@ "session-file-store": "^1.1.2", "simple-node-logger": "^0.93.30", "sqlite": "^2.8.0", - "devtron": "^1.4.0" + "ws": "^3.3.2" }, "devDependencies": { "electron-compile": "^6.4.2", diff --git a/public/javascripts/init.js b/public/javascripts/init.js index 1cb4b3eb9..60df6451e 100644 --- a/public/javascripts/init.js +++ b/public/javascripts/init.js @@ -139,4 +139,31 @@ function initAjax() { }); } -initAjax(); \ No newline at end of file +initAjax(); + +// use wss for secure messaging +const ws = new WebSocket("ws://" + location.host); +ws.onopen = function (event) { +}; + +ws.onmessage = function (event) { + console.log(event.data); + + const message = JSON.parse(event.data); + + if (message.type === 'sync') { + 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(); + } + } +}; \ No newline at end of file diff --git a/public/javascripts/status.js b/public/javascripts/status.js index a35914b39..fcd3a92d6 100644 --- a/public/javascripts/status.js +++ b/public/javascripts/status.js @@ -26,17 +26,17 @@ const status = (function() { } }); - if (resp.changedTree) { - console.log("Reloading tree because of background changes"); - - noteTree.reload(); - } - - if (resp.changedCurrentNote) { - showMessage('Reloading note because background change'); - - noteEditor.reload(); - } + // if (resp.changedTree) { + // console.log("Reloading tree because of background changes"); + // + // noteTree.reload(); + // } + // + // if (resp.changedCurrentNote) { + // showMessage('Reloading note because background change'); + // + // noteEditor.reload(); + // } changesToPushCountEl.html(resp.changesToPushCount); } diff --git a/services/messaging.js b/services/messaging.js new file mode 100644 index 000000000..90079af7b --- /dev/null +++ b/services/messaging.js @@ -0,0 +1,26 @@ +const utils = require('../services/utils'); +const WebSocket = require('ws'); + +let webSocketServer; + +function init(httpServer) { + webSocketServer = new WebSocket.Server({server: httpServer}); + webSocketServer.on('connection', function connection(ws, req) { + console.log("websocket client connected"); + }); +} + +async function send(message) { + const jsonStr = JSON.stringify(message); + + webSocketServer.clients.forEach(function each(client) { + if (client.readyState === WebSocket.OPEN) { + client.send(jsonStr); + } + }); +} + +module.exports = { + init, + send +}; \ No newline at end of file diff --git a/services/sync_table.js b/services/sync_table.js index e34b2d63c..42a6cf6ae 100644 --- a/services/sync_table.js +++ b/services/sync_table.js @@ -1,6 +1,7 @@ const sql = require('./sql'); const source_id = require('./source_id'); const utils = require('./utils'); +const messaging = require('./messaging'); async function addNoteSync(noteId, sourceId) { await addEntitySync("notes", noteId, sourceId) @@ -35,6 +36,41 @@ async function addEntitySync(entityName, entityId, sourceId) { }); } +let startTime = utils.nowTimestamp(); +let sentSyncId = []; + +setInterval(async () => { + const syncs = await sql.getResults("SELECT * FROM sync WHERE sync_date >= ?", [startTime]); + startTime = utils.nowTimestamp(); + + const data = {}; + const syncIds = []; + + for (const sync of syncs) { + if (sentSyncId.includes(sync.id)) { + continue; + } + + if (!data[sync.entity_name]) { + data[sync.entity_name] = []; + } + + data[sync.entity_name].push(sync.entity_id); + syncIds.push(sync.id); + } + + if (syncIds.length > 0) { + messaging.send({ + type: 'sync', + data: data + }); + + for (const syncId of syncIds) { + sentSyncId.push(syncId); + } + } +}, 1000); + module.exports = { addNoteSync, addNoteTreeSync,