automatic map reconnection

This commit is contained in:
Jakob Ketterl 2019-07-10 23:13:03 +02:00
parent 596c868b9d
commit d57f9de21e

View File

@ -21,11 +21,6 @@
var ws_url = protocol + "://" + (window.location.origin.split("://")[1]) + "/ws/"; var ws_url = protocol + "://" + (window.location.origin.split("://")[1]) + "/ws/";
if (!("WebSocket" in window)) return; if (!("WebSocket" in window)) return;
var ws = new WebSocket(ws_url);
ws.onopen = function(){
ws.send("SERVER DE CLIENT client=map.js type=map");
};
var map; var map;
var markers = {}; var markers = {};
var rectangles = {}; var rectangles = {};
@ -106,6 +101,20 @@
}); });
}; };
var clearMap = function(){
var reset = function(callsign, item) { item.setMap(); };
$.each(markers, reset);
$.each(rectangles, reset);
markers = {};
rectangles = {};
};
var connect = function(){
var ws = new WebSocket(ws_url);
ws.onopen = function(){
ws.send("SERVER DE CLIENT client=map.js type=map");
};
ws.onmessage = function(e){ ws.onmessage = function(e){
if (typeof e.data != 'string') { if (typeof e.data != 'string') {
console.error("unsupported binary data on websocket; ignoring"); console.error("unsupported binary data on websocket; ignoring");
@ -120,7 +129,7 @@
switch (json.type) { switch (json.type) {
case "config": case "config":
var config = json.value; var config = json.value;
$.getScript("https://maps.googleapis.com/maps/api/js?key=" + config.google_maps_api_key).done(function(){ if (!map) $.getScript("https://maps.googleapis.com/maps/api/js?key=" + config.google_maps_api_key).done(function(){
map = new google.maps.Map($('.openwebrx-map')[0], { map = new google.maps.Map($('.openwebrx-map')[0], {
center: { center: {
lat: config.receiver_gps[0], lat: config.receiver_gps[0],
@ -129,6 +138,7 @@
zoom: 5 zoom: 5
}); });
processUpdates(updateQueue); processUpdates(updateQueue);
updateQueue = [];
$.getScript("/static/lib/nite-overlay.js").done(function(){ $.getScript("/static/lib/nite-overlay.js").done(function(){
nite.init(map); nite.init(map);
setInterval(function() { nite.refresh() }, 10000); // every 10s setInterval(function() { nite.refresh() }, 10000); // every 10s
@ -146,16 +156,23 @@
} }
}; };
ws.onclose = function(){ ws.onclose = function(){
console.info("onclose"); clearMap();
setTimeout(connect, 5000);
}; };
window.onbeforeunload = function() { //http://stackoverflow.com/questions/4812686/closing-websocket-correctly-html5-javascript window.onbeforeunload = function() { //http://stackoverflow.com/questions/4812686/closing-websocket-correctly-html5-javascript
ws.onclose = function () {}; ws.onclose = function () {};
ws.close(); ws.close();
}; };
/*
ws.onerror = function(){ ws.onerror = function(){
console.info("onerror"); console.info("websocket error");
}; };
*/
};
connect();
var infowindow; var infowindow;
var showInfoWindow = function(locator, pos) { var showInfoWindow = function(locator, pos) {