fix(submissions): better reconnect for ws
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { ref, computed, onBeforeUnmount } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import { useWebSocket } from '@vueuse/core'
|
import { useWebSocket } from '@vueuse/core'
|
||||||
|
|
||||||
interface SubmissionUpdate {
|
interface SubmissionUpdate {
|
||||||
@@ -37,9 +37,6 @@ type WebSocketData = SubmissionUpdate | SubmissionFileUpdate | ImageCropUpdate
|
|||||||
|
|
||||||
export function useSubmissionsWebSocket(userSubmissions: any) {
|
export function useSubmissionsWebSocket(userSubmissions: any) {
|
||||||
const isConnected = ref(false)
|
const isConnected = ref(false)
|
||||||
const reconnectAttempts = ref(0)
|
|
||||||
const maxReconnectAttempts = 10
|
|
||||||
const reconnectDelay = ref(3000)
|
|
||||||
|
|
||||||
const wsUrl = computed(() => {
|
const wsUrl = computed(() => {
|
||||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
|
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||||
@@ -53,14 +50,11 @@ export function useSubmissionsWebSocket(userSubmissions: any) {
|
|||||||
} = useWebSocket(wsUrl.value, {
|
} = useWebSocket(wsUrl.value, {
|
||||||
onConnected: () => {
|
onConnected: () => {
|
||||||
isConnected.value = true
|
isConnected.value = true
|
||||||
reconnectAttempts.value = 0
|
|
||||||
reconnectDelay.value = 3000
|
|
||||||
console.log('✓ Connected to submissions WebSocket')
|
console.log('✓ Connected to submissions WebSocket')
|
||||||
},
|
},
|
||||||
onDisconnected: () => {
|
onDisconnected: () => {
|
||||||
isConnected.value = false
|
isConnected.value = false
|
||||||
console.log('✗ Disconnected from submissions WebSocket, attempting to reconnect...')
|
console.log('✗ Disconnected from submissions WebSocket')
|
||||||
attemptReconnect()
|
|
||||||
},
|
},
|
||||||
onError: (event) => {
|
onError: (event) => {
|
||||||
console.error('WebSocket error:', event)
|
console.error('WebSocket error:', event)
|
||||||
@@ -82,24 +76,16 @@ export function useSubmissionsWebSocket(userSubmissions: any) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
immediate: true,
|
immediate: true,
|
||||||
|
// vueuse's own reconnect stops once close() is called (it also closes
|
||||||
|
// on unmount), unlike a hand-rolled setTimeout(open) that would reopen
|
||||||
|
// the socket after leaving the page.
|
||||||
|
autoReconnect: {
|
||||||
|
retries: 10,
|
||||||
|
delay: 3000,
|
||||||
|
onFailed: () => console.error('Max reconnection attempts reached'),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const attemptReconnect = () => {
|
|
||||||
if (reconnectAttempts.value >= maxReconnectAttempts) {
|
|
||||||
console.error('Max reconnection attempts reached')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
reconnectAttempts.value++
|
|
||||||
const delay = reconnectDelay.value * Math.pow(1.5, reconnectAttempts.value - 1)
|
|
||||||
|
|
||||||
console.log(`Reconnecting (attempt ${reconnectAttempts.value}/${maxReconnectAttempts}) in ${Math.round(delay / 1000)}s...`)
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
open()
|
|
||||||
}, delay)
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleSubmissionUpdate = (submission: SubmissionUpdate) => {
|
const handleSubmissionUpdate = (submission: SubmissionUpdate) => {
|
||||||
const existingIndex = userSubmissions.value.findIndex((s: any) => s.id === submission.id)
|
const existingIndex = userSubmissions.value.findIndex((s: any) => s.id === submission.id)
|
||||||
|
|
||||||
@@ -143,10 +129,6 @@ export function useSubmissionsWebSocket(userSubmissions: any) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
|
||||||
close()
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isConnected,
|
isConnected,
|
||||||
status,
|
status,
|
||||||
|
|||||||
Reference in New Issue
Block a user