SUPPORT-9130: unsubscribe from socket messages
This commit is contained in:
parent
fee4ad24d6
commit
a9d15b5626
3 changed files with 39 additions and 22 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
import {ChangeDetectionStrategy, ChangeDetectorRef, Component} from "@angular/core";
|
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy} from "@angular/core";
|
||||||
import {AuthorizationService} from "../service/authorization.service";
|
import {AuthorizationService} from "../service/authorization.service";
|
||||||
|
import {WebsocketService} from "../websocket/websocket.service";
|
||||||
|
import {HttpClient} from "@angular/common/http";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
moduleId: module.id,
|
moduleId: module.id,
|
||||||
|
|
@ -7,18 +9,35 @@ import {AuthorizationService} from "../service/authorization.service";
|
||||||
templateUrl: "../../../../../src/resources/template/app/component/app_header.html",
|
templateUrl: "../../../../../src/resources/template/app/component/app_header.html",
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush
|
changeDetection: ChangeDetectionStrategy.OnPush
|
||||||
})
|
})
|
||||||
export class AppHeaderComponent {
|
export class AppHeaderComponent implements OnDestroy {
|
||||||
|
|
||||||
name: string;
|
name: string;
|
||||||
realm: string;
|
realm: string;
|
||||||
|
|
||||||
constructor(protected authService: AuthorizationService,
|
constructor(protected authService: AuthorizationService, protected cd: ChangeDetectorRef,
|
||||||
protected cd: ChangeDetectorRef) {
|
protected websocketService: WebsocketService, protected httpClient: HttpClient) {
|
||||||
authService.onSessionUpdate
|
authService.onSessionUpdate
|
||||||
.subscribe(session => {
|
.subscribe(session => {
|
||||||
this.name = session.name;
|
this.name = session.name;
|
||||||
this.realm = session.realm;
|
this.realm = session.realm;
|
||||||
cd.markForCheck()
|
cd.markForCheck();
|
||||||
|
|
||||||
|
if (this.authService.hasRole('security_administrator')) {
|
||||||
|
this.websocketService.subscribe(({data}) => {
|
||||||
|
let parsedObj = JSON.parse(data);
|
||||||
|
|
||||||
|
if (parsedObj && parsedObj.traceId) {
|
||||||
|
if (parsedObj.className === 'update' || parsedObj.className === 'processError') {
|
||||||
|
this.httpClient.put('status', parsedObj).toPromise()
|
||||||
|
.catch(err => console.log('failed to update application status', err));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ngOnDestroy(): void {
|
||||||
|
this.websocketService.unsubscribe();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
import {Injectable} from "@angular/core";
|
import {Injectable} from "@angular/core";
|
||||||
import {Subject} from "rxjs";
|
import {Subject} from "rxjs";
|
||||||
import {HttpClient} from "@angular/common/http";
|
import {HttpClient} from "@angular/common/http";
|
||||||
import {WebsocketService} from "../websocket/websocket.service";
|
|
||||||
|
|
||||||
export interface UserSession {
|
export interface UserSession {
|
||||||
userId: string,
|
userId: string,
|
||||||
|
|
@ -18,7 +17,7 @@ export class AuthorizationService {
|
||||||
|
|
||||||
public onSessionUpdate: Subject<UserSession> = new Subject<UserSession>();
|
public onSessionUpdate: Subject<UserSession> = new Subject<UserSession>();
|
||||||
|
|
||||||
constructor(protected httpClient: HttpClient, protected websocketService: WebsocketService) {}
|
constructor(protected httpClient: HttpClient) {}
|
||||||
|
|
||||||
public getCurrentSession(): Promise<any> {
|
public getCurrentSession(): Promise<any> {
|
||||||
if (this.session) return new Promise(resolve => resolve(this.session));
|
if (this.session) return new Promise(resolve => resolve(this.session));
|
||||||
|
|
@ -27,19 +26,6 @@ export class AuthorizationService {
|
||||||
.then((session: UserSession) => {
|
.then((session: UserSession) => {
|
||||||
this.session = session;
|
this.session = session;
|
||||||
this.onSessionUpdate.next(session);
|
this.onSessionUpdate.next(session);
|
||||||
|
|
||||||
if (this.hasRole('security_administrator')) {
|
|
||||||
this.websocketService.listen(({data}) => {
|
|
||||||
let parsedObj = JSON.parse(data);
|
|
||||||
|
|
||||||
if (parsedObj && parsedObj.traceId) {
|
|
||||||
if (parsedObj.className === 'update' || parsedObj.className === 'processError') {
|
|
||||||
this.httpClient.put('status', parsedObj).toPromise()
|
|
||||||
.catch(err => console.log('failed to update application status', err));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return session;
|
return session;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,18 @@ import {Injectable} from "@angular/core";
|
||||||
@Injectable({providedIn: 'root'})
|
@Injectable({providedIn: 'root'})
|
||||||
export class WebsocketService {
|
export class WebsocketService {
|
||||||
|
|
||||||
public listen(fn){
|
private initialData;
|
||||||
|
|
||||||
|
public subscribe(fn: Function): void {
|
||||||
let property = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
|
let property = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
|
||||||
const data = property.get;
|
const data = property.get;
|
||||||
|
this.initialData = data;
|
||||||
|
|
||||||
// wrapper that replaces getter
|
// wrapper that replaces getter
|
||||||
function lookAtMessage() {
|
function lookAtMessage() {
|
||||||
let socket = this.currentTarget instanceof WebSocket;
|
let socket = this.currentTarget instanceof WebSocket;
|
||||||
|
|
||||||
if (!socket) {
|
if (!socket || !this.currentTarget.url.endsWith('notifier.message.send.push')) {
|
||||||
return data.call(this);
|
return data.call(this);
|
||||||
}
|
}
|
||||||
let msg = data.call(this);
|
let msg = data.call(this);
|
||||||
|
|
@ -22,4 +25,13 @@ export class WebsocketService {
|
||||||
property.get = lookAtMessage;
|
property.get = lookAtMessage;
|
||||||
Object.defineProperty(MessageEvent.prototype, "data", property);
|
Object.defineProperty(MessageEvent.prototype, "data", property);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public unsubscribe(): void {
|
||||||
|
let property = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
|
||||||
|
|
||||||
|
if (this.initialData) {
|
||||||
|
property.get = this.initialData;
|
||||||
|
Object.defineProperty(MessageEvent.prototype, "data", property);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue