From bc026e7e72ca092d9ef3e3fc72daca48ac202b9f Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Tue, 23 Apr 2019 12:36:12 +0100 Subject: [PATCH] moved the signalr code into it's own service --- src/Ombi/ClientApp/src/app/app.component.ts | 22 +++------ src/Ombi/ClientApp/src/app/app.module.ts | 2 + .../services/signlarnotification.service.ts | 48 +++++++++++++++++++ src/Ombi/ClientApp/src/typings/globals.d.ts | 2 +- 4 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 src/Ombi/ClientApp/src/app/services/signlarnotification.service.ts diff --git a/src/Ombi/ClientApp/src/app/app.component.ts b/src/Ombi/ClientApp/src/app/app.component.ts index 6cb0fc07a..456cc42e8 100644 --- a/src/Ombi/ClientApp/src/app/app.component.ts +++ b/src/Ombi/ClientApp/src/app/app.component.ts @@ -12,8 +12,7 @@ import { MatSnackBar } from '@angular/material'; import { ICustomizationSettings, ICustomPage } from "./interfaces"; import { StorageService } from './shared/storage/storage-service'; -import { HubConnection } from '@aspnet/signalr'; -import * as signalR from '@aspnet/signalr'; +import { SignalRNotificationService } from './services/signlarnotification.service'; @Component({ @@ -42,8 +41,6 @@ export class AppComponent implements OnInit { @HostBinding('class') public componentCssClass; - private notificationHubConnection: HubConnection | undefined; - constructor(public notificationService: NotificationService, public authService: AuthService, private readonly router: Router, @@ -54,6 +51,7 @@ export class AppComponent implements OnInit { private readonly customPageService: CustomPageService, public overlayContainer: OverlayContainer, private storage: StorageService, + private signalrNotification: SignalRNotificationService, private readonly snackBar: MatSnackBar) { // const base = this.platformLocation.getBaseHrefFromDOM(); @@ -120,23 +118,15 @@ export class AppComponent implements OnInit { } if (this.authService.loggedIn() && !this.hubConnected) { - this.notificationHubConnection = new signalR.HubConnectionBuilder().withUrl("/hubs/notification", { - accessTokenFactory: () => { - return this.authService.getToken(); - } - }).configureLogging(signalR.LogLevel.Information).build(); - - this.notificationHubConnection - .start() - .then(() => this.hubConnected = true) - .catch(err => console.error(err)); - this.notificationHubConnection.on("Notification", (data: any) => { + this.signalrNotification.initialize(); + this.hubConnected = true; + + this.signalrNotification.Notification.subscribe(data => { this.snackBar.open(data, "OK", { duration: 3000 }); }); } - } }); } diff --git a/src/Ombi/ClientApp/src/app/app.module.ts b/src/Ombi/ClientApp/src/app/app.module.ts index 502bee92a..1526e0295 100644 --- a/src/Ombi/ClientApp/src/app/app.module.ts +++ b/src/Ombi/ClientApp/src/app/app.module.ts @@ -52,6 +52,7 @@ import { NavSearchComponent } from "./my-nav/nav-search.component"; import { OverlayModule } from "@angular/cdk/overlay"; import { getBaseLocation } from "./shared/functions/common-functions"; import { StorageService } from "./shared/storage/storage-service"; +import { SignalRNotificationService } from "./services/signlarnotification.service"; const routes: Routes = [ { path: "*", component: PageNotFoundComponent }, @@ -179,6 +180,7 @@ export function JwtTokenGetter() { SearchV2Service, MessageService, StorageService, + SignalRNotificationService, { provide: APP_BASE_HREF, useValue: window['_app_base'] || '/' } // { // provide: APP_BASE_HREF, diff --git a/src/Ombi/ClientApp/src/app/services/signlarnotification.service.ts b/src/Ombi/ClientApp/src/app/services/signlarnotification.service.ts new file mode 100644 index 000000000..ec417003f --- /dev/null +++ b/src/Ombi/ClientApp/src/app/services/signlarnotification.service.ts @@ -0,0 +1,48 @@ +import { Injectable, EventEmitter } from '@angular/core'; +import { AuthService } from '../auth/auth.service'; + +import { HubConnection } from '@aspnet/signalr'; +import * as signalR from '@aspnet/signalr'; + +@Injectable() +export class SignalRNotificationService { + + private hubConnection: HubConnection | undefined; + public Notification: EventEmitter; + + constructor(private authService: AuthService) { + this.Notification = new EventEmitter(); + } + + public initialize(): void { + + this.stopConnection(); + + this.hubConnection = new signalR.HubConnectionBuilder().withUrl("/hubs/notification", { + accessTokenFactory: () => { + return this.authService.getToken(); + } + }).configureLogging(signalR.LogLevel.Information).build(); + + + this.hubConnection.on("Notification", (data: any) => { + this.Notification.emit(data); + }); + + + this.hubConnection.start().then((data: any) => { + console.log('Now connected'); + }).catch((error: any) => { + console.log('Could not connect ' + error); + setTimeout(() => this.initialize(), 3000); + }); + } + + + stopConnection() { + if (this.hubConnection) { + this.hubConnection.stop(); + this.hubConnection = null; + } + }; +} \ No newline at end of file diff --git a/src/Ombi/ClientApp/src/typings/globals.d.ts b/src/Ombi/ClientApp/src/typings/globals.d.ts index ff6860d41..03059272f 100644 --- a/src/Ombi/ClientApp/src/typings/globals.d.ts +++ b/src/Ombi/ClientApp/src/typings/globals.d.ts @@ -1,6 +1,6 @@ // Globals declare var __webpack_public_path__: any; -declare var module: any; +declare var module: NodeModule; // declare module "*.json" { // const value: any;