ekangularbase
Version:
Authentication service for usermanagement(oidc client)
122 lines (99 loc) • 3.29 kB
text/typescript
import { Injectable, Inject } from '@angular/core';
import * as signalR from "@aspnet/signalr";
import { BlockUI, NgBlockUI } from 'ng-block-ui';
import { HttpHeaders, HttpParams, HttpClient } from '@angular/common/http';
import 'rxjs/Rx';
import { BehaviorSubject } from 'rxjs/Rx';
import { AuthService } from '../auth/auth.service';
import { AppConfig } from '../AppConfig/AppConfig';
()
export class ConnectionIndicationService {
connection:signalR.HubConnection;
connectionisvalid:boolean=true;
connectionbroken:boolean=false;
func: Function;
typestr:string;
syncserviceUrl:string;
milisecond:number;
() blockUI: NgBlockUI;
public static ConnectionStatusChanged: BehaviorSubject<boolean>;
intervalfunc:any;
constructor( private authService: AuthService,
public http: HttpClient,
('syncserviceurl') syncserviceUrl: string)
{
this.syncserviceUrl=syncserviceUrl;
ConnectionIndicationService.ConnectionStatusChanged = new BehaviorSubject<boolean>(true);
}
ConnectionCheck()
{
if(this.authService.getUserName!=null)
{
var optionss = {
headers: new HttpHeaders({ 'Content-Type': 'application/json','Authorization': this.authService.getAuthorizationHeaderValue() }),
params:new HttpParams()
};
this.http.get<any>(this.syncserviceUrl+"api/Notification", optionss).subscribe(res=>{
// alert(JSON.stringify(res));
if(JSON.stringify(res) == "true")
{
clearInterval(this.intervalfunc);
this.start();
this.on(this.func,this.typestr);
// this.blockUI.stop();
ConnectionIndicationService.ConnectionStatusChanged.next(true);
}
});
}
}
build()
{
this.connection = new signalR.HubConnectionBuilder()
.withUrl(AppConfig.settings.syncserviceurl+"NotificationHub",
{ accessTokenFactory: () =>
this.authService.getAccessToken()
})
.build();
// this.connection = new signalR.HubConnectionBuilder()
// .withUrl(AppConfig.settings.syncserviceurl+"NotificationHub", signalR.HttpTransportType.WebSockets)
// .build();
this.connection.serverTimeoutInMilliseconds=3600000;
ConnectionIndicationService.ConnectionStatusChanged.next(false);
this.connection.onclose((e)=>{
// this.blockUI.start('Reconnecting...');
console.log('Reconnecting...');
this.intervalfunc =setInterval(() => {
this.ConnectionCheck();
}, 5000);
// this.connectionisvalid=false;
this.stop();
});
return this.connection;
}
on(func: Function,typestr:string)
{
this.func=func;
this.typestr=typestr;
this.connection.on("Random", data => {
});
}
start()
{
this.connection.start().then(() => {
console.log('Hub connection started');
}).catch(err=>{
// alert('Error while establishing connection');
});
}
off()
{
this.connection.off("Random");
}
stop()
{
this.connection.stop().then(()=>{
}).catch(function (err) {
alert('Error while establishing connection');
});
}
}