trilium/services/messaging.js

83 lines
2.3 KiB
JavaScript
Raw Normal View History

const WebSocket = require('ws');
const utils = require('./utils');
const log = require('./log');
const sql = require('./sql');
const options = require('./options');
const sync_setup = require('./sync_setup');
let webSocketServer;
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) => {
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') {
sendPing(ws, message.lastSyncId);
}
2017-12-02 11:28:22 +08:00
else {
log.error('Unrecognized message: ');
log.error(message);
}
});
});
}
async function sendMessage(client, message) {
const jsonStr = JSON.stringify(message);
if (client.readyState === WebSocket.OPEN) {
client.send(jsonStr);
}
}
async function sendMessageToAllClients(message) {
const jsonStr = JSON.stringify(message);
2018-01-02 08:41:22 +08:00
log.info("Sending message to all clients: " + jsonStr);
webSocketServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(jsonStr);
}
});
}
async function sendPing(client, lastSentSyncId) {
const syncData = await sql.getAll("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
const lastSyncedPush = await options.getOption('last_synced_push');
const changesToPushCount = await sql.getFirstValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
await sendMessage(client, {
type: 'sync',
data: syncData,
changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0
});
}
module.exports = {
init,
sendMessageToAllClients
};