mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
websocket reconnection
This commit is contained in:
parent
3a30aba42b
commit
abb122e2a9
@ -141,12 +141,7 @@ function initAjax() {
|
|||||||
|
|
||||||
initAjax();
|
initAjax();
|
||||||
|
|
||||||
// use wss for secure messaging
|
function messageHandler(event) {
|
||||||
const ws = new WebSocket("ws://" + location.host);
|
|
||||||
ws.onopen = function (event) {
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.onmessage = function (event) {
|
|
||||||
console.log(event.data);
|
console.log(event.data);
|
||||||
|
|
||||||
const message = JSON.parse(event.data);
|
const message = JSON.parse(event.data);
|
||||||
@ -170,12 +165,43 @@ ws.onmessage = function (event) {
|
|||||||
const changesToPushCountEl = $("#changesToPushCount");
|
const changesToPushCountEl = $("#changesToPushCount");
|
||||||
changesToPushCountEl.html(message.changesToPushCount);
|
changesToPushCountEl.html(message.changesToPushCount);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
let ws = null;
|
||||||
|
|
||||||
|
function connectWebSocket() {
|
||||||
|
// use wss for secure messaging
|
||||||
|
ws = new WebSocket("ws://" + location.host);
|
||||||
|
ws.onopen = function (event) {};
|
||||||
|
ws.onmessage = messageHandler;
|
||||||
|
ws.onclose = function(){
|
||||||
|
// Try to reconnect in 5 seconds
|
||||||
|
setTimeout(() => connectWebSocket(), 1000);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
connectWebSocket();
|
||||||
|
|
||||||
let lastPingTs = new Date().getTime();
|
let lastPingTs = new Date().getTime();
|
||||||
|
let connectionBrokenNotification = null;
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
if (new Date().getTime() - lastPingTs > 5000) {
|
if (new Date().getTime() - lastPingTs > 5000) {
|
||||||
showError("No communication with server");
|
if (!connectionBrokenNotification) {
|
||||||
|
connectionBrokenNotification = $.notify({
|
||||||
|
// options
|
||||||
|
message: "Lost connection to server"
|
||||||
|
},{
|
||||||
|
// settings
|
||||||
|
type: 'danger',
|
||||||
|
delay: 100000000
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (connectionBrokenNotification) {
|
||||||
|
connectionBrokenNotification.close();
|
||||||
|
connectionBrokenNotification = null;
|
||||||
|
|
||||||
|
showMessage("Re-connected to server");
|
||||||
}
|
}
|
||||||
}, 3000);
|
}, 3000);
|
Loading…
x
Reference in New Issue
Block a user