Verbesserung: Erweiterung der Protokollausgaben in socket.js zur Nachverfolgbarkeit der Socket-Interaktionen
Änderungen: - Protokollausgaben wurden hinzugefügt, um Verbindungsherstellung, Benutzer-ID-Setzung und Trennung von Sockets zu dokumentieren. - Zusätzliche Warnungen wurden implementiert, um fehlende Sockets für Benutzer zu kennzeichnen. Diese Anpassungen erhöhen die Nachvollziehbarkeit und Robustheit der Socket.io-Integration in der Anwendung.
This commit is contained in:
@@ -14,15 +14,19 @@ export function setupWebSocket(server) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', (socket) => {
|
||||||
|
console.log('[socket.io] connection established:', socket.id, 'from', socket.handshake.address);
|
||||||
|
|
||||||
socket.on('setUserId', (userId) => {
|
socket.on('setUserId', (userId) => {
|
||||||
if (userId) {
|
if (userId) {
|
||||||
socket.userId = userId;
|
socket.userId = userId;
|
||||||
userSockets[userId] = socket.id;
|
userSockets[userId] = socket.id;
|
||||||
|
console.log('[socket.io] setUserId received:', userId, '→ socket', socket.id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
socket.on('disconnect', () => {
|
socket.on('disconnect', () => {
|
||||||
if (socket.userId) {
|
if (socket.userId) {
|
||||||
delete userSockets[socket.userId];
|
delete userSockets[socket.userId];
|
||||||
|
console.log('[socket.io] disconnected:', socket.id, 'for userId', socket.userId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -47,9 +51,12 @@ export async function notifyUser(recipientHashedUserId, event, data) {
|
|||||||
if (recipientUser) {
|
if (recipientUser) {
|
||||||
const socketId = userSockets[recipientUser.hashedId];
|
const socketId = userSockets[recipientUser.hashedId];
|
||||||
if (socketId) {
|
if (socketId) {
|
||||||
|
console.log('[socket.io] notifyUser → emit:', event, 'to socket', socketId, 'user', recipientUser.hashedId, 'data:', JSON.stringify(data));
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
io.to(socketId).emit(event, data);
|
io.to(socketId).emit(event, data);
|
||||||
}, 250);
|
}, 250);
|
||||||
|
} else {
|
||||||
|
console.warn('[socket.io] notifyUser: no socket registered for user', recipientUser.hashedId);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`Benutzer mit gehashter ID ${recipientHashedUserId} nicht gefunden.`);
|
console.log(`Benutzer mit gehashter ID ${recipientHashedUserId} nicht gefunden.`);
|
||||||
@@ -57,7 +64,7 @@ export async function notifyUser(recipientHashedUserId, event, data) {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Fehler beim Senden der Benachrichtigung:', err);
|
console.error('Fehler beim Senden der Benachrichtigung:', err);
|
||||||
}
|
}
|
||||||
console.log('done sending socket');
|
console.log('[socket.io] notifyUser done');
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function notifyAllUsers(event, data) {
|
export async function notifyAllUsers(event, data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user