Skip to content

Commit

Permalink
FFM-10924 Tidyup
Browse files Browse the repository at this point in the history
  • Loading branch information
erdirowlands committed Apr 15, 2024
1 parent f5baa09 commit 089e645
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Streamer {
private readTimeoutCheckerId: any
private connectionOpened = false
private disconnectEventEmitted = false
private reconnectAttempts = 0; // Reconnect attempt counter
private reconnectAttempts = 0

constructor(
private eventBus: Emitter,
Expand Down Expand Up @@ -41,20 +41,22 @@ export class Streamer {
const onConnected = () => {
this.logDebugMessage('Stream connected')
this.eventBus.emit(Event.CONNECTED)
this.reconnectAttempts = 0;
this.reconnectAttempts = 0
}

const onDisconnect = () => {
clearInterval(this.readTimeoutCheckerId)
const reconnectDelayMs = getRandom(1000, 10000)
this.reconnectAttempts++;
this.reconnectAttempts++
this.logDebugMessage('Stream disconnected, will reconnect in ' + reconnectDelayMs + 'ms')
if (!this.disconnectEventEmitted) {
this.eventBus.emit(Event.DISCONNECTED)
this.disconnectEventEmitted = true
}
if (this.reconnectAttempts >= 5 && this.reconnectAttempts % 5 === 0) {
this.logErrorMessage(`Failed to reconnect after ${this.reconnectAttempts} attempts`);
this.logErrorMessage(
`Reconnection failed after ${this.reconnectAttempts} attempts; attempting further reconnections.`
)
}
setTimeout(() => this.start(), reconnectDelayMs)
}
Expand Down Expand Up @@ -103,7 +105,7 @@ export class Streamer {
onFailed('SSE timeout')
}

// XMLHttpRequest fires `onload` when a request completes successfully, meaning the entire content has been download.
// XMLHttpRequest fires `onload` when a request completes successfully, meaning the entire content has been downloaded.
// For SSE, if it fires it indicates an invalid state and we should reconnect
this.xhr.onload = () => {
onFailed(`Received XMLHttpRequest onLoad event: ${this.xhr.status}`)
Expand Down

0 comments on commit 089e645

Please sign in to comment.