fix: PWA icon, robust socket.io reconnect, faster real-time sync
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user