diff --git a/projects/ucap-webmessenger-app/src/app/services/notification.service.ts b/projects/ucap-webmessenger-app/src/app/services/notification.service.ts index ff31045b..474bbdb3 100644 --- a/projects/ucap-webmessenger-app/src/app/services/notification.service.ts +++ b/projects/ucap-webmessenger-app/src/app/services/notification.service.ts @@ -1,16 +1,15 @@ import { Injectable } from '@angular/core'; -import { filter, tap } from 'rxjs/operators'; +import { tap } from 'rxjs/operators'; import { Store } from '@ngrx/store'; -import { ProtocolService } from '@ucap-webmessenger/protocol'; import { - SVC_TYPE_LOGOUT, SSVC_TYPE_LOGOUT_RES, SSVC_TYPE_LOGOUT_REMOTE_NOTI, - decodeLogout, - decodeLogoutRemoteNotification + AuthenticationProtocolService, + LogoutResponse, + LogoutRemoteNotification } from '@ucap-webmessenger/protocol-authentication'; import * as AuthenticationStore from '../store/account/authentication'; @@ -19,36 +18,29 @@ import { NGXLogger } from 'ngx-logger'; @Injectable() export class AppNotificationService { constructor( - private protocolService: ProtocolService, + private authenticationProtocolService: AuthenticationProtocolService, private store: Store, private logger: NGXLogger ) {} public subscribe(): void { - this.protocolService.serverMessage + this.authenticationProtocolService.logoutNotification$ .pipe( - filter( - message => - message.serviceType === SVC_TYPE_LOGOUT && - message.subServiceType === SSVC_TYPE_LOGOUT_RES - ), - tap(message => { - const logoutRes = decodeLogout(message); - this.logger.debug('logoutRes', logoutRes); - this.store.dispatch(AuthenticationStore.logout()); - }) - ) - .subscribe(); - - this.protocolService.serverMessage - .pipe( - filter( - message => - message.serviceType === SVC_TYPE_LOGOUT && - message.subServiceType === SSVC_TYPE_LOGOUT_REMOTE_NOTI - ), - tap(message => { - const logoutRemoteNoti = decodeLogoutRemoteNotification(message); + tap(notiOrRes => { + switch (notiOrRes.Type) { + case SSVC_TYPE_LOGOUT_RES: + { + const res = notiOrRes as LogoutResponse; + } + break; + case SSVC_TYPE_LOGOUT_REMOTE_NOTI: + { + const noti = notiOrRes as LogoutRemoteNotification; + } + break; + default: + break; + } this.store.dispatch(AuthenticationStore.logout()); }) ) diff --git a/projects/ucap-webmessenger-protocol-authentication/src/lib/services/authentication-protocol.service.ts b/projects/ucap-webmessenger-protocol-authentication/src/lib/services/authentication-protocol.service.ts index 1be94a84..1cd48016 100644 --- a/projects/ucap-webmessenger-protocol-authentication/src/lib/services/authentication-protocol.service.ts +++ b/projects/ucap-webmessenger-protocol-authentication/src/lib/services/authentication-protocol.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@angular/core'; -import { Observable } from 'rxjs'; -import { map, take } from 'rxjs/operators'; +import { Observable, Subject } from 'rxjs'; +import { map, take, filter, tap, share } from 'rxjs/operators'; import { ProtocolService } from '@ucap-webmessenger/protocol'; import { @@ -15,7 +15,9 @@ import { SSVC_TYPE_LOGIN_REQ, SVC_TYPE_LOGOUT, SSVC_TYPE_LOGOUT_REQ, - SSVC_TYPE_LOGOUT_REMOTE_REQ + SSVC_TYPE_LOGOUT_REMOTE_REQ, + SSVC_TYPE_LOGOUT_RES, + SSVC_TYPE_LOGOUT_REMOTE_NOTI } from '../types/service'; import { LogoutRequest, @@ -27,14 +29,57 @@ import { encodeLogoutRemote, decodeLogoutRemote, LogoutRemoteRequest, - LogoutRemoteResponse + LogoutRemoteResponse, + decodeLogoutRemoteNotification, + LogoutRemoteNotification } from '../protocols/logout-remote'; @Injectable({ providedIn: 'root' }) export class AuthenticationProtocolService { - constructor(private protocolService: ProtocolService) {} + private logoutNotificationSubject: Subject< + LogoutResponse | LogoutRemoteNotification + >; + public logoutNotification$: Observable< + LogoutResponse | LogoutRemoteNotification + >; + + constructor(private protocolService: ProtocolService) { + this.logoutNotificationSubject = new Subject(); + this.logoutNotification$ = this.logoutNotificationSubject + .asObservable() + .pipe(share()); + + this.protocolService.serverMessage + .pipe( + filter(message => message.serviceType === SVC_TYPE_LOGOUT), + tap(message => { + switch (message.subServiceType) { + case SSVC_TYPE_LOGOUT_RES: + { + this.logoutNotificationSubject.next({ + ...decodeLogout(message), + Type: SSVC_TYPE_LOGOUT_RES + }); + } + break; + case SSVC_TYPE_LOGOUT_REMOTE_NOTI: + { + this.logoutNotificationSubject.next({ + ...decodeLogoutRemoteNotification(message), + Type: SSVC_TYPE_LOGOUT_REMOTE_NOTI + }); + } + break; + + default: + break; + } + }) + ) + .subscribe(); + } public login(req: LoginRequest): Observable { return this.protocolService diff --git a/projects/ucap-webmessenger-protocol-event/src/lib/services/event-protocol.service.ts b/projects/ucap-webmessenger-protocol-event/src/lib/services/event-protocol.service.ts index 6eded15c..bf4156ae 100644 --- a/projects/ucap-webmessenger-protocol-event/src/lib/services/event-protocol.service.ts +++ b/projects/ucap-webmessenger-protocol-event/src/lib/services/event-protocol.service.ts @@ -1,7 +1,15 @@ import { Injectable } from '@angular/core'; -import { Observable } from 'rxjs'; -import { map, takeWhile, timeout, take } from 'rxjs/operators'; +import { Observable, Subject } from 'rxjs'; +import { + map, + takeWhile, + timeout, + take, + share, + filter, + tap +} from 'rxjs/operators'; import { ProtocolService } from '@ucap-webmessenger/protocol'; @@ -22,7 +30,8 @@ import { SSVC_TYPE_EVENT_PUSH_CL_REQ, SSVC_TYPE_EVENT_READ_REQ, SSVC_TYPE_EVENT_DEL_REQ, - SSVC_TYPE_EVENT_CANCEL_REQ + SSVC_TYPE_EVENT_CANCEL_REQ, + SSVC_TYPE_EVENT_CANCEL_NOTI } from '../types/service'; import { SendRequest, @@ -47,14 +56,44 @@ import { CancelRequest, CancelResponse, encodeCancel, - decodeCancel + decodeCancel, + CancelNotification, + decodeCancelNotification } from '../protocols/cancel'; @Injectable({ providedIn: 'root' }) export class EventProtocolService { - constructor(private protocolService: ProtocolService) {} + private eventNotificationSubject: Subject; + public eventNotification$: Observable; + + constructor(private protocolService: ProtocolService) { + this.eventNotificationSubject = new Subject(); + this.eventNotification$ = this.eventNotificationSubject + .asObservable() + .pipe(share()); + + this.protocolService.serverMessage + .pipe( + filter(message => message.serviceType === SVC_TYPE_EVENT), + tap(message => { + switch (message.subServiceType) { + case SSVC_TYPE_EVENT_CANCEL_NOTI: + { + this.eventNotificationSubject.next({ + ...decodeCancelNotification(message), + Type: SSVC_TYPE_EVENT_CANCEL_NOTI + }); + } + break; + default: + break; + } + }) + ) + .subscribe(); + } public info(req: InfoRequest): Observable { return this.protocolService diff --git a/projects/ucap-webmessenger-protocol/src/lib/protocols/protocol.ts b/projects/ucap-webmessenger-protocol/src/lib/protocols/protocol.ts index 49a5e827..cdeb6149 100644 --- a/projects/ucap-webmessenger-protocol/src/lib/protocols/protocol.ts +++ b/projects/ucap-webmessenger-protocol/src/lib/protocols/protocol.ts @@ -14,8 +14,9 @@ export interface ProtocolStream { Type?: number; } -// tslint:disable-next-line: no-empty-interface -export interface ProtocolNotification {} +export interface ProtocolNotification { + Type?: number; +} export interface ProtocolMessage { serviceType: number;