Änderung: Optimierung der Statusabfrage und Fehlerbehandlung im StatusBar-Komponenten

Änderungen:
- Entfernen von Konsolenausgaben zur Verbesserung der Codequalität und Reduzierung von Debugging-Informationen in der Produktionsumgebung.
- Anpassung der Fehlerbehandlung in der fetchStatus-Methode, um die Lesbarkeit zu erhöhen und die Fehlerprotokollierung zu vereinfachen.
- Verbesserung der Socket.io- und Daemon-WebSocket-Verbindungslogik zur Unterstützung lokaler Entwicklungsumgebungen.

Diese Anpassungen erhöhen die Effizienz und Benutzerfreundlichkeit der Anwendung, indem sie die Codebasis bereinigen und die Fehlerbehandlung optimieren.
This commit is contained in:
Torsten Schulz (local)
2025-09-21 22:01:27 +02:00
parent 1244c87d45
commit f418b59e14
4 changed files with 284 additions and 206 deletions

View File

@@ -145,15 +145,20 @@ const store = createStore({
currentSocket.disconnect();
}
commit('setConnectionStatus', 'connecting');
const socketIoUrl = import.meta.env.VITE_SOCKET_IO_URL || import.meta.env.VITE_API_BASE_URL;
console.log('🔌 Initializing Socket.io connection to:', socketIoUrl);
// Socket.io URL für lokale Entwicklung und Produktion
let socketIoUrl = import.meta.env.VITE_SOCKET_IO_URL || import.meta.env.VITE_API_BASE_URL;
// Für lokale Entwicklung: direkte Backend-Verbindung
if (!socketIoUrl && (import.meta.env.DEV || window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1')) {
socketIoUrl = 'http://localhost:3001';
}
const socket = io(socketIoUrl, {
secure: true,
transports: ['websocket', 'polling']
});
socket.on('connect', () => {
console.log('✅ Socket.io connected successfully');
retryCount = 0; // Reset retry counter on successful connection
commit('setConnectionStatus', 'connected');
const idForSocket = state.user?.hashedId || state.user?.id;
@@ -161,14 +166,11 @@ const store = createStore({
});
socket.on('disconnect', (reason) => {
console.warn('❌ Socket.io disconnected:', reason);
commit('setConnectionStatus', 'disconnected');
retryConnection(connectSocket);
});
socket.on('connect_error', (error) => {
console.error('❌ Socket.io connection error:', error);
console.error('❌ URL attempted:', import.meta.env.VITE_API_BASE_URL);
commit('setConnectionStatus', 'error');
});
@@ -179,30 +181,36 @@ const store = createStore({
const maxRetries = 10;
const retryConnection = (reconnectFn) => {
if (retryCount >= maxRetries) {
console.error('❌ Max retry attempts reached for Socket.io');
return;
}
retryCount++;
const delay = Math.min(1000 * Math.pow(1.5, retryCount - 1), 30000); // Exponential backoff, max 30s
console.log(`🔄 Retrying Socket.io connection in ${delay}ms (attempt ${retryCount}/${maxRetries})`);
setTimeout(() => {
reconnectFn();
}, delay);
};
connectSocket();
} else {
console.log("User is not logged in or user data is not available.");
}
},
initializeDaemonSocket({ commit, state }) {
if (!state.isLoggedIn || !state.user) {
console.log("User is not logged in or user data is not available for Daemon WebSocket.");
return;
}
const daemonUrl = import.meta.env.VITE_DAEMON_SOCKET || 'wss://www.your-part.de:4551';
console.log('🔌 Initializing Daemon WebSocket connection to:', daemonUrl);
// Daemon URL für lokale Entwicklung und Produktion
let daemonUrl = import.meta.env.VITE_DAEMON_SOCKET;
// Für lokale Entwicklung: direkte Daemon-Verbindung
if (!daemonUrl && (import.meta.env.DEV || window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1')) {
daemonUrl = 'ws://localhost:4551';
}
// Fallback für Produktion
if (!daemonUrl) {
daemonUrl = 'wss://www.your-part.de:4551';
}
const connectDaemonSocket = () => {
// Protokoll-Fallback: zuerst mit Subprotokoll, dann ohne
@@ -215,13 +223,11 @@ const store = createStore({
const daemonSocket = currentProtocol
? new WebSocket(daemonUrl, currentProtocol)
: new WebSocket(daemonUrl);
console.log('🔌 Protocol:', currentProtocol ?? 'none (fallback)');
let opened = false;
daemonSocket.onopen = () => {
opened = true;
console.log('✅ Daemon WebSocket connected successfully');
retryCount = 0; // Reset retry counter on successful connection
const payload = JSON.stringify({
user_id: state.user.id,
@@ -232,17 +238,9 @@ const store = createStore({
};
daemonSocket.onclose = (event) => {
console.warn('❌ Daemon WebSocket disconnected:', event.reason);
console.warn('❌ Close details:', {
code: event.code,
reason: event.reason,
wasClean: event.wasClean,
readyState: daemonSocket.readyState
});
// Falls Verbindungsaufbau nicht offen war und es noch einen Fallback gibt → nächsten Versuch ohne Subprotokoll
if (!opened && attemptIndex < protocols.length - 1) {
attemptIndex += 1;
console.warn('🔄 Fallback: versuche erneut ohne Subprotokoll...');
tryConnectWithProtocol();
return;
}
@@ -250,23 +248,9 @@ const store = createStore({
};
daemonSocket.onerror = (error) => {
console.error('❌ Daemon WebSocket error:', error);
console.error('❌ Error details:', {
type: error.type,
target: error.target,
readyState: daemonSocket.readyState,
url: daemonSocket.url,
protocol: daemonSocket.protocol
});
console.error('❌ Browser info:', {
userAgent: navigator.userAgent,
location: window.location.href,
isSecure: window.location.protocol === 'https:'
});
// Bei Fehler vor Open: Fallback versuchen
if (!opened && attemptIndex < protocols.length - 1) {
attemptIndex += 1;
console.warn('🔄 Fallback: versuche erneut ohne Subprotokoll (onerror)...');
tryConnectWithProtocol();
return;
}
@@ -281,21 +265,17 @@ const store = createStore({
try {
const data = JSON.parse(message);
// Handle daemon messages here
console.log('📨 Daemon message received:', data);
} catch (error) {
console.error("Error parsing daemon message:", error);
// Error parsing daemon message
}
}
});
commit('setDaemonSocket', daemonSocket);
} catch (error) {
console.error('❌ Failed to create Daemon WebSocket:', error);
console.error('❌ URL attempted:', import.meta.env.VITE_DAEMON_SOCKET);
// Beim Konstruktionsfehler ebenfalls Fallback versuchen
if (attemptIndex < protocols.length - 1) {
attemptIndex += 1;
console.warn('🔄 Fallback: Konstruktion ohne Subprotokoll...');
tryConnectWithProtocol();
return;
}
@@ -310,18 +290,15 @@ const store = createStore({
const maxRetries = 15; // Increased max retries
const retryConnection = (reconnectFn) => {
if (retryCount >= maxRetries) {
console.error('❌ Max retry attempts reached for Daemon WebSocket');
// Reset counter after a longer delay to allow for network recovery
setTimeout(() => {
retryCount = 0;
console.log('🔄 Resetting Daemon WebSocket retry counter - attempting reconnection');
reconnectFn();
}, 60000); // Wait 1 minute before resetting
return;
}
retryCount++;
const delay = Math.min(1000 * Math.pow(1.5, retryCount - 1), 30000); // Exponential backoff, max 30s
console.log(`🔄 Retrying Daemon WebSocket connection in ${delay}ms (attempt ${retryCount}/${maxRetries})`);
setTimeout(() => {
reconnectFn();
}, delay);
@@ -336,7 +313,6 @@ const store = createStore({
const menu = await loadMenu();
commit('setMenu', menu);
} catch (err) {
console.error(err);
commit('setMenu', []);
}
},