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');
|
|
|
|
const options = require('./options');
|
|
|
|
const sync_setup = require('./sync_setup');
|
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);
|
|
|
|
|
|
|
|
webSocketServer.clients.forEach(function each(client) {
|
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
client.send(jsonStr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-20 12:22:21 +08:00
|
|
|
async function sendPing(client, lastSentSyncId) {
|
2017-12-24 00:02:38 +08:00
|
|
|
const syncData = await sql.getAll("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
|
2017-12-20 12:22:21 +08:00
|
|
|
|
|
|
|
const lastSyncedPush = await options.getOption('last_synced_push');
|
|
|
|
|
2017-12-24 00:02:38 +08:00
|
|
|
const changesToPushCount = await sql.getFirstValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
|
2017-12-20 12:22:21 +08:00
|
|
|
|
|
|
|
await sendMessage(client, {
|
|
|
|
type: 'sync',
|
|
|
|
data: syncData,
|
|
|
|
changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-26 06:43:05 +08:00
|
|
|
module.exports = {
|
|
|
|
init,
|
2017-12-20 12:22:21 +08:00
|
|
|
sendMessageToAllClients
|
2017-11-26 06:43:05 +08:00
|
|
|
};
|