Fix missing headers in SSE queries

This commit is contained in:
Ian Renton
2026-08-14 23:47:22 +01:00
parent efec8e220e
commit ef2c23c8b6
16 changed files with 358 additions and 129 deletions
+30 -25
View File
@@ -1,6 +1,5 @@
// SSE connection for live spot updates
let evtSource;
let restartSSEOnErrorTimeoutId;
let sseAbortController = null;
// Debounce timer so rapid SSE bursts only trigger one updateBands() call, as these could trigger a flash of the user
// visible content
let updateBandsDebounceId;
@@ -17,10 +16,10 @@ BAND_COLUMN_SPOT_DIV_HEIGHT_PX = BAND_COLUMN_FONT_SIZE * 1.6;
// Load spots and populate the bands display.
function loadSpots() {
// Close any existing SSE connection before fetching fresh data
if (evtSource != null) {
evtSource.close();
if (sseAbortController != null) {
sseAbortController.abort();
}
$.ajax({url: '/api/v2/spots' + buildQueryString(), dataType: 'json', headers: getCredentialHeaders(), success: function (jsonData) {
$.ajax({url: '/api/v2/spots' + buildQueryString(), dataType: 'json', success: function (jsonData) {
// Store data
spots = jsonData;
// Update bands display
@@ -33,30 +32,36 @@ function loadSpots() {
// Start an SSE connection to receive new spots as they arrive.
function startSSEConnection() {
if (evtSource != null) {
evtSource.close();
if (sseAbortController != null) {
sseAbortController.abort();
}
evtSource = new EventSource('/api/v2/spots/stream' + buildQueryString());
sseAbortController = new AbortController();
evtSource.onmessage = function (event) {
const newSpot = JSON.parse(event.data);
// No need to include QRZ/HamQTH credentials because the information wouldn't be displayed on the bands panel anyway
fetchEventSource('/api/v2/spots/stream' + buildQueryString(), {
signal: sseAbortController.signal,
openWhenHidden: true,
// Replace any existing spot for this callsign
spots = spots.filter(s => newSpot["dx_call"] !== s["dx_call"]);
spots.unshift(newSpot);
onmessage(event) {
if (!event.data) {
return; // heartbeat/keep-alive, nothing to do
}
// Debounce. Wait 500ms after the last message before re-rendering to avoid too many ugly flashes
clearTimeout(updateBandsDebounceId);
updateBandsDebounceId = setTimeout(updateBands, 5000);
};
const newSpot = JSON.parse(event.data);
evtSource.onerror = function () {
if (evtSource != null) {
evtSource.close();
// Replace any existing spot for this callsign
spots = spots.filter(s => newSpot["dx_call"] !== s["dx_call"]);
spots.unshift(newSpot);
// Debounce. Wait 500ms after the last message before re-rendering to avoid too many ugly flashes
clearTimeout(updateBandsDebounceId);
updateBandsDebounceId = setTimeout(updateBands, 5000);
},
onerror(err) {
console.error('SSE error:', err);
return 1000;
}
clearTimeout(restartSSEOnErrorTimeoutId);
restartSSEOnErrorTimeoutId = setTimeout(startSSEConnection, 1000);
};
});
}
// Remove spots from the display that are older than the selected max age.
@@ -319,8 +324,8 @@ function displayUpdated() {
$(document).ready(function () {
// Close SSE connection cleanly when navigating away
window.addEventListener('beforeunload', function () {
if (evtSource != null) {
evtSource.close();
if (sseAbortController != null) {
sseAbortController.abort();
}
});