FarmMapsLib/projects/common/src/fm/services/event.service.ts

38 lines
1.3 KiB
TypeScript
Raw Normal View History

2019-07-15 14:54:19 +00:00
import { Injectable } from '@angular/core';
import { IEventMessage } from '../models/event.message';
import { Subject } from 'rxjs';
import { OAuthService } from 'angular-oauth2-oidc';
2019-11-04 17:47:15 +00:00
import { HubConnection, HubConnectionBuilder, LogLevel ,HttpTransportType} from '@aspnet/signalr';
2019-07-15 14:54:19 +00:00
import { AppConfig } from "../shared/app.config";
2019-11-05 16:19:33 +00:00
@Injectable({
providedIn: 'root',
})
2019-07-15 14:54:19 +00:00
export class EventService {
2019-07-18 16:59:42 +00:00
public event:Subject <IEventMessage> = new Subject<IEventMessage>();
2019-07-15 14:54:19 +00:00
private _connection: HubConnection = null;
private _apiEndPoint: string;
constructor(private oauthService: OAuthService, private appConfig: AppConfig) {
2019-07-18 16:59:42 +00:00
this._apiEndPoint = appConfig.getConfig("apiEndPoint");
2019-11-04 17:47:15 +00:00
this._connection = new HubConnectionBuilder().withUrl(`${ this._apiEndPoint}/eventHub`,
{ transport: HttpTransportType.WebSockets,
// accessTokenFactory: () => {
// return oauthService.getAccessToken();
// },
skipNegotiation:true
}).configureLogging(LogLevel.Information).build();
this._connection.on('event', eventMessage => {
this.event.next(eventMessage);
});
2019-07-15 14:54:19 +00:00
this._connection.start().then(() => {
var accessToken = oauthService.getAccessToken();
if (accessToken) {
this._connection.send('authenticate', oauthService.getAccessToken());
}
2019-11-04 17:47:15 +00:00
});
2019-07-15 14:54:19 +00:00
}
}