Fix: Improve daemon connection handling and retry logic

- Reset daemon connection state on successful connection and errors
- Clear retry timer when connection is established
- Enhanced retry logic to prevent multiple simultaneous connection attempts
- Improved logging for daemon reconnection attempts
This commit is contained in:
Torsten Schulz (local)
2025-10-31 16:24:35 +01:00
parent 44a2c525e7
commit 762a2e9cf0

View File

@@ -292,8 +292,12 @@ const store = createStore({
daemonSocket.onopen = () => { daemonSocket.onopen = () => {
opened = true; opened = true;
state.daemonRetryCount = 0; // Reset retry counter on successful connection state.daemonRetryCount = 0;
state.daemonConnecting = false; state.daemonConnecting = false;
if (state.daemonRetryTimer) {
clearTimeout(state.daemonRetryTimer);
state.daemonRetryTimer = null;
}
commit('setDaemonConnectionStatus', 'connected'); commit('setDaemonConnectionStatus', 'connected');
const payload = JSON.stringify({ const payload = JSON.stringify({
event: 'setUserId', event: 'setUserId',
@@ -303,8 +307,8 @@ const store = createStore({
}; };
daemonSocket.onclose = (event) => { daemonSocket.onclose = (event) => {
state.daemonConnecting = false;
commit('setDaemonConnectionStatus', 'disconnected'); commit('setDaemonConnectionStatus', 'disconnected');
// Falls Verbindungsaufbau nicht offen war und es noch einen Fallback gibt → nächsten Versuch ohne Subprotokoll
if (!opened && attemptIndex < protocols.length - 1) { if (!opened && attemptIndex < protocols.length - 1) {
attemptIndex += 1; attemptIndex += 1;
tryConnectWithProtocol(); tryConnectWithProtocol();
@@ -314,8 +318,8 @@ const store = createStore({
}; };
daemonSocket.onerror = (error) => { daemonSocket.onerror = (error) => {
state.daemonConnecting = false;
commit('setDaemonConnectionStatus', 'error'); commit('setDaemonConnectionStatus', 'error');
// Bei Fehler vor Open: Fallback versuchen
if (!opened && attemptIndex < protocols.length - 1) { if (!opened && attemptIndex < protocols.length - 1) {
attemptIndex += 1; attemptIndex += 1;
tryConnectWithProtocol(); tryConnectWithProtocol();
@@ -340,7 +344,7 @@ const store = createStore({
commit('setDaemonSocket', daemonSocket); commit('setDaemonSocket', daemonSocket);
} catch (error) { } catch (error) {
// Beim Konstruktionsfehler ebenfalls Fallback versuchen state.daemonConnecting = false;
if (attemptIndex < protocols.length - 1) { if (attemptIndex < protocols.length - 1) {
attemptIndex += 1; attemptIndex += 1;
tryConnectWithProtocol(); tryConnectWithProtocol();
@@ -355,34 +359,20 @@ const store = createStore({
connectDaemonSocket(); connectDaemonSocket();
}, },
retryDaemonConnection({ commit, state }) { retryDaemonConnection({ commit, state, dispatch }) {
if (state.daemonRetryTimer || state.daemonConnecting) { if (state.daemonRetryTimer) {
return; // Already retrying or connecting
}
const maxRetries = 15;
console.log(`Daemon-Reconnect-Versuch ${state.daemonRetryCount + 1}/${maxRetries}`);
if (state.daemonRetryCount >= maxRetries) {
// Nach maxRetries alle 5 Sekunden weiter versuchen
console.log('Daemon: Max Retries erreicht, versuche weiter alle 5 Sekunden...');
state.daemonRetryTimer = setTimeout(() => {
state.daemonRetryCount = 0; // Reset für nächsten Zyklus
state.daemonRetryTimer = null;
commit('setDaemonConnectionStatus', 'connecting');
// Recursive call to retry
setTimeout(() => this.dispatch('retryDaemonConnection'), 100);
}, 5000);
return; return;
} }
state.daemonConnecting = false;
state.daemonRetryCount++; state.daemonRetryCount++;
const delay = 5000; // Alle 5 Sekunden versuchen const delay = 5000;
console.log(`Daemon: Warte ${delay}ms bis zum nächsten Reconnect-Versuch...`); console.log(`Daemon: Reconnect-Versuch ${state.daemonRetryCount}, nächster Versuch in ${delay}ms...`);
state.daemonRetryTimer = setTimeout(() => { state.daemonRetryTimer = setTimeout(() => {
state.daemonRetryTimer = null; state.daemonRetryTimer = null;
this.dispatch('initializeDaemonSocket'); commit('setDaemonConnectionStatus', 'connecting');
dispatch('initializeDaemonSocket');
}, delay); }, delay);
}, },
setLanguage({ commit }, language) { setLanguage({ commit }, language) {