try to reconnect when connection closed

This commit is contained in:
Kyle Spearrin 2018-08-22 13:46:35 -04:00
parent a67ea2422f
commit ebf6aee542
1 changed files with 53 additions and 17 deletions

View File

@ -19,25 +19,54 @@ import {
export class NotificationsService implements NotificationsServiceAbstraction { export class NotificationsService implements NotificationsServiceAbstraction {
private signalrConnection: signalR.HubConnection; private signalrConnection: signalR.HubConnection;
private url: string; private url: string;
private connected = false;
private inited = false;
private reconnectTimer: any = null;
constructor(private userService: UserService, private tokenService: TokenService, constructor(private userService: UserService, private tokenService: TokenService,
private syncService: SyncService, private appIdService: AppIdService, private syncService: SyncService, private appIdService: AppIdService,
private apiService: ApiService) { } private apiService: ApiService) { }
async init(environmentService: EnvironmentService): Promise<void> { async init(environmentService: EnvironmentService): Promise<void> {
this.inited = false;
this.url = 'https://notifications.bitwarden.com'; this.url = 'https://notifications.bitwarden.com';
if (environmentService.notificationsUrl != null) { if (environmentService.notificationsUrl != null) {
this.url = environmentService.notificationsUrl; this.url = environmentService.notificationsUrl;
} else if (environmentService.baseUrl != null) { } else if (environmentService.baseUrl != null) {
this.url = environmentService.baseUrl + '/notifications'; this.url = environmentService.baseUrl + '/notifications';
} }
this.reconnect();
if (this.signalrConnection != null) {
this.signalrConnection.off('ReceiveMessage');
await this.signalrConnection.stop();
this.connected = false;
this.signalrConnection = null;
}
this.signalrConnection = new signalR.HubConnectionBuilder()
.withUrl(this.url + '/hub', {
accessTokenFactory: () => this.tokenService.getToken(),
})
// .configureLogging(signalR.LogLevel.Information)
.build();
this.signalrConnection.on('ReceiveMessage', async (data: any) => {
await this.processNotification(new NotificationResponse(data));
});
this.signalrConnection.onclose(() => {
this.connected = false;
this.reconnect();
});
this.inited = true;
if (await this.userService.isAuthenticated()) {
await this.connect();
}
} }
async updateConnection(): Promise<void> { async updateConnection(): Promise<void> {
try { try {
if (await this.userService.isAuthenticated()) { if (await this.userService.isAuthenticated()) {
await this.signalrConnection.start(); await this.connect();
} else { } else {
await this.signalrConnection.stop(); await this.signalrConnection.stop();
} }
@ -79,30 +108,37 @@ export class NotificationsService implements NotificationsServiceAbstraction {
case NotificationType.SyncOrgKeys: case NotificationType.SyncOrgKeys:
await this.apiService.refreshIdentityToken(); await this.apiService.refreshIdentityToken();
await this.syncService.fullSync(true); await this.syncService.fullSync(true);
// Now reconnect to join the new org groups // Stop so a reconnect can be made
await this.reconnect(); await this.signalrConnection.stop();
break; break;
default: default:
break; break;
} }
} }
private async connect() {
await this.signalrConnection.start();
this.connected = true;
}
private async reconnect() { private async reconnect() {
if (this.signalrConnection != null) { if (this.reconnectTimer != null) {
await this.signalrConnection.stop(); clearTimeout(this.reconnectTimer);
this.signalrConnection = null; this.reconnectTimer = null;
}
const authed = await this.userService.isAuthenticated();
if (this.connected || !this.inited || !authed) {
return;
} }
this.signalrConnection = new signalR.HubConnectionBuilder() try {
.withUrl(this.url + '/hub', { await this.connect();
accessTokenFactory: () => this.tokenService.getToken(), } catch { }
})
// .configureLogging(signalR.LogLevel.Information)
.build();
this.signalrConnection.on('ReceiveMessage', async (data: any) => { if (!this.connected) {
await this.processNotification(new NotificationResponse(data)); this.reconnectTimer = setTimeout(() => {
}); this.reconnect();
await this.updateConnection(); }, 120000);
}
} }
} }