Update max socket reconnection to 15s, add socket reconnection logs

This commit is contained in:
advplyr 2025-04-14 16:17:09 -05:00
parent 1e791f9601
commit 46d5e1d96c

View file

@ -10,6 +10,8 @@ class ServerSocket extends EventEmitter {
this.connected = false
this.serverAddress = null
this.token = null
this.lastReconnectAttemptTime = 0
}
$on(evt, callback) {
@ -35,8 +37,8 @@ class ServerSocket extends EventEmitter {
const socketOptions = {
transports: ['websocket'],
upgrade: false,
path: `${serverPath}/socket.io`
// reconnectionAttempts: 3
path: `${serverPath}/socket.io`,
reconnectionDelayMax: 15000
}
this.socket = io(serverHost, socketOptions)
this.setSocketListeners()
@ -54,6 +56,9 @@ class ServerSocket extends EventEmitter {
this.socket.on('user_updated', this.onUserUpdated.bind(this))
this.socket.on('user_item_progress_updated', this.onUserItemProgressUpdated.bind(this))
this.socket.on('playlist_added', this.onPlaylistAdded.bind(this))
this.socket.io.on('reconnect_attempt', this.onReconnectAttempt.bind(this))
this.socket.io.on('reconnect_error', this.onReconnectError.bind(this))
this.socket.io.on('reconnect_failed', this.onReconnectFailed.bind(this))
}
removeListeners() {
@ -72,6 +77,20 @@ class ServerSocket extends EventEmitter {
this.socket.emit('auth', this.token) // Required to connect a user with their socket
}
onReconnectAttempt(attemptNumber) {
const timeSinceLastReconnectAttempt = this.lastReconnectAttemptTime ? Date.now() - this.lastReconnectAttemptTime : 0
this.lastReconnectAttemptTime = Date.now()
console.log(`[SOCKET] Reconnect attempt ${attemptNumber} ${timeSinceLastReconnectAttempt > 0 ? `after ${timeSinceLastReconnectAttempt}ms` : ''}`)
}
onReconnectError(error) {
console.log('[SOCKET] Reconnect error', error)
}
onReconnectFailed(error) {
console.log('[SOCKET] Reconnect failed', error)
}
onDisconnect(reason) {
console.log('[SOCKET] Socket Disconnected: ' + reason)
this.connected = false