From 0a573cc3e918e751f5a9e02f5877f7786636539d Mon Sep 17 00:00:00 2001 From: ivanshen Date: Tue, 5 Mar 2019 15:58:03 -0500 Subject: Added live update functionality (feat 8) --- web/public/javascripts/map.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'web/public') diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 190307d..5739dec 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -11,7 +11,6 @@ L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r} maxZoom: 18, ext: 'png' }).addTo(map); - L.geoJson(points, { pointToLayer: function (feature, latlng) { //return L.circleMarker(latlng); @@ -21,3 +20,34 @@ L.geoJson(points, { // layer.feature.geometry gives you access to all the fields return "

" + JSON.stringify(layer.feature.geometry) + "

"; }).addTo(map) +if (!!window.EventSource) { + var source = new EventSource('/stream'); + + source.addEventListener('message', function(e) { + var data = JSON.parse(e.data); + if (data.coordinates) { + points.push(data); + L.geoJson(points, { + pointToLayer: function (feature, latlng) { + //return L.circleMarker(latlng); + return L.marker(latlng); + } + }).bindPopup(function (layer) { + // layer.feature.geometry gives you access to all the fields + return "

" + JSON.stringify(layer.feature.geometry) + "

"; + }).addTo(map) + } + }, false) + + source.addEventListener('open', function(e) { + console.log("Connection was opened") + }, false) + + source.addEventListener('error', function(e) { + if (e.readyState == EventSource.CLOSED) { + console.log("Connection was closed") + } + }, false) +} else { + console.log("sse not supported."); +} \ No newline at end of file -- cgit v1.2.3