Auto-reconnect if event source connection lost

pull/18/head
Sami Mokaddem 2017-10-23 17:50:56 +02:00
parent 13305e627f
commit 366f1d826e
2 changed files with 40 additions and 32 deletions

View File

@ -103,11 +103,10 @@ sources.addSource('global');
var curNumLog = 0; var curNumLog = 0;
var curMaxDataNumLog = 0; var curMaxDataNumLog = 0;
var source_log;
$(document).ready(function () { function connect_source_log() {
createHead(function() { source_log = new EventSource(urlForLogs);
if (!!window.EventSource) {
var source_log = new EventSource(urlForLogs);
source_log.onopen = function(){ source_log.onopen = function(){
//console.log('connection is opened. '+source_log.readyState); //console.log('connection is opened. '+source_log.readyState);
@ -115,17 +114,23 @@ $(document).ready(function () {
source_log.onerror = function(){ source_log.onerror = function(){
console.log('error: '+source_log.readyState); console.log('error: '+source_log.readyState);
setTimeout(function() { location.reload(); }, 5000); setTimeout(function() { connect_source_log(); }, 5000);
}; };
source_log.onmessage = function(event) { source_log.onmessage = function(event) {
var json = jQuery.parseJSON( event.data ); var json = jQuery.parseJSON( event.data );
updateLogTable(json.feedName, json.log); updateLogTable(json.feedName, json.log);
}; };
}
$(document).ready(function () {
createHead(function() {
if (!!window.EventSource) {
connect_source_log();
} else { } else {
console.log("No event source_log"); console.log("No event source_log");
} }
}); });
$( "#rotation_wait_time_selector" ).change(function() { $( "#rotation_wait_time_selector" ).change(function() {

View File

@ -89,7 +89,6 @@ class MapEventManager {
$("#textMap1").fadeOut(400, function(){ $(this).text(mapEvent.text); }).fadeIn(400); $("#textMap1").fadeOut(400, function(){ $(this).text(mapEvent.text); }).fadeIn(400);
if(ROTATIONWAITTIME != 0) { if(ROTATIONWAITTIME != 0) {
console.log(ROTATIONWAITTIME);
this._timeoutRotate = setTimeout(function(){ mapEventManager.rotateMap(); }, ROTATIONWAITTIME); this._timeoutRotate = setTimeout(function(){ mapEventManager.rotateMap(); }, ROTATIONWAITTIME);
} }
} }
@ -159,7 +158,9 @@ $(function(){
}); });
// Subscribe to the flask eventStream // Subscribe to the flask eventStream
var source_map = new EventSource(urlForMaps); var source_map;
function connect_source_map() {
source_map = new EventSource(urlForMaps);
source_map.onmessage = function(event) { source_map.onmessage = function(event) {
var json = jQuery.parseJSON( event.data ); var json = jQuery.parseJSON( event.data );
var marker = L.marker([json.coord.lat, json.coord.lon]).addTo(myOpenStreetMap); var marker = L.marker([json.coord.lat, json.coord.lon]).addTo(myOpenStreetMap);
@ -172,5 +173,7 @@ source_map.onopen = function(){
}; };
source_map.onerror = function(){ source_map.onerror = function(){
console.log('error: '+source_map.readyState); console.log('error: '+source_map.readyState);
setTimeout(function() { location.reload(); }, 5000); setTimeout(function() { connect_source_map(); }, 5000);
}; };
}
connect_source_map()