2018-03-25 23:09:17 +08:00
|
|
|
import utils from './utils.js';
|
2018-03-26 09:29:35 +08:00
|
|
|
import infoService from "./info.js";
|
2017-11-29 06:52:47 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
const $changesToPushCount = $("#changes-to-push-count");
|
2017-11-29 06:52:47 +08:00
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
const messageHandlers = [];
|
|
|
|
|
2018-03-26 01:08:58 +08:00
|
|
|
let ws;
|
|
|
|
let lastSyncId;
|
|
|
|
let lastPingTs;
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function logError(message) {
|
|
|
|
console.log(utils.now(), message); // needs to be separate from .trace()
|
|
|
|
console.trace();
|
2017-12-20 12:22:21 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (ws && ws.readyState === 1) {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: 'log-error',
|
|
|
|
error: message
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 02:46:18 +08:00
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
function subscribeToMessages(messageHandler) {
|
|
|
|
messageHandlers.push(messageHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMessage(event) {
|
2018-03-25 23:09:17 +08:00
|
|
|
const message = JSON.parse(event.data);
|
2017-12-20 12:22:21 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (message.type === 'sync') {
|
|
|
|
lastPingTs = new Date().getTime();
|
2018-01-07 11:56:54 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (message.data.length > 0) {
|
|
|
|
console.log(utils.now(), "Sync data: ", message.data);
|
2017-11-29 06:52:47 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
lastSyncId = message.data[message.data.length - 1].id;
|
|
|
|
}
|
2017-11-29 06:52:47 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
const syncData = message.data.filter(sync => sync.sourceId !== glob.sourceId);
|
2017-11-29 06:52:47 +08:00
|
|
|
|
2018-03-26 09:16:57 +08:00
|
|
|
for (const messageHandler of messageHandlers) {
|
|
|
|
messageHandler(syncData);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$changesToPushCount.html(message.changesToPushCount);
|
|
|
|
}
|
|
|
|
else if (message.type === 'sync-hash-check-failed') {
|
2018-03-26 09:29:35 +08:00
|
|
|
infoService.showError("Sync check failed!", 60000);
|
2017-11-29 06:52:47 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
else if (message.type === 'consistency-checks-failed') {
|
2018-03-26 09:29:35 +08:00
|
|
|
infoService.showError("Consistency checks failed! See logs for details.", 50 * 60000);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function connectWebSocket() {
|
|
|
|
const protocol = document.location.protocol === 'https:' ? 'wss' : 'ws';
|
|
|
|
|
|
|
|
// use wss for secure messaging
|
|
|
|
const ws = new WebSocket(protocol + "://" + location.host);
|
|
|
|
ws.onopen = event => console.log(utils.now(), "Connected to server with WebSocket");
|
2018-03-26 09:16:57 +08:00
|
|
|
ws.onmessage = handleMessage;
|
2018-03-25 23:09:17 +08:00
|
|
|
ws.onclose = function(){
|
|
|
|
// Try to reconnect in 5 seconds
|
|
|
|
setTimeout(() => connectWebSocket(), 5000);
|
|
|
|
};
|
2017-11-29 06:52:47 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
2018-03-26 01:08:58 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
ws = connectWebSocket();
|
|
|
|
|
|
|
|
lastSyncId = glob.maxSyncIdAtLoad;
|
|
|
|
lastPingTs = new Date().getTime();
|
|
|
|
let connectionBrokenNotification = null;
|
|
|
|
|
|
|
|
setInterval(async () => {
|
|
|
|
if (new Date().getTime() - lastPingTs > 30000) {
|
|
|
|
if (!connectionBrokenNotification) {
|
|
|
|
connectionBrokenNotification = $.notify({
|
|
|
|
// options
|
|
|
|
message: "Lost connection to server"
|
|
|
|
},{
|
2018-04-02 05:41:28 +08:00
|
|
|
// options
|
2018-03-26 01:08:58 +08:00
|
|
|
type: 'danger',
|
|
|
|
delay: 100000000 // keep it until we explicitly close it
|
|
|
|
});
|
|
|
|
}
|
2017-11-29 06:52:47 +08:00
|
|
|
}
|
2018-03-26 01:08:58 +08:00
|
|
|
else if (connectionBrokenNotification) {
|
|
|
|
await connectionBrokenNotification.close();
|
|
|
|
connectionBrokenNotification = null;
|
2017-11-29 06:52:47 +08:00
|
|
|
|
2018-03-26 09:29:35 +08:00
|
|
|
infoService.showMessage("Re-connected to server");
|
2018-03-26 01:08:58 +08:00
|
|
|
}
|
2017-12-20 12:22:21 +08:00
|
|
|
|
2018-03-26 01:08:58 +08:00
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: 'ping',
|
|
|
|
lastSyncId: lastSyncId
|
|
|
|
}));
|
|
|
|
}, 1000);
|
2018-03-25 23:09:17 +08:00
|
|
|
}, 1000);
|
2017-12-02 11:28:22 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
2018-03-26 09:16:57 +08:00
|
|
|
logError,
|
|
|
|
subscribeToMessages
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|