moved the signalr code into it's own service

pull/2947/head^2
Jamie Rees 6 years ago
parent 15d34e0aaf
commit bc026e7e72

@ -12,8 +12,7 @@ import { MatSnackBar } from '@angular/material';
import { ICustomizationSettings, ICustomPage } from "./interfaces"; import { ICustomizationSettings, ICustomPage } from "./interfaces";
import { StorageService } from './shared/storage/storage-service'; import { StorageService } from './shared/storage/storage-service';
import { HubConnection } from '@aspnet/signalr'; import { SignalRNotificationService } from './services/signlarnotification.service';
import * as signalR from '@aspnet/signalr';
@Component({ @Component({
@ -42,8 +41,6 @@ export class AppComponent implements OnInit {
@HostBinding('class') public componentCssClass; @HostBinding('class') public componentCssClass;
private notificationHubConnection: HubConnection | undefined;
constructor(public notificationService: NotificationService, constructor(public notificationService: NotificationService,
public authService: AuthService, public authService: AuthService,
private readonly router: Router, private readonly router: Router,
@ -54,6 +51,7 @@ export class AppComponent implements OnInit {
private readonly customPageService: CustomPageService, private readonly customPageService: CustomPageService,
public overlayContainer: OverlayContainer, public overlayContainer: OverlayContainer,
private storage: StorageService, private storage: StorageService,
private signalrNotification: SignalRNotificationService,
private readonly snackBar: MatSnackBar) { private readonly snackBar: MatSnackBar) {
// const base = this.platformLocation.getBaseHrefFromDOM(); // const base = this.platformLocation.getBaseHrefFromDOM();
@ -120,23 +118,15 @@ export class AppComponent implements OnInit {
} }
if (this.authService.loggedIn() && !this.hubConnected) { if (this.authService.loggedIn() && !this.hubConnected) {
this.notificationHubConnection = new signalR.HubConnectionBuilder().withUrl("/hubs/notification", { this.signalrNotification.initialize();
accessTokenFactory: () => { this.hubConnected = true;
return this.authService.getToken();
}
}).configureLogging(signalR.LogLevel.Information).build();
this.notificationHubConnection this.signalrNotification.Notification.subscribe(data => {
.start()
.then(() => this.hubConnected = true)
.catch(err => console.error(err));
this.notificationHubConnection.on("Notification", (data: any) => {
this.snackBar.open(data, "OK", { this.snackBar.open(data, "OK", {
duration: 3000 duration: 3000
}); });
}); });
} }
} }
}); });
} }

@ -52,6 +52,7 @@ import { NavSearchComponent } from "./my-nav/nav-search.component";
import { OverlayModule } from "@angular/cdk/overlay"; import { OverlayModule } from "@angular/cdk/overlay";
import { getBaseLocation } from "./shared/functions/common-functions"; import { getBaseLocation } from "./shared/functions/common-functions";
import { StorageService } from "./shared/storage/storage-service"; import { StorageService } from "./shared/storage/storage-service";
import { SignalRNotificationService } from "./services/signlarnotification.service";
const routes: Routes = [ const routes: Routes = [
{ path: "*", component: PageNotFoundComponent }, { path: "*", component: PageNotFoundComponent },
@ -179,6 +180,7 @@ export function JwtTokenGetter() {
SearchV2Service, SearchV2Service,
MessageService, MessageService,
StorageService, StorageService,
SignalRNotificationService,
{ provide: APP_BASE_HREF, useValue: window['_app_base'] || '/' } { provide: APP_BASE_HREF, useValue: window['_app_base'] || '/' }
// { // {
// provide: APP_BASE_HREF, // 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;
}
};
}

@ -1,6 +1,6 @@
// Globals // Globals
declare var __webpack_public_path__: any; declare var __webpack_public_path__: any;
declare var module: any; declare var module: NodeModule;
// declare module "*.json" { // declare module "*.json" {
// const value: any; // const value: any;

Loading…
Cancel
Save