Skip to content

Commit

Permalink
Redirect to index when there is no key returned
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong1120 committed Nov 22, 2024
1 parent 43f44ab commit 572325d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions examples/streaming_web/frontend/public/js/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ document.addEventListener('DOMContentLoaded', () => {
// If the label is missing from the URL, log an error and terminate further execution
if (!label) {
console.error('Label parameter is missing in the URL');
window.location.href = 'index.html'; // Redirect to index.html
return;
}

Expand Down Expand Up @@ -56,10 +57,15 @@ function initializeWebSocket(label) {
};

// Handle WebSocket errors
socket.onerror = (error) => console.error('WebSocket error:', error);
socket.onerror = (error) => {
console.error('WebSocket error:', error);
window.location.href = 'index.html'; // Redirect to index.html on error
};

// Handle WebSocket closure
socket.onclose = () => console.log('WebSocket closed');
socket.onclose = () => {
console.log('WebSocket closed');
};
}

/**
Expand All @@ -69,11 +75,12 @@ function initializeWebSocket(label) {
* @param {string} currentLabel - The label currently being displayed.
*/
function handleUpdate(data, currentLabel) {
if (data.label === currentLabel) {
if (data.label === currentLabel && data.images.length > 0) {
console.log('Received update for current label:', data.label);
updateCameraGrid(data.images); // Update the camera grid with new images
} else {
console.log('Received update for different label:', data.label);
console.log('No data for the current label, redirecting to index.html');
window.location.href = 'index.html'; // Redirect to index.html if no images
}
}

Expand All @@ -84,6 +91,7 @@ function handleUpdate(data, currentLabel) {
*/
function updateCameraGrid(images) {
const cameraGrid = document.getElementById('camera-grid'); // Reference to the camera grid container
cameraGrid.innerHTML = ''; // Clear existing content in the grid
images.forEach(({ key, image }) => {
// Check if a camera div for the given key already exists
const existingCameraDiv = document.querySelector(`.camera[data-key="${key}"]`);
Expand Down

0 comments on commit 572325d

Please sign in to comment.