chore: re-establish websocket connection upon connection close (#2673)
This commit is contained in:
@@ -6,6 +6,7 @@ import { StorageServiceInterface } from '../Storage/StorageServiceInterface'
|
||||
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
import { StorageKey } from '../Storage/StorageKeys'
|
||||
import { Result } from '@standardnotes/domain-core'
|
||||
|
||||
export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, DomainEventInterface> {
|
||||
private webSocket?: WebSocket
|
||||
@@ -36,22 +37,24 @@ export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, D
|
||||
)._websocket_url
|
||||
}
|
||||
|
||||
async startWebSocketConnection(): Promise<void> {
|
||||
async startWebSocketConnection(): Promise<Result<void>> {
|
||||
if (!this.webSocketUrl) {
|
||||
return
|
||||
return Result.fail('WebSocket URL is not set')
|
||||
}
|
||||
|
||||
const webSocketConectionToken = await this.createWebSocketConnectionToken()
|
||||
if (webSocketConectionToken === undefined) {
|
||||
return
|
||||
return Result.fail('Failed to create WebSocket connection token')
|
||||
}
|
||||
|
||||
try {
|
||||
this.webSocket = new WebSocket(`${this.webSocketUrl}?authToken=${webSocketConectionToken}`)
|
||||
this.webSocket.onmessage = this.onWebSocketMessage.bind(this)
|
||||
this.webSocket.onclose = this.onWebSocketClose.bind(this)
|
||||
} catch (e) {
|
||||
console.error('Error starting WebSocket connection', e)
|
||||
|
||||
return Result.ok()
|
||||
} catch (error) {
|
||||
return Result.fail(`Error starting WebSocket connection: ${(error as Error).message}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +90,9 @@ export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, D
|
||||
}
|
||||
|
||||
private onWebSocketClose() {
|
||||
this.webSocket = undefined
|
||||
if (this.webSocket?.readyState === WebSocket.CLOSED) {
|
||||
void this.startWebSocketConnection()
|
||||
}
|
||||
}
|
||||
|
||||
private async createWebSocketConnectionToken(): Promise<string | undefined> {
|
||||
|
||||
Reference in New Issue
Block a user