Update socket status more eagerly
This commit is contained in:
parent
3cf6ea882b
commit
f937eba94e
3 changed files with 20 additions and 3 deletions
|
@ -27,6 +27,8 @@ export function initializeNetworkObserver(
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
window.Whisper.events.on('socketStatusChange', refresh);
|
||||||
|
|
||||||
window.addEventListener('online', refresh);
|
window.addEventListener('online', refresh);
|
||||||
window.addEventListener('offline', refresh);
|
window.addEventListener('offline', refresh);
|
||||||
window.setInterval(refresh, REFRESH_INTERVAL);
|
window.setInterval(refresh, REFRESH_INTERVAL);
|
||||||
|
|
|
@ -128,7 +128,7 @@ export class SocketManager extends EventListener {
|
||||||
|
|
||||||
window.log.info('SocketManager: connecting authenticated socket');
|
window.log.info('SocketManager: connecting authenticated socket');
|
||||||
|
|
||||||
this.status = SocketStatus.CONNECTING;
|
this.setStatus(SocketStatus.CONNECTING);
|
||||||
|
|
||||||
const process = this.connectResource({
|
const process = this.connectResource({
|
||||||
path: '/v1/websocket/',
|
path: '/v1/websocket/',
|
||||||
|
@ -185,7 +185,7 @@ export class SocketManager extends EventListener {
|
||||||
let authenticated: WebSocketResource;
|
let authenticated: WebSocketResource;
|
||||||
try {
|
try {
|
||||||
authenticated = await process.getResult();
|
authenticated = await process.getResult();
|
||||||
this.status = SocketStatus.OPEN;
|
this.setStatus(SocketStatus.OPEN);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.log.warn(
|
window.log.warn(
|
||||||
'SocketManager: authenticated socket connection failed with ' +
|
'SocketManager: authenticated socket connection failed with ' +
|
||||||
|
@ -407,6 +407,15 @@ export class SocketManager extends EventListener {
|
||||||
// Private
|
// Private
|
||||||
//
|
//
|
||||||
|
|
||||||
|
private setStatus(status: SocketStatus): void {
|
||||||
|
if (this.status === status) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.status = status;
|
||||||
|
this.emit('statusChange');
|
||||||
|
}
|
||||||
|
|
||||||
private async getUnauthenticatedResource(): Promise<WebSocketResource> {
|
private async getUnauthenticatedResource(): Promise<WebSocketResource> {
|
||||||
if (this.isOffline) {
|
if (this.isOffline) {
|
||||||
throw new HTTPError('SocketManager offline', {
|
throw new HTTPError('SocketManager offline', {
|
||||||
|
@ -592,7 +601,7 @@ export class SocketManager extends EventListener {
|
||||||
|
|
||||||
this.incomingRequestQueue = [];
|
this.incomingRequestQueue = [];
|
||||||
this.authenticated = undefined;
|
this.authenticated = undefined;
|
||||||
this.status = SocketStatus.CLOSED;
|
this.setStatus(SocketStatus.CLOSED);
|
||||||
}
|
}
|
||||||
|
|
||||||
private dropUnauthenticated(
|
private dropUnauthenticated(
|
||||||
|
@ -707,6 +716,7 @@ export class SocketManager extends EventListener {
|
||||||
// EventEmitter types
|
// EventEmitter types
|
||||||
|
|
||||||
public on(type: 'authError', callback: (error: HTTPError) => void): this;
|
public on(type: 'authError', callback: (error: HTTPError) => void): this;
|
||||||
|
public on(type: 'statusChange', callback: () => void): this;
|
||||||
|
|
||||||
public on(
|
public on(
|
||||||
type: string | symbol,
|
type: string | symbol,
|
||||||
|
@ -717,6 +727,7 @@ export class SocketManager extends EventListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
public emit(type: 'authError', error: HTTPError): boolean;
|
public emit(type: 'authError', error: HTTPError): boolean;
|
||||||
|
public emit(type: 'statusChange'): boolean;
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
public emit(type: string | symbol, ...args: Array<any>): boolean {
|
public emit(type: string | symbol, ...args: Array<any>): boolean {
|
||||||
|
|
|
@ -1093,6 +1093,10 @@ export function initialize({
|
||||||
proxyUrl,
|
proxyUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socketManager.on('statusChange', () => {
|
||||||
|
window.Whisper.events.trigger('socketStatusChange');
|
||||||
|
});
|
||||||
|
|
||||||
socketManager.on('authError', () => {
|
socketManager.on('authError', () => {
|
||||||
window.Whisper.events.trigger('unlinkAndDisconnect');
|
window.Whisper.events.trigger('unlinkAndDisconnect');
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue