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/";
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 markers = {};
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){
if (typeof e.data != 'string') {
console.error("unsupported binary data on websocket; ignoring");
@ -120,7 +129,7 @@
switch (json.type) {
case "config":
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], {
center: {
lat: config.receiver_gps[0],
@ -129,6 +138,7 @@
zoom: 5
});
processUpdates(updateQueue);
updateQueue = [];
$.getScript("/static/lib/nite-overlay.js").done(function(){
nite.init(map);
setInterval(function() { nite.refresh() }, 10000); // every 10s
@ -146,16 +156,23 @@
}
};
ws.onclose = function(){
console.info("onclose");
clearMap();
setTimeout(connect, 5000);
};
window.onbeforeunload = function() { //http://stackoverflow.com/questions/4812686/closing-websocket-correctly-html5-javascript
ws.onclose = function () {};
ws.close();
};
/*
ws.onerror = function(){
console.info("onerror");
console.info("websocket error");
};
*/
};
connect();
var infowindow;
var showInfoWindow = function(locator, pos) {