2017-11-26 06:43:05 +08:00
|
|
|
const WebSocket = require('ws');
|
2017-12-01 12:50:42 +08:00
|
|
|
const utils = require('./utils');
|
|
|
|
const log = require('./log');
|
2017-12-20 12:22:21 +08:00
|
|
|
const sql = require('./sql');
|
2017-11-26 06:43:05 +08:00
|
|
|
|
|
|
|
let webSocketServer;
|
|
|
|
|
2017-12-01 12:50:42 +08:00
|
|
|
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) => {
|
2017-11-26 06:43:05 +08:00
|
|
|
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);
|
|
|
|
}
|
2017-12-20 12:22:21 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2017-11-26 06:43:05 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-20 12:22:21 +08:00
|
|
|
async function sendMessage(client, message) {
|
|
|
|
const jsonStr = JSON.stringify(message);
|
|
|
|
|
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
client.send(jsonStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendMessageToAllClients(message) {
|
2017-11-26 06:43:05 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-26 06:43:05 +08:00
|
|
|
}
|
|
|
|
|
2017-12-20 12:22:21 +08:00
|
|
|
async function sendPing(client, lastSentSyncId) {
|
2018-01-30 06:41:59 +08:00
|
|
|
const syncData = await sql.getRows("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
|
2019-06-01 18:14:09 +08:00
|
|
|
|
|
|
|
for (const sync of syncData) {
|
2019-10-20 18:29:34 +08:00
|
|
|
// fill in some extra data needed by the frontend
|
2019-06-01 18:14:09 +08:00
|
|
|
if (sync.entityName === 'attributes') {
|
|
|
|
sync.noteId = await sql.getValue(`SELECT noteId FROM attributes WHERE attributeId = ?`, [sync.entityId]);
|
|
|
|
}
|
2019-08-07 05:20:27 +08:00
|
|
|
else if (sync.entityName === 'note_revisions') {
|
|
|
|
sync.noteId = await sql.getValue(`SELECT noteId FROM note_revisions WHERE noteRevisionId = ?`, [sync.entityId]);
|
|
|
|
}
|
2019-10-20 18:29:34 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-06-01 18:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 03:43:15 +08:00
|
|
|
const stats = require('./sync').stats;
|
2017-12-20 12:22:21 +08:00
|
|
|
|
|
|
|
await sendMessage(client, {
|
|
|
|
type: 'sync',
|
|
|
|
data: syncData,
|
2018-07-25 03:43:15 +08:00
|
|
|
outstandingSyncs: stats.outstandingPushes + stats.outstandingPulls
|
2017-12-20 12:22:21 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-10 23:59:50 +08:00
|
|
|
async function refreshTree() {
|
|
|
|
await sendMessageToAllClients({ type: 'refresh-tree' });
|
|
|
|
}
|
|
|
|
|
2017-11-26 06:43:05 +08:00
|
|
|
module.exports = {
|
|
|
|
init,
|
2019-02-10 23:59:50 +08:00
|
|
|
sendMessageToAllClients,
|
2019-02-11 02:36:03 +08:00
|
|
|
refreshTree
|
2017-11-26 06:43:05 +08:00
|
|
|
};
|