fix: PWA icon, robust socket.io reconnect, faster real-time sync

This commit is contained in:
Pepe Ziberi
2026-02-21 23:37:36 +01:00
parent e3f8f14f6a
commit 2432e9a17f
5 changed files with 53 additions and 12 deletions

View File

@@ -3,23 +3,50 @@
import { io, Socket } from 'socket.io-client'
let socket: Socket | null = null
let currentRoom: string | null = null
export function getSocket(): Socket {
if (!socket) {
socket = io({
path: '/socket.io',
transports: ['polling', 'websocket'],
transports: ['websocket', 'polling'],
upgrade: true,
reconnectionAttempts: 10,
reconnectionDelay: 2000,
reconnection: true,
reconnectionAttempts: Infinity,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
timeout: 10000,
forceNew: false,
})
socket.on('connect', () => {
console.log('[Socket.io] Connected:', socket?.id)
// Re-join project room after reconnect
if (currentRoom) {
console.log('[Socket.io] Re-joining room:', currentRoom)
socket?.emit('join-project', currentRoom)
}
})
socket.on('disconnect', (reason) => {
console.warn('[Socket.io] Disconnected:', reason)
if (reason === 'io server disconnect') {
// Server disconnected us, need to manually reconnect
socket?.connect()
}
})
socket.on('connect_error', (err) => {
console.warn('[Socket.io] Connection error:', err.message)
})
socket.io.on('reconnect', (attempt) => {
console.log('[Socket.io] Reconnected after', attempt, 'attempts')
})
socket.io.on('reconnect_attempt', (attempt) => {
console.log('[Socket.io] Reconnect attempt', attempt)
})
}
return socket
}
/** Track which room the socket should be in (for auto-rejoin on reconnect) */
export function setSocketRoom(projectId: string | null): void {
currentRoom = projectId
}