Mark offline if websocket connection attempt times out

This commit is contained in:
trevor-signal 2024-05-02 16:53:53 -04:00 committed by GitHub
parent 80bc63bb90
commit c18559b6da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -36,7 +36,7 @@ import WebSocketResource, {
TransportOption, TransportOption,
WebSocketResourceWithShadowing, WebSocketResourceWithShadowing,
} from './WebsocketResources'; } from './WebsocketResources';
import { HTTPError } from './Errors'; import { ConnectTimeoutError, HTTPError } from './Errors';
import type { IRequestHandler, WebAPICredentials } from './Types.d'; import type { IRequestHandler, WebAPICredentials } from './Types.d';
import { connect as connectWebSocket } from './WebSocket'; import { connect as connectWebSocket } from './WebSocket';
import { isAlpha, isBeta, isStaging } from '../util/version'; import { isAlpha, isBeta, isStaging } from '../util/version';
@ -117,6 +117,13 @@ export class SocketManager extends EventListener {
return this.status; return this.status;
} }
private markOffline() {
if (this.privIsOnline !== false) {
this.privIsOnline = false;
this.emit('offline');
}
}
// Update WebAPICredentials and reconnect authenticated resource if // Update WebAPICredentials and reconnect authenticated resource if
// credentials changed // credentials changed
public async authenticate(credentials: WebAPICredentials): Promise<void> { public async authenticate(credentials: WebAPICredentials): Promise<void> {
@ -257,10 +264,11 @@ export class SocketManager extends EventListener {
return; return;
} }
if (code === -1 && this.privIsOnline !== false) { if (code === -1) {
this.privIsOnline = false; this.markOffline();
this.emit('offline');
} }
} else if (error instanceof ConnectTimeoutError) {
this.markOffline();
} }
drop(reconnect()); drop(reconnect());