diff options
| author | ivanshen <iwshen11@gmail.com> | 2019-03-05 20:58:03 +0000 |
|---|---|---|
| committer | ivanshen <iwshen11@gmail.com> | 2019-03-05 20:58:03 +0000 |
| commit | 0a573cc3e918e751f5a9e02f5877f7786636539d (patch) | |
| tree | e4726c734926e0c2dc4fcf1af3c94367476bb458 /web/public/javascripts | |
| parent | 786d0aacd27f27875568c7a89d9e1df918fadbf3 (diff) | |
Added live update functionality (feat 8)
Diffstat (limited to 'web/public/javascripts')
| -rw-r--r-- | web/public/javascripts/map.js | 32 |
1 files changed, 31 insertions, 1 deletions
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 "<p>" + JSON.stringify(layer.feature.geometry) + "</p>"; }).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 "<p>" + JSON.stringify(layer.feature.geometry) + "</p>"; + }).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 |
