trilium/src/services/ws.js

143 lines
4.2 KiB
JavaScript
Raw Normal View History

const WebSocket = require('ws');
const utils = require('./utils');
const log = require('./log');
const sql = require('./sql');
2019-10-23 03:59:51 +08:00
const syncMutexService = require('./sync_mutex');
let webSocketServer;
let lastAcceptedSyncIds = {};
function init(httpServer, sessionParser) {
webSocketServer = new WebSocket.Server({
verifyClient: (info, done) => {
sessionParser(info.req, {}, () => {
const allowed = utils.isElectron() || info.req.session.loggedIn;
if (!allowed) {
log.error("WebSocket connection not allowed because session is neither electron nor logged in.");
}
done(allowed)
});
},
server: httpServer
});
2017-12-02 11:28:22 +08:00
webSocketServer.on('connection', (ws, req) => {
ws.id = utils.randomString(10);
lastAcceptedSyncIds[ws.id] = 0;
console.log(`websocket client connected`);
2017-12-02 11:28:22 +08:00
ws.on('message', messageJson => {
const message = JSON.parse(messageJson);
if (message.type === 'log-error') {
log.error('JS Error: ' + message.error);
}
else if (message.type === 'ping') {
lastAcceptedSyncIds[ws.id] = message.lastSyncId;
syncMutexService.doExclusively(async () => await sendPing(ws));
}
2017-12-02 11:28:22 +08:00
else {
log.error('Unrecognized message: ');
log.error(message);
}
});
});
}
2019-10-29 01:42:22 +08:00
function sendMessage(client, message) {
const jsonStr = JSON.stringify(message);
if (client.readyState === WebSocket.OPEN) {
client.send(jsonStr);
}
}
2019-10-29 01:42:22 +08:00
function sendMessageToAllClients(message) {
const jsonStr = JSON.stringify(message);
2019-01-03 05:36:06 +08:00
if (webSocketServer) {
log.info("Sending message to all clients: " + jsonStr);
2018-01-02 08:41:22 +08:00
2019-01-03 05:36:06 +08:00
webSocketServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(jsonStr);
}
});
}
}
async function fillInAdditionalProperties(sync) {throw new Error("AAA");
// fill in some extra data needed by the frontend
if (sync.entityName === 'attributes') {
sync.noteId = await sql.getValue(`SELECT noteId
FROM attributes
WHERE attributeId = ?`, [sync.entityId]);
} else if (sync.entityName === 'note_revisions') {
sync.noteId = await sql.getValue(`SELECT noteId
FROM note_revisions
WHERE noteRevisionId = ?`, [sync.entityId]);
} else if (sync.entityName === 'branches') {
const {noteId, parentNoteId} = await sql.getRow(`SELECT noteId, parentNoteId
FROM branches
WHERE branchId = ?`, [sync.entityId]);
sync.noteId = noteId;
sync.parentNoteId = parentNoteId;
}
}
async function sendPing(client) {
const syncData = require('./sync_table').getEntitySyncsNewerThan(lastAcceptedSyncIds[client.id]);
for (const sync of syncData) {
try {
await fillInAdditionalProperties(sync);
}
catch (e) {
log.error("Could not fill additional properties for sync " + JSON.stringify(sync)
+ " because of error: " + e.message + ": " + e.stack);
2019-10-20 18:29:34 +08:00
}
}
const stats = require('./sync').stats;
2019-10-29 01:42:22 +08:00
sendMessage(client, {
type: 'sync',
data: syncData,
outstandingSyncs: stats.outstandingPushes + stats.outstandingPulls
});
}
function sendPingToAllClients() {
if (webSocketServer) {
webSocketServer.clients.forEach(function each(client) {
sendPing(client);
});
}
}
2019-10-29 01:42:22 +08:00
function refreshTree() {
sendMessageToAllClients({ type: 'refresh-tree' });
}
2019-10-29 01:42:22 +08:00
function syncPullInProgress() {
sendMessageToAllClients({ type: 'sync-pull-in-progress' });
2019-10-26 04:20:14 +08:00
}
2019-10-29 02:45:36 +08:00
function syncPullFinished() {
2019-10-29 01:42:22 +08:00
sendMessageToAllClients({ type: 'sync-pull-finished' });
2019-10-26 04:20:14 +08:00
}
module.exports = {
init,
sendMessageToAllClients,
2019-10-26 04:20:14 +08:00
refreshTree,
syncPullInProgress,
syncPullFinished,
sendPingToAllClients
};