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 { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
||||||
import { AbstractService } from '../Service/AbstractService'
|
import { AbstractService } from '../Service/AbstractService'
|
||||||
import { StorageKey } from '../Storage/StorageKeys'
|
import { StorageKey } from '../Storage/StorageKeys'
|
||||||
|
import { Result } from '@standardnotes/domain-core'
|
||||||
|
|
||||||
export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, DomainEventInterface> {
|
export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, DomainEventInterface> {
|
||||||
private webSocket?: WebSocket
|
private webSocket?: WebSocket
|
||||||
@@ -36,22 +37,24 @@ export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, D
|
|||||||
)._websocket_url
|
)._websocket_url
|
||||||
}
|
}
|
||||||
|
|
||||||
async startWebSocketConnection(): Promise<void> {
|
async startWebSocketConnection(): Promise<Result<void>> {
|
||||||
if (!this.webSocketUrl) {
|
if (!this.webSocketUrl) {
|
||||||
return
|
return Result.fail('WebSocket URL is not set')
|
||||||
}
|
}
|
||||||
|
|
||||||
const webSocketConectionToken = await this.createWebSocketConnectionToken()
|
const webSocketConectionToken = await this.createWebSocketConnectionToken()
|
||||||
if (webSocketConectionToken === undefined) {
|
if (webSocketConectionToken === undefined) {
|
||||||
return
|
return Result.fail('Failed to create WebSocket connection token')
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.webSocket = new WebSocket(`${this.webSocketUrl}?authToken=${webSocketConectionToken}`)
|
this.webSocket = new WebSocket(`${this.webSocketUrl}?authToken=${webSocketConectionToken}`)
|
||||||
this.webSocket.onmessage = this.onWebSocketMessage.bind(this)
|
this.webSocket.onmessage = this.onWebSocketMessage.bind(this)
|
||||||
this.webSocket.onclose = this.onWebSocketClose.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() {
|
private onWebSocketClose() {
|
||||||
this.webSocket = undefined
|
if (this.webSocket?.readyState === WebSocket.CLOSED) {
|
||||||
|
void this.startWebSocketConnection()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async createWebSocketConnectionToken(): Promise<string | undefined> {
|
private async createWebSocketConnectionToken(): Promise<string | undefined> {
|
||||||
|
|||||||
Reference in New Issue
Block a user