mirror of
https://github.com/zadam/trilium.git
synced 2024-09-20 23:55:59 +08:00
websocket reimplementation of status requests
This commit is contained in:
parent
f433b30089
commit
992238f0b3
17
bin/www
17
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;
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -140,3 +140,30 @@ function initAjax() {
|
|||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
};
|
|
@ -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);
|
||||
}
|
||||
|
|
26
services/messaging.js
Normal file
26
services/messaging.js
Normal file
|
@ -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
|
||||
};
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue