move signalr start to function, restart if failure after 2 secs

This commit is contained in:
Ryan Peters 2023-01-26 11:29:15 -05:00
parent e9222c1ee2
commit b1a5d24d6c

View File

@ -1,11 +1,14 @@
let connection = new signalR.HubConnectionBuilder().withUrl("/noteHub").build(); let connection = new signalR.HubConnectionBuilder().withUrl("/noteHub").build();
connection.start().then(function () { let start = function () {
connection.start().then(function () {
console.log('Started websocket listener'); console.log('Started websocket listener');
}).catch(function (err) { }).catch(function (err) {
console.error(err.toString()); console.error(err.toString());
return alert('Connection error. Reload page.'); return alert('Connection error. Reload page.');
}); });
}
let showToast = function (selector) { let showToast = function (selector) {
@ -26,10 +29,14 @@ let saveContent = function ($textarea) {
connection.invoke('SaveNote', content).then(function () { connection.invoke('SaveNote', content).then(function () {
showToast('#saved-indicator'); showToast('#saved-indicator');
}).catch(function (err) { }).catch(function (err) {
return alert('Connection error. Reload page.'); console.error(err.toString());
setTimeout(start, 2000);
}); });
}; };
// start the signalr connection
start();
$(function () { $(function () {
let $textarea = $('textarea'); let $textarea = $('textarea');