Automatic reconnect signalR
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good

This commit is contained in:
Willem Dantuma
2020-04-15 21:31:02 +02:00
parent 1532e141b9
commit e964fa72f3
3 changed files with 63 additions and 56 deletions

View File

@@ -1,37 +1,44 @@
import { Injectable } from '@angular/core';
import { IEventMessage } from '../models/event.message';
import { Subject } from 'rxjs';
import { OAuthService } from 'angular-oauth2-oidc';
import { HubConnection, HubConnectionBuilder, LogLevel ,HttpTransportType} from '@aspnet/signalr';
import { AppConfig } from "../shared/app.config";
@Injectable({
providedIn: 'root',
})
export class EventService {
public event:Subject <IEventMessage> = new Subject<IEventMessage>();
private _connection: HubConnection = null;
private _apiEndPoint: string;
constructor(private oauthService: OAuthService, private appConfig: AppConfig) {
this._apiEndPoint = appConfig.getConfig("apiEndPoint");
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);
});
this._connection.start().then(() => {
var accessToken = oauthService.getAccessToken();
if (accessToken) {
this._connection.send('authenticate', oauthService.getAccessToken());
}
});
}
}
import { Injectable } from '@angular/core';
import { IEventMessage } from '../models/event.message';
import { Subject } from 'rxjs';
import { OAuthService } from 'angular-oauth2-oidc';
import { HubConnection, HubConnectionBuilder, LogLevel ,HttpTransportType} from '@microsoft/signalr';
import { AppConfig } from "../shared/app.config";
@Injectable({
providedIn: 'root',
})
export class EventService {
public event:Subject <IEventMessage> = new Subject<IEventMessage>();
private _connection: HubConnection = null;
private _apiEndPoint: string;
constructor(private oauthService: OAuthService, private appConfig: AppConfig) {
this._apiEndPoint = appConfig.getConfig("apiEndPoint");
this._connection = new HubConnectionBuilder().withUrl(`${ this._apiEndPoint}/eventHub`,
{ transport: HttpTransportType.WebSockets,
// accessTokenFactory: () => {
// return oauthService.getAccessToken();
// },
skipNegotiation:true
}).withAutomaticReconnect().configureLogging(LogLevel.Information).build();
this._connection.on('event', eventMessage => {
this.event.next(eventMessage);
});
this._connection.onreconnected( event => {
this.Authenticate();
});
this._connection.start().then(() => {
this.Authenticate();
});
}
Authenticate() {
var accessToken = this.oauthService.getAccessToken();
if (accessToken) {
this._connection.send('authenticate', this.oauthService.getAccessToken());
}
}
}