Fix: Umstellung auf Socket.io für alle Views und Verbesserung der Event-Registrierung

Änderung:
- Alle Views wurden aktualisiert, um den Daemon WebSocket zu deaktivieren und stattdessen Socket.io für die Event-Registrierung zu verwenden.
- Eine neue Methode `setupSocketEvents` wurde hinzugefügt, um die Socket.io-Events zu registrieren und Protokollausgaben für den Status der Registrierung bereitzustellen.
- Die Logik zur Handhabung von WebSocket-Events wurde vereinfacht und verbessert, um die Stabilität und Nachvollziehbarkeit zu erhöhen.

Diese Anpassung sorgt für eine konsistentere Handhabung von WebSocket-Events und verbessert die Benutzererfahrung durch zuverlässigere Datenaktualisierungen.
This commit is contained in:
Torsten Schulz (local)
2025-09-08 12:06:56 +02:00
parent 975a1dd7ca
commit 917b04fb5e
10 changed files with 159 additions and 49 deletions

View File

@@ -81,7 +81,7 @@ class DaemonWebSocketBridge {
case 'familychanged': case 'familychanged':
// Broadcast an alle verbundenen Clients // Broadcast an alle verbundenen Clients
io.emit(message.event, message.data || {}); io.emit(message.event, message.data || {});
console.log(`📤 Event ${message.event} an alle Clients weitergeleitet`); console.log(`📤 Event ${message.event} an alle Clients weitergeleitet:`, message.data || {});
break; break;
case 'workerStatus': case 'workerStatus':

View File

@@ -55,20 +55,14 @@ export default {
...mapGetters(['menu']), ...mapGetters(['menu']),
}, },
async mounted() { async mounted() {
await this.fetchStatus(); await this.fetchStatus();
if (this.socket) { this.setupSocketEvents();
this.socket.on("falukantUpdateStatus", this.fetchStatus);
}
if (this.daemonSocket) {
this.daemonSocket.addEventListener("message", this.handleDaemonSocketMessage);
}
}, },
beforeUnmount() { beforeUnmount() {
if (this.socket) { if (this.socket) {
this.socket.off("falukantUpdateStatus", this.fetchStatus); this.socket.off("falukantUpdateStatus", this.fetchStatus);
} this.socket.off("stock_change", this.fetchStatus);
if (this.daemonSocket) { this.socket.off("familychanged", this.fetchStatus);
this.daemonSocket.removeEventListener("message", this.handleDaemonSocketMessage);
} }
}, },
methods: { methods: {
@@ -110,7 +104,26 @@ export default {
} }
}, },
// Daemon WebSocket deaktiviert - verwende Socket.io Events // Daemon WebSocket deaktiviert - verwende Socket.io Events
// Diese Methode wird nicht mehr verwendet setupSocketEvents() {
if (this.socket) {
this.socket.on('falukantUpdateStatus', (data) => {
console.log('📨 StatusBar: falukantUpdateStatus empfangen:', data);
this.fetchStatus();
});
this.socket.on('stock_change', (data) => {
console.log('📨 StatusBar: stock_change empfangen:', data);
this.fetchStatus();
});
this.socket.on('familychanged', (data) => {
console.log('📨 StatusBar: familychanged empfangen:', data);
this.fetchStatus();
});
console.log('✅ StatusBar: Socket.io Events registriert');
} else {
console.log('⚠️ StatusBar: Socket.io noch nicht verfügbar');
setTimeout(() => this.setupSocketEvents(), 1000);
}
},
openPage(url, hasSubmenu = false) { openPage(url, hasSubmenu = false) {
if (hasSubmenu) { if (hasSubmenu) {
return; return;

View File

@@ -118,16 +118,27 @@ export default {
}; };
}, },
computed: { computed: {
...mapState(['daemonSocket']) ...mapState(['socket'])
}, },
async mounted() { async mounted() {
await this.loadBankOverview(); await this.loadBankOverview();
if (this.daemonSocket) this.daemonSocket.addEventListener('message', this.handleDaemonMessage); this.setupSocketEvents();
}, },
beforeUnmount() { beforeUnmount() {
if (this.daemonSocket) this.daemonSocket.removeEventListener('message', this.handleDaemonMessage); if (this.socket) {
this.socket.off('falukantUpdateStatus', this.loadBankOverview);
}
}, },
methods: { methods: {
setupSocketEvents() {
if (this.socket) {
this.socket.on('falukantUpdateStatus', this.loadBankOverview);
console.log('✅ BankView: Socket.io Events registriert');
} else {
console.log('⚠️ BankView: Socket.io noch nicht verfügbar');
setTimeout(() => this.setupSocketEvents(), 1000);
}
},
async loadBankOverview() { async loadBankOverview() {
try { try {
const { data } = await apiClient.get('/api/falukant/bank/overview'); const { data } = await apiClient.get('/api/falukant/bank/overview');

View File

@@ -73,7 +73,7 @@ export default {
} }
// Daemon WebSocket deaktiviert - verwende Socket.io // Daemon WebSocket deaktiviert - verwende Socket.io
console.log('✅ BranchView: Socket.io Events werden verwendet'); this.setupSocketEvents();
// Live-Socket-Events // Live-Socket-Events
[ [
@@ -116,6 +116,24 @@ export default {
}, },
methods: { methods: {
setupSocketEvents() {
if (this.socket) {
this.socket.on('falukantBranchUpdate', (data) => {
console.log('📨 BranchView: falukantBranchUpdate empfangen:', data);
this.loadBranches();
this.loadProducts();
});
this.socket.on('falukantUpdateStatus', (data) => {
console.log('📨 BranchView: falukantUpdateStatus empfangen:', data);
this.loadBranches();
this.loadProducts();
});
console.log('✅ BranchView: Socket.io Events registriert');
} else {
console.log('⚠️ BranchView: Socket.io noch nicht verfügbar');
setTimeout(() => this.setupSocketEvents(), 1000);
}
},
async loadBranches() { async loadBranches() {
try { try {
const result = await apiClient.get('/api/falukant/branches'); const result = await apiClient.get('/api/falukant/branches');

View File

@@ -92,20 +92,27 @@ export default {
}; };
}, },
computed: { computed: {
...mapState(['daemonSocket']) ...mapState(['socket'])
}, },
async mounted() { async mounted() {
await this.loadDirectors(); await this.loadDirectors();
if (this.daemonSocket) { this.setupSocketEvents();
this.daemonSocket.addEventListener('message', this.handleDaemonMessage);
}
}, },
beforeUnmount() { beforeUnmount() {
if (this.daemonSocket) { if (this.socket) {
this.daemonSocket.removeEventListener('message', this.handleDaemonMessage); this.socket.off('falukantUpdateStatus', this.loadDirectors);
} }
}, },
methods: { methods: {
setupSocketEvents() {
if (this.socket) {
this.socket.on('falukantUpdateStatus', this.loadDirectors);
console.log('✅ DirectorView: Socket.io Events registriert');
} else {
console.log('⚠️ DirectorView: Socket.io noch nicht verfügbar');
setTimeout(() => this.setupSocketEvents(), 1000);
}
},
async loadDirectors() { async loadDirectors() {
try { try {
const { data } = await apiClient.get('/api/falukant/directors'); const { data } = await apiClient.get('/api/falukant/directors');

View File

@@ -194,18 +194,26 @@ export default {
} }
}, },
computed: { computed: {
...mapState(['socket', 'daemonSocket']) ...mapState(['socket'])
}, },
async mounted() { async mounted() {
await this.loadFamilyData(); await this.loadFamilyData();
await this.loadGifts(); await this.loadGifts();
await this.loadMoodAffects(); await this.loadMoodAffects();
await this.loadCharacterAffects(); await this.loadCharacterAffects();
if (this.daemonSocket) { this.setupSocketEvents();
this.daemonSocket.addEventListener('message', this.handleDaemonMessage);
}
}, },
methods: { methods: {
setupSocketEvents() {
if (this.socket) {
this.socket.on('falukantUpdateStatus', this.loadFamilyData);
this.socket.on('familychanged', this.loadFamilyData);
console.log('✅ FamilyView: Socket.io Events registriert');
} else {
console.log('⚠️ FamilyView: Socket.io noch nicht verfügbar');
setTimeout(() => this.setupSocketEvents(), 1000);
}
},
async loadFamilyData() { async loadFamilyData() {
try { try {
const response = await apiClient.get('/api/falukant/family'); const response = await apiClient.get('/api/falukant/family');

View File

@@ -67,7 +67,7 @@ export default {
}; };
}, },
computed: { computed: {
...mapState(['daemonSocket']), ...mapState(['socket']),
/** /**
* Selected measure object based on selectedTr * Selected measure object based on selectedTr
*/ */
@@ -87,16 +87,23 @@ export default {
}, },
async mounted() { async mounted() {
await this.loadHealthData(); await this.loadHealthData();
if (this.daemonSocket) { this.setupSocketEvents();
this.daemonSocket.addEventListener('message', this.handleDaemonMessage);
}
}, },
beforeUnmount() { beforeUnmount() {
if (this.daemonSocket) { if (this.socket) {
this.daemonSocket.removeEventListener('message', this.handleDaemonMessage); this.socket.off('falukantUpdateStatus', this.loadHealthData);
} }
}, },
methods: { methods: {
setupSocketEvents() {
if (this.socket) {
this.socket.on('falukantUpdateStatus', this.loadHealthData);
console.log('✅ HealthView: Socket.io Events registriert');
} else {
console.log('⚠️ HealthView: Socket.io noch nicht verfügbar');
setTimeout(() => this.setupSocketEvents(), 1000);
}
},
async loadHealthData() { async loadHealthData() {
try { try {
const { data } = await apiClient.get('/api/falukant/health'); const { data } = await apiClient.get('/api/falukant/health');

View File

@@ -88,7 +88,7 @@ export default {
}; };
}, },
computed: { computed: {
...mapState(['socket', 'daemonSocket']), ...mapState(['socket']),
allRenovated() { allRenovated() {
return Object.values(this.status).every(v => v >= 100); return Object.values(this.status).every(v => v >= 100);
} }
@@ -190,13 +190,25 @@ export default {
}, },
async mounted() { async mounted() {
await this.loadData(); await this.loadData();
if (this.socket) this.socket.on('falukantHouseUpdate', this.loadData); this.setupSocketEvents();
if (this.daemonSocket) this.daemonSocket.addEventListener('message', this.handleDaemonMessage);
}, },
beforeUnmount() { beforeUnmount() {
if (this.socket) this.socket.off('falukantHouseUpdate', this.loadData); if (this.socket) {
if (this.daemonSocket) this.daemonSocket.removeEventListener('message', this.handleDaemonMessage); this.socket.off('falukantHouseUpdate', this.loadData);
} this.socket.off('falukantUpdateStatus', this.loadData);
}
},
methods: {
setupSocketEvents() {
if (this.socket) {
this.socket.on('falukantHouseUpdate', this.loadData);
this.socket.on('falukantUpdateStatus', this.loadData);
console.log('✅ HouseView: Socket.io Events registriert');
} else {
console.log('⚠️ HouseView: Socket.io noch nicht verfügbar');
setTimeout(() => this.setupSocketEvents(), 1000);
}
}
}; };
</script> </script>

View File

@@ -62,7 +62,7 @@
}; };
}, },
computed: { computed: {
...mapState(['daemonSocket', 'falukantData']), ...mapState(['socket', 'falukantData']),
gender() { gender() {
return this.current.charactersWithNobleTitle[0]?.gender || 'male'; return this.current.charactersWithNobleTitle[0]?.gender || 'male';
}, },
@@ -72,12 +72,23 @@
}, },
async mounted() { async mounted() {
await this.loadNobility(); await this.loadNobility();
if (this.daemonSocket) this.daemonSocket.addEventListener('message', this.handleDaemonMessage); this.setupSocketEvents();
}, },
beforeUnmount() { beforeUnmount() {
if (this.daemonSocket) this.daemonSocket.removeEventListener('message', this.handleDaemonMessage); if (this.socket) {
this.socket.off('falukantUpdateStatus', this.loadNobility);
}
}, },
methods: { methods: {
setupSocketEvents() {
if (this.socket) {
this.socket.on('falukantUpdateStatus', this.loadNobility);
console.log('✅ NobilityView: Socket.io Events registriert');
} else {
console.log('⚠️ NobilityView: Socket.io noch nicht verfügbar');
setTimeout(() => this.setupSocketEvents(), 1000);
}
},
async loadNobility() { async loadNobility() {
try { try {
const { data } = await apiClient.get('/api/falukant/nobility'); const { data } = await apiClient.get('/api/falukant/nobility');

View File

@@ -152,7 +152,7 @@ export default {
}; };
}, },
computed: { computed: {
...mapState(['daemonSocket']), ...mapState(['socket']),
getAvatarStyle() { getAvatarStyle() {
if (!this.falukantUser) return {}; if (!this.falukantUser) return {};
const { gender, age } = this.falukantUser.character; const { gender, age } = this.falukantUser.character;
@@ -203,18 +203,20 @@ export default {
return window.navigator.language || 'en-US'; return window.navigator.language || 'en-US';
}, },
}, },
watch: {
socket(newSocket) {
if (newSocket) {
console.log('🔌 Socket.io verfügbar - registriere Events');
this.setupSocketEvents();
}
}
},
async mounted() { async mounted() {
await this.fetchFalukantUser(); await this.fetchFalukantUser();
await this.fetchAllStock(); await this.fetchAllStock();
await this.fetchProductions(); await this.fetchProductions();
// Daemon WebSocket deaktiviert - verwende Socket.io für alle Events // Daemon WebSocket deaktiviert - verwende Socket.io für alle Events
if (this.socket) { this.setupSocketEvents();
this.socket.on("falukantUserUpdated", this.fetchFalukantUser);
this.socket.on("production_ready", this.handleProductionReadyEvent);
console.log('✅ FalukantOverviewView: Socket.io Events registriert');
} else {
console.log('⚠️ FalukantOverviewView: Kein Socket.io verfügbar');
}
}, },
beforeUnmount() { beforeUnmount() {
if (this.socket) { if (this.socket) {
@@ -224,6 +226,27 @@ export default {
// Daemon WebSocket deaktiviert - keine Cleanup nötig // Daemon WebSocket deaktiviert - keine Cleanup nötig
}, },
methods: { methods: {
setupSocketEvents() {
if (this.socket) {
this.socket.on("falukantUserUpdated", this.fetchFalukantUser);
this.socket.on("production_ready", this.handleProductionReadyEvent);
this.socket.on("falukantUpdateStatus", (data) => {
console.log('📨 FalukantOverviewView: falukantUpdateStatus empfangen:', data);
this.fetchFalukantUser();
});
this.socket.on("falukantBranchUpdate", (data) => {
console.log('📨 FalukantOverviewView: falukantBranchUpdate empfangen:', data);
this.fetchFalukantUser();
});
console.log('✅ FalukantOverviewView: Socket.io Events registriert');
} else {
console.log('⚠️ FalukantOverviewView: Socket.io noch nicht verfügbar, versuche es später...');
// Versuche es nach kurzer Verzögerung erneut
setTimeout(() => {
this.setupSocketEvents();
}, 1000);
}
},
getAgeGroup(age) { getAgeGroup(age) {
if (age <= 1) return "0-1"; if (age <= 1) return "0-1";
if (age <= 3) return "2-3"; if (age <= 3) return "2-3";