fix(socket): improve connection stability with heartbeat and auto-reconnect
- Backend: Add pingInterval (25s) and pingTimeout (60s) for better keep-alive - Frontend: Increase reconnection attempts to Infinity (keep trying forever) - Frontend: Add reconnect event handlers to rejoin rooms after reconnection - Frontend: Check initial connection state when reusing socket instance
This commit is contained in:
@@ -64,8 +64,18 @@ const useMatchChat = (match, userId, slug) => {
|
||||
// Socket event listeners
|
||||
socket.on('connect', joinMatchRoom);
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
socket.on('disconnect', (reason) => {
|
||||
setIsConnected(false);
|
||||
console.log('🔌 Match chat disconnected:', reason);
|
||||
});
|
||||
|
||||
// Handle reconnection
|
||||
socket.on('reconnect', (attemptNumber) => {
|
||||
console.log('🔄 Match chat reconnected after', attemptNumber, 'attempts');
|
||||
});
|
||||
|
||||
socket.on('reconnect_attempt', (attemptNumber) => {
|
||||
console.log('🔄 Match chat reconnection attempt', attemptNumber);
|
||||
});
|
||||
|
||||
// Receive messages
|
||||
@@ -82,6 +92,8 @@ const useMatchChat = (match, userId, slug) => {
|
||||
return () => {
|
||||
socket.off('connect', joinMatchRoom);
|
||||
socket.off('disconnect');
|
||||
socket.off('reconnect');
|
||||
socket.off('reconnect_attempt');
|
||||
socket.off('match_message');
|
||||
};
|
||||
}, [match, userId]);
|
||||
|
||||
Reference in New Issue
Block a user