subscription of logout is added

This commit is contained in:
병준 박 2019-09-19 17:03:39 +09:00
parent 977a6bcdc0
commit 036500f65e
8 changed files with 135 additions and 37 deletions

View File

@ -1,8 +1,36 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { filter, tap } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { ProtocolService } from '@ucap-webmessenger/protocol';
import {
SVC_TYPE_LOGOUT,
SSVC_TYPE_LOGOUT_RES
} from '@ucap-webmessenger/protocol-authentication';
import * as AuthenticationStore from '../store/account/authentication';
@Injectable() @Injectable()
export class AppService { export class AppService {
constructor() {} constructor(
private protocolService: ProtocolService,
private store: Store<any>
) {
this.protocolService.serverMessage
.pipe(
filter(
res =>
res.serviceType === SVC_TYPE_LOGOUT &&
res.subServiceType === SSVC_TYPE_LOGOUT_RES
),
tap(res => {
this.store.dispatch(AuthenticationStore.logout());
})
)
.subscribe();
}
public postInit(): Promise<void> { public postInit(): Promise<void> {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {

View File

@ -6,9 +6,9 @@ import {
SessionStorageService, SessionStorageService,
LocalStorageService LocalStorageService
} from '@ucap-webmessenger/web-storage'; } from '@ucap-webmessenger/web-storage';
import { LoginInfo, KEY_LOGIN_INFO } from '../types';
import { EnviromentUtilService } from '@ucap-webmessenger/util'; import { EnviromentUtilService } from '@ucap-webmessenger/util';
import { DeviceType } from '@ucap-webmessenger/core'; import { DeviceType } from '@ucap-webmessenger/core';
import { LoginInfo, KEY_LOGIN_INFO } from '../types';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -56,5 +56,7 @@ export class AuthenticationService {
} }
} }
logout() {} logout() {
this.sessionStorageService.remove(KEY_LOGIN_INFO);
}
} }

View File

@ -4,31 +4,32 @@ import { Router } from '@angular/router';
import { Actions, ofType, createEffect } from '@ngrx/effects'; import { Actions, ofType, createEffect } from '@ngrx/effects';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators';
catchError,
exhaustMap,
map,
tap,
switchMap,
take
} from 'rxjs/operators';
import { import {
PiService, PiService,
Login2Response, Login2Response,
ResponseStatus ResponseStatus
} from '@ucap-webmessenger/pi'; } from '@ucap-webmessenger/pi';
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';
import {
DialogService,
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
} from '@ucap-webmessenger/ui';
import { import {
login, login,
loginSuccess, loginSuccess,
loginFailure, loginFailure,
loginRedirect, loginRedirect,
logout logout,
logoutConfirmation,
logoutConfirmationDismiss
} from './actions'; } from './actions';
import { LoginInfo } from '../../../types'; import { LoginInfo } from '../../../types';
import { AuthenticationService } from '../../../services/authentication.service'; import { AuthenticationService } from '../../../services/authentication.service';
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';
@Injectable() @Injectable()
export class Effects { export class Effects {
@ -72,7 +73,7 @@ export class Effects {
take(1), take(1),
map((update: boolean) => { map((update: boolean) => {
if (!update) { if (!update) {
this.authentication.login( this.authenticationService.login(
params.loginInfo, params.loginInfo,
params.rememberMe params.rememberMe
); );
@ -90,7 +91,7 @@ export class Effects {
loginRedirect$ = createEffect( loginRedirect$ = createEffect(
() => () =>
this.actions$.pipe( this.actions$.pipe(
ofType(loginRedirect, logout), ofType(loginRedirect),
tap(authed => { tap(authed => {
this.router.navigate(['/account/login']); this.router.navigate(['/account/login']);
}) })
@ -98,11 +99,45 @@ export class Effects {
{ dispatch: false } { dispatch: false }
); );
logout$ = createEffect(() =>
this.actions$.pipe(
ofType(logout),
map(action => action),
map(() => {
this.authenticationService.logout();
return loginRedirect();
})
)
);
logoutConfirmation$ = createEffect(() =>
this.actions$.pipe(
ofType(logoutConfirmation),
exhaustMap(async () => {
const result = await this.dialogService.open<
ConfirmDialogComponent,
ConfirmDialogData,
ConfirmDialogResult
>(ConfirmDialogComponent, {
width: '220px',
data: {
title: 'Logout',
message: 'Logout ?'
}
});
return result.choice;
}),
map(result => (result ? logout() : logoutConfirmationDismiss()))
)
);
constructor( constructor(
private actions$: Actions, private actions$: Actions,
private router: Router,
private piService: PiService, private piService: PiService,
private authentication: AuthenticationService, private authenticationService: AuthenticationService,
@Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService, private dialogService: DialogService,
private router: Router @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService
) {} ) {}
} }

View File

@ -0,0 +1,7 @@
import { ProtocolRequest, ProtocolResponse } from '@ucap-webmessenger/protocol';
// tslint:disable-next-line: no-empty-interface
export interface LogoutRequest extends ProtocolRequest {}
// tslint:disable-next-line: no-empty-interface
export interface LogoutResponse extends ProtocolResponse {}

View File

@ -12,9 +12,11 @@ import { LoginRequest, LoginResponse } from '../models/login';
import { import {
SVC_TYPE_LOGIN, SVC_TYPE_LOGIN,
SSVC_TYPE_LOGIN_REQ, SSVC_TYPE_LOGIN_REQ,
SSVC_TYPE_LOGIN_RES SVC_TYPE_LOGOUT,
SSVC_TYPE_LOGOUT_REQ
} from '../types/service'; } from '../types/service';
import { RoleCode } from '../types/role-code'; import { RoleCode } from '../types/role-code';
import { LogoutRequest, LogoutResponse } from '../models/logout';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -95,4 +97,14 @@ export class AuthenticationProtocolService {
}) })
); );
} }
public logout(req: LogoutRequest): Observable<LogoutResponse> {
return this.protocolService
.call(SVC_TYPE_LOGOUT, SSVC_TYPE_LOGOUT_REQ)
.pipe(
map(res => {
return {} as LogoutResponse;
})
);
}
} }

View File

@ -2,7 +2,7 @@ export const SVC_TYPE_LOGIN = 1;
export const SSVC_TYPE_LOGIN_REQ = 1; // 로그인 요청 export const SSVC_TYPE_LOGIN_REQ = 1; // 로그인 요청
export const SSVC_TYPE_LOGIN_RES = 2; // 로그인 성공 export const SSVC_TYPE_LOGIN_RES = 2; // 로그인 성공
export const SSVC_TYPE_FMC_INFO_REQ = 101; // FMC 정보요청 export const SSVC_TYPE_FMC_INFO_REQ = 101; // FMC 정보요청
export const SSVC_TYPE_FMC_INFO = 100; // FMC 정보 export const SSVC_TYPE_FMC_INFO_RES = 100; // FMC 정보
export const SVC_TYPE_LOGOUT = 2; export const SVC_TYPE_LOGOUT = 2;
export const SSVC_TYPE_LOGOUT_REQ = 1; // 로그아웃 요청 export const SSVC_TYPE_LOGOUT_REQ = 1; // 로그아웃 요청

View File

@ -4,6 +4,7 @@
export * from './lib/models/fmc'; export * from './lib/models/fmc';
export * from './lib/models/login'; export * from './lib/models/login';
export * from './lib/models/logout';
export * from './lib/services/authentication-protocol.service'; export * from './lib/services/authentication-protocol.service';

View File

@ -21,8 +21,15 @@ import {
import { PacketBodyValue } from '../types/packet-body-value.type'; import { PacketBodyValue } from '../types/packet-body-value.type';
import { SSVC_TYPE_ERROR_RES, ServerErrorCode } from '../types/service'; import { SSVC_TYPE_ERROR_RES, ServerErrorCode } from '../types/service';
export interface ServerMessage {
serviceType: number;
subServiceType: number;
senderSeq: number;
bodyList: any[];
}
interface RequestState { interface RequestState {
subject: Subject<ServerResponse>; subject: Subject<ServerMessage>;
request: { request: {
serviceType: number; serviceType: number;
subServiceType: number; subServiceType: number;
@ -30,13 +37,6 @@ interface RequestState {
}; };
} }
export interface ServerResponse {
serviceType: number;
subServiceType: number;
senderSeq: number;
bodyList: any[];
}
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
@ -48,10 +48,16 @@ export class ProtocolService {
private socket$: Observable<GetWebSocketResponses<any>>; private socket$: Observable<GetWebSocketResponses<any>>;
private messages$: Observable<any>; private messages$: Observable<any>;
private messagesSubscription: Subscription | null = null; private messagesSubscription: Subscription | null = null;
private serverMessageSubject: Subject<ServerMessage> | null = null;
private serverMessage$: Observable<ServerMessage> | null = null;
constructor(@Inject(_MODULE_CONFIG) private moduleConfig: ModuleConfig) { constructor(@Inject(_MODULE_CONFIG) private moduleConfig: ModuleConfig) {
this.pendingRequests = new Map(); this.pendingRequests = new Map();
this.input$ = new QueueingSubject<string>(); this.input$ = new QueueingSubject<string>();
this.serverMessageSubject = new Subject();
this.serverMessage$ = this.serverMessageSubject
.asObservable()
.pipe(share());
} }
public connect(serverIp: string | null = null): Promise<void> { public connect(serverIp: string | null = null): Promise<void> {
@ -86,8 +92,8 @@ export class ProtocolService {
this.pendingRequests.delete(res.requestId); this.pendingRequests.delete(res.requestId);
} }
if (SSVC_TYPE_ERROR_RES === res.response.subServiceType) { if (SSVC_TYPE_ERROR_RES === res.message.subServiceType) {
const errorCode: ServerErrorCode = res.response const errorCode: ServerErrorCode = res.message
.bodyList[0] as ServerErrorCode; .bodyList[0] as ServerErrorCode;
if (requestState) { if (requestState) {
@ -97,8 +103,11 @@ export class ProtocolService {
} }
if (requestState) { if (requestState) {
requestState.subject.next(res.response); requestState.subject.next(res.message);
return;
} }
this.serverMessageSubject.next(res.message);
}, },
(error: Error) => { (error: Error) => {
const { message } = error; const { message } = error;
@ -122,11 +131,15 @@ export class ProtocolService {
this.messagesSubscription.unsubscribe(); this.messagesSubscription.unsubscribe();
} }
public get serverMessage(): Observable<ServerMessage> {
return this.serverMessage$;
}
public call( public call(
serviceType: number | null, serviceType: number | null,
subServiceType: number | null, subServiceType: number | null,
...bodyList: PacketBody[] ...bodyList: PacketBody[]
): Observable<ServerResponse> { ): Observable<ServerMessage> {
return this.sendInternal(true, serviceType, subServiceType, bodyList); return this.sendInternal(true, serviceType, subServiceType, bodyList);
} }
@ -143,9 +156,9 @@ export class ProtocolService {
serviceType: number, serviceType: number,
subServiceType: number, subServiceType: number,
bodyList: PacketBody[] bodyList: PacketBody[]
): Observable<ServerResponse> | undefined { ): Observable<ServerMessage> | undefined {
let packet: string; let packet: string;
let responseSubject: Subject<ServerResponse> | null = null; let responseSubject: Subject<ServerMessage> | null = null;
if (hasResponse) { if (hasResponse) {
const requestId = this.requestId; const requestId = this.requestId;
@ -155,7 +168,7 @@ export class ProtocolService {
{ type: PacketBodyValue.RequestId, value: requestId } { type: PacketBodyValue.RequestId, value: requestId }
]); ]);
responseSubject = new Subject<ServerResponse>(); responseSubject = new Subject<ServerMessage>();
this.pendingRequests.set(requestId, { this.pendingRequests.set(requestId, {
subject: responseSubject, subject: responseSubject,
request: { serviceType, subServiceType, bodyList } request: { serviceType, subServiceType, bodyList }
@ -182,7 +195,7 @@ export class ProtocolService {
private decodePacket( private decodePacket(
arg: string[] arg: string[]
): { requestId: number; response: ServerResponse } | null { ): { requestId: number; message: ServerMessage } | null {
const cmdArg = arg[0].split(PacketBodyValueDivider); const cmdArg = arg[0].split(PacketBodyValueDivider);
if (2 > cmdArg.length) { if (2 > cmdArg.length) {
// OnError(3); // OnError(3);
@ -247,7 +260,7 @@ export class ProtocolService {
return { return {
requestId, requestId,
response: { message: {
serviceType, serviceType,
subServiceType, subServiceType,
senderSeq, senderSeq,