mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-16 02:02:55 -07:00
moved the signalr code into it's own service
This commit is contained in:
parent
15d34e0aaf
commit
bc026e7e72
4 changed files with 57 additions and 17 deletions
|
@ -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
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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<any>;
|
||||
|
||||
constructor(private authService: AuthService) {
|
||||
this.Notification = new EventEmitter<any>();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
2
src/Ombi/ClientApp/src/typings/globals.d.ts
vendored
2
src/Ombi/ClientApp/src/typings/globals.d.ts
vendored
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue