automatic map reconnection

This commit is contained in:
Jakob Ketterl 2019-07-10 23:13:03 +02:00
parent 596c868b9d
commit d57f9de21e
1 changed files with 69 additions and 52 deletions

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,57 +101,79 @@
});
};
ws.onmessage = function(e){
if (typeof e.data != 'string') {
console.error("unsupported binary data on websocket; ignoring");
return
}
if (e.data.substr(0, 16) == "CLIENT DE SERVER") {
console.log("Server acknowledged WebSocket connection.");
return
}
try {
var json = JSON.parse(e.data);
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(){
map = new google.maps.Map($('.openwebrx-map')[0], {
center: {
lat: config.receiver_gps[0],
lng: config.receiver_gps[1]
},
zoom: 5
});
processUpdates(updateQueue);
$.getScript("/static/lib/nite-overlay.js").done(function(){
nite.init(map);
setInterval(function() { nite.refresh() }, 10000); // every 10s
});
});
retention_time = config.map_position_retention_time * 1000;
break;
case "update":
processUpdates(json.value);
break;
}
} catch (e) {
// don't lose exception
console.error(e);
}
};
ws.onclose = function(){
console.info("onclose");
var clearMap = function(){
var reset = function(callsign, item) { item.setMap(); };
$.each(markers, reset);
$.each(rectangles, reset);
markers = {};
rectangles = {};
};
window.onbeforeunload = function() { //http://stackoverflow.com/questions/4812686/closing-websocket-correctly-html5-javascript
ws.onclose = function () {};
ws.close();
};
ws.onerror = function(){
console.info("onerror");
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");
return
}
if (e.data.substr(0, 16) == "CLIENT DE SERVER") {
console.log("Server acknowledged WebSocket connection.");
return
}
try {
var json = JSON.parse(e.data);
switch (json.type) {
case "config":
var config = json.value;
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],
lng: config.receiver_gps[1]
},
zoom: 5
});
processUpdates(updateQueue);
updateQueue = [];
$.getScript("/static/lib/nite-overlay.js").done(function(){
nite.init(map);
setInterval(function() { nite.refresh() }, 10000); // every 10s
});
});
retention_time = config.map_position_retention_time * 1000;
break;
case "update":
processUpdates(json.value);
break;
}
} catch (e) {
// don't lose exception
console.error(e);
}
};
ws.onclose = function(){
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("websocket error");
};
*/
};
connect();
var infowindow;
var showInfoWindow = function(locator, pos) {
if (!infowindow) infowindow = new google.maps.InfoWindow();