From 834c25762cac7c077ac3ea9969efca79ce2cf1c5 Mon Sep 17 00:00:00 2001 From: Richard Park Date: Thu, 19 Sep 2019 14:15:43 +0900 Subject: [PATCH] protocol-inner is added --- angular.json | 41 ++++++- package-lock.json | 6 - .../src/app/app-provider.module.ts | 2 + .../src/app/app-routing.module.ts | 6 +- .../src/app/app.module.ts | 2 + .../components/main.page.component.ts | 13 ++- .../src/app/resolvers/index.ts | 3 + .../src/app/resolvers/messenger.resolver.ts | 65 +++++++++++ .../store/account/authentication/actions.ts | 9 -- .../store/account/authentication/effects.ts | 63 ++++++----- .../app/store/setting/version-info/effects.ts | 21 ---- .../README.md | 25 ++++ .../karma.conf.js | 35 ++++++ .../ng-package.json | 7 ++ .../package.json | 8 ++ .../src/lib/models/conn.ts | 8 ++ .../services/inner-protocol.service.spec.ts | 12 ++ .../lib/services/inner-protocol.service.ts | 24 ++++ .../src/lib/types/service.ts | 4 + .../src/lib/ucap-inner-protocol.module.ts | 19 ++++ .../src/public-api.ts | 7 ++ .../src/test.ts | 21 ++++ .../tsconfig.lib.json | 26 +++++ .../tsconfig.spec.json | 17 +++ .../tslint.json | 7 ++ .../src/lib/services/protocol.service.ts | 107 ++++++++++-------- tsconfig.json | 3 + 27 files changed, 435 insertions(+), 126 deletions(-) create mode 100644 projects/ucap-webmessenger-app/src/app/resolvers/index.ts create mode 100644 projects/ucap-webmessenger-app/src/app/resolvers/messenger.resolver.ts create mode 100644 projects/ucap-webmessenger-protocol-inner/README.md create mode 100644 projects/ucap-webmessenger-protocol-inner/karma.conf.js create mode 100644 projects/ucap-webmessenger-protocol-inner/ng-package.json create mode 100644 projects/ucap-webmessenger-protocol-inner/package.json create mode 100644 projects/ucap-webmessenger-protocol-inner/src/lib/models/conn.ts create mode 100644 projects/ucap-webmessenger-protocol-inner/src/lib/services/inner-protocol.service.spec.ts create mode 100644 projects/ucap-webmessenger-protocol-inner/src/lib/services/inner-protocol.service.ts create mode 100644 projects/ucap-webmessenger-protocol-inner/src/lib/types/service.ts create mode 100644 projects/ucap-webmessenger-protocol-inner/src/lib/ucap-inner-protocol.module.ts create mode 100644 projects/ucap-webmessenger-protocol-inner/src/public-api.ts create mode 100644 projects/ucap-webmessenger-protocol-inner/src/test.ts create mode 100644 projects/ucap-webmessenger-protocol-inner/tsconfig.lib.json create mode 100644 projects/ucap-webmessenger-protocol-inner/tsconfig.spec.json create mode 100644 projects/ucap-webmessenger-protocol-inner/tslint.json diff --git a/angular.json b/angular.json index cc24d340..b8a09b85 100644 --- a/angular.json +++ b/angular.json @@ -1200,9 +1200,7 @@ "projects/ucap-webmessenger-web-socket/tsconfig.lib.json", "projects/ucap-webmessenger-web-socket/tsconfig.spec.json" ], - "exclude": [ - "**/node_modules/**" - ] + "exclude": ["**/node_modules/**"] } } } @@ -1235,9 +1233,40 @@ "projects/ucap-webmessenger-util/tsconfig.lib.json", "projects/ucap-webmessenger-util/tsconfig.spec.json" ], - "exclude": [ - "**/node_modules/**" - ] + "exclude": ["**/node_modules/**"] + } + } + } + }, + "ucap-webmessenger-protocol-inner": { + "projectType": "library", + "root": "projects/ucap-webmessenger-protocol-inner", + "sourceRoot": "projects/ucap-webmessenger-protocol-inner/src", + "prefix": "ucap-protocol-inner", + "architect": { + "build": { + "builder": "@angular-devkit/build-ng-packagr:build", + "options": { + "tsConfig": "projects/ucap-webmessenger-protocol-inner/tsconfig.lib.json", + "project": "projects/ucap-webmessenger-protocol-inner/ng-package.json" + } + }, + "test": { + "builder": "@angular-devkit/build-angular:karma", + "options": { + "main": "projects/ucap-webmessenger-protocol-inner/src/test.ts", + "tsConfig": "projects/ucap-webmessenger-protocol-inner/tsconfig.spec.json", + "karmaConfig": "projects/ucap-webmessenger-protocol-inner/karma.conf.js" + } + }, + "lint": { + "builder": "@angular-devkit/build-angular:tslint", + "options": { + "tsConfig": [ + "projects/ucap-webmessenger-protocol-inner/tsconfig.lib.json", + "projects/ucap-webmessenger-protocol-inner/tsconfig.spec.json" + ], + "exclude": ["**/node_modules/**"] } } } diff --git a/package-lock.json b/package-lock.json index c9ba9396..1a59786f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4832,12 +4832,6 @@ "integrity": "sha1-QICG1AlVDCYxFVYZ6fp7ytw7mRs=", "dev": true }, - "current-device": { - "version": "0.8.2", - "resolved": "https://registry.npmjs.org/current-device/-/current-device-0.8.2.tgz", - "integrity": "sha512-DB/lozoIa5jZpyNQq7C6zlWNJ00V/qKeYhXRyI1sckTmlQhS9sfeflK72Tsx7HyUYmDLywb3lhfzrHt/9Tx7Gg==", - "dev": true - }, "currently-unhandled": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz", diff --git a/projects/ucap-webmessenger-app/src/app/app-provider.module.ts b/projects/ucap-webmessenger-app/src/app/app-provider.module.ts index 08f091b5..05b28019 100644 --- a/projects/ucap-webmessenger-app/src/app/app-provider.module.ts +++ b/projects/ucap-webmessenger-app/src/app/app-provider.module.ts @@ -3,6 +3,7 @@ import { NgModule, APP_INITIALIZER } from '@angular/core'; import { UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native'; import { ElectronNativeService } from '@ucap-webmessenger/native-electron'; +import { RESOLVERS } from './resolvers'; import { SERVICES } from './services'; import { AppService } from './services/app.service'; @@ -17,6 +18,7 @@ export function initializeApp(appService: AppService) { exports: [], providers: [ ...SERVICES, + ...RESOLVERS, { provide: APP_INITIALIZER, useFactory: initializeApp, diff --git a/projects/ucap-webmessenger-app/src/app/app-routing.module.ts b/projects/ucap-webmessenger-app/src/app/app-routing.module.ts index 46569658..ce7e1aa9 100644 --- a/projects/ucap-webmessenger-app/src/app/app-routing.module.ts +++ b/projects/ucap-webmessenger-app/src/app/app-routing.module.ts @@ -1,13 +1,17 @@ import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { AuthGuard } from './guards/auth.guard'; +import { MessengerResolver } from './resolvers/messenger.resolver'; const routes: Routes = [ { path: '', redirectTo: '/messenger', pathMatch: 'full' }, { path: 'messenger', loadChildren: './pages/messenger/messenger.page.module#MessengerPageModule', - canActivate: [AuthGuard] + canActivate: [AuthGuard], + resolve: { + protocol: MessengerResolver + } }, { path: 'account', diff --git a/projects/ucap-webmessenger-app/src/app/app.module.ts b/projects/ucap-webmessenger-app/src/app/app.module.ts index 084bf072..20b54c1d 100644 --- a/projects/ucap-webmessenger-app/src/app/app.module.ts +++ b/projects/ucap-webmessenger-app/src/app/app.module.ts @@ -13,6 +13,7 @@ import { UCapPiModule } from '@ucap-webmessenger/pi'; import { UCapProtocolModule } from '@ucap-webmessenger/protocol'; import { UCapAuthenticationProtocolModule } from '@ucap-webmessenger/protocol-authentication'; +import { UCapInnerProtocolModule } from '@ucap-webmessenger/protocol-inner'; import { UCapUiModule } from '@ucap-webmessenger/ui'; import { UCapUiAccountModule } from '@ucap-webmessenger/ui-account'; @@ -60,6 +61,7 @@ import { GUARDS } from './guards'; requestId: environment.protocol.requestId }), UCapAuthenticationProtocolModule.forRoot(), + UCapInnerProtocolModule.forRoot(), UCapUiModule.forRoot(), UCapUiAccountModule.forRoot(), diff --git a/projects/ucap-webmessenger-app/src/app/pages/messenger/components/main.page.component.ts b/projects/ucap-webmessenger-app/src/app/pages/messenger/components/main.page.component.ts index a3788af2..9eace471 100644 --- a/projects/ucap-webmessenger-app/src/app/pages/messenger/components/main.page.component.ts +++ b/projects/ucap-webmessenger-app/src/app/pages/messenger/components/main.page.component.ts @@ -1,10 +1,17 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; +import { AuthenticationProtocolService } from '@ucap-webmessenger/protocol-authentication'; @Component({ selector: 'app-page-messenger-main', templateUrl: './main.page.component.html', styleUrls: ['./main.page.component.scss'] }) -export class MainPageComponent { - title = 'ucap-webmessenger-app'; +export class MainPageComponent implements OnInit { + constructor( + private authenticationProtocolService: AuthenticationProtocolService + ) {} + + ngOnInit(): void { + // this.authenticationProtocolService.login({}); + } } diff --git a/projects/ucap-webmessenger-app/src/app/resolvers/index.ts b/projects/ucap-webmessenger-app/src/app/resolvers/index.ts new file mode 100644 index 00000000..93ac0e7b --- /dev/null +++ b/projects/ucap-webmessenger-app/src/app/resolvers/index.ts @@ -0,0 +1,3 @@ +import { MessengerResolver } from './messenger.resolver'; + +export const RESOLVERS = [MessengerResolver]; diff --git a/projects/ucap-webmessenger-app/src/app/resolvers/messenger.resolver.ts b/projects/ucap-webmessenger-app/src/app/resolvers/messenger.resolver.ts new file mode 100644 index 00000000..7af2e8b7 --- /dev/null +++ b/projects/ucap-webmessenger-app/src/app/resolvers/messenger.resolver.ts @@ -0,0 +1,65 @@ +import { Injectable } from '@angular/core'; +import { + Resolve, + ActivatedRouteSnapshot, + RouterStateSnapshot +} from '@angular/router'; +import { Observable } from 'rxjs'; +import { take, map } from 'rxjs/operators'; + +import { Store, select } from '@ngrx/store'; + +import { ProtocolService } from '@ucap-webmessenger/protocol'; +import { SessionStorageService } from '@ucap-webmessenger/web-storage'; +import { PublicApiService } from '@ucap-webmessenger/api-public'; + +import * as AppStore from '../store'; +import * as VersionInfoStore from '../store/setting/version-info'; +import { LoginInfo, KEY_LOGIN_INFO } from '../types'; +import { InnerProtocolService } from '@ucap-webmessenger/protocol-inner'; + +@Injectable() +export class MessengerResolver implements Resolve { + constructor( + private store: Store, + private sessionStorageService: SessionStorageService, + private publicApiService: PublicApiService, + private protocolService: ProtocolService, + private innerProtocolService: InnerProtocolService + ) {} + + resolve( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot + ): void | Observable | Promise { + const loginInfo = this.sessionStorageService.get(KEY_LOGIN_INFO); + + return this.publicApiService + .versionInfo2({ + deviceType: loginInfo.deviceType, + companyGroupType: 'C', + companyCode: loginInfo.companyCode, + loginId: loginInfo.loginId + }) + .pipe( + take(1), + map(res => { + this.store.dispatch(VersionInfoStore.fetchSuccess(res)); + this.protocolService + .connect(res.serverIp) + .then(() => { + this.innerProtocolService + .conn({}) + .pipe( + take(1), + map(() => { + console.log('innerProtocolService.conn'); + }) + ) + .subscribe(); + }) + .catch(reason => {}); + }) + ); + } +} diff --git a/projects/ucap-webmessenger-app/src/app/store/account/authentication/actions.ts b/projects/ucap-webmessenger-app/src/app/store/account/authentication/actions.ts index 7c2656fd..28b5bcc0 100644 --- a/projects/ucap-webmessenger-app/src/app/store/account/authentication/actions.ts +++ b/projects/ucap-webmessenger-app/src/app/store/account/authentication/actions.ts @@ -21,15 +21,6 @@ export const loginSuccess = createAction( }>() ); -export const postLoginSuccess = createAction( - '[Account::Authentication] Post Login Success', - props<{ - loginInfo: LoginInfo; - rememberMe: boolean; - login2Response: Login2Response; - }>() -); - export const loginFailure = createAction( '[Account::Authentication] Login Failure', props<{ error: any }>() diff --git a/projects/ucap-webmessenger-app/src/app/store/account/authentication/effects.ts b/projects/ucap-webmessenger-app/src/app/store/account/authentication/effects.ts index 2eb7ea9d..700222a8 100644 --- a/projects/ucap-webmessenger-app/src/app/store/account/authentication/effects.ts +++ b/projects/ucap-webmessenger-app/src/app/store/account/authentication/effects.ts @@ -4,7 +4,14 @@ import { Router } from '@angular/router'; import { Actions, ofType, createEffect } from '@ngrx/effects'; import { of } from 'rxjs'; -import { catchError, exhaustMap, map, tap, switchMap } from 'rxjs/operators'; +import { + catchError, + exhaustMap, + map, + tap, + switchMap, + take +} from 'rxjs/operators'; import { PiService, @@ -17,8 +24,7 @@ import { loginSuccess, loginFailure, loginRedirect, - logout, - postLoginSuccess + logout } from './actions'; import { LoginInfo } from '../../../types'; import { AuthenticationService } from '../../../services/authentication.service'; @@ -39,7 +45,6 @@ export class Effects { }) .pipe( map((res: Login2Response) => { - console.log(res); if (res.status === ResponseStatus.Fail) { return loginFailure({ error: 'Failed' }); } else { @@ -56,32 +61,30 @@ export class Effects { ) ); - loginSuccess$ = createEffect(() => - this.actions$.pipe( - ofType(loginSuccess), - map(action => action), - exhaustMap( - (params: { - loginInfo: LoginInfo; - rememberMe: boolean; - login2Response: Login2Response; - }) => - this.nativeService.checkForUpdates().pipe( - map((update: boolean) => { - if (!update) { - this.authentication.login(params.loginInfo, params.rememberMe); - this.router.navigate(['/messenger']); - return postLoginSuccess({ - loginInfo: params.loginInfo, - rememberMe: params.rememberMe, - login2Response: params.login2Response - }); - } - }), - catchError(error => of(error)) - ) - ) - ) + loginSuccess$ = createEffect( + () => + this.actions$.pipe( + ofType(loginSuccess), + tap(params => { + this.nativeService + .checkForUpdates() + .pipe( + take(1), + map((update: boolean) => { + if (!update) { + this.authentication.login( + params.loginInfo, + params.rememberMe + ); + this.router.navigate(['/messenger']); + } + }), + catchError(error => of(error)) + ) + .subscribe(); + }) + ), + { dispatch: false } ); loginRedirect$ = createEffect( diff --git a/projects/ucap-webmessenger-app/src/app/store/setting/version-info/effects.ts b/projects/ucap-webmessenger-app/src/app/store/setting/version-info/effects.ts index 656a811c..726770fd 100644 --- a/projects/ucap-webmessenger-app/src/app/store/setting/version-info/effects.ts +++ b/projects/ucap-webmessenger-app/src/app/store/setting/version-info/effects.ts @@ -10,31 +10,10 @@ import { PublicApiService } from '@ucap-webmessenger/api-public'; import { StatusCode } from '@ucap-webmessenger/api'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; -import { postLoginSuccess } from '../../account/authentication'; - import { fetch, fetchSuccess, fetchFailure } from './actions'; -import { LoginInfo, KEY_LOGIN_INFO } from '../../../types'; @Injectable() export class Effects { - init$ = createEffect(() => - this.actions$.pipe( - ofType(postLoginSuccess), - map(action => { - const loginInfo = this.sessionStorageService.get( - KEY_LOGIN_INFO - ); - - return fetch({ - deviceType: loginInfo.deviceType, - companyGroupType: 'C', - companyCode: loginInfo.companyCode, - loginId: loginInfo.loginId - }); - }) - ) - ); - fetch$ = createEffect(() => this.actions$.pipe( ofType(fetch), diff --git a/projects/ucap-webmessenger-protocol-inner/README.md b/projects/ucap-webmessenger-protocol-inner/README.md new file mode 100644 index 00000000..ee91a7e0 --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/README.md @@ -0,0 +1,25 @@ +# UcapWebmessengerProtocolInner + +This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.2.5. + +## Code scaffolding + +Run `ng generate component component-name --project ucap-webmessenger-protocol-inner` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project ucap-webmessenger-protocol-inner`. + +> Note: Don't forget to add `--project ucap-webmessenger-protocol-inner` or else it will be added to the default project in your `angular.json` file. + +## Build + +Run `ng build ucap-webmessenger-protocol-inner` to build the project. The build artifacts will be stored in the `dist/` directory. + +## Publishing + +After building your library with `ng build ucap-webmessenger-protocol-inner`, go to the dist folder `cd dist/ucap-webmessenger-protocol-inner` and run `npm publish`. + +## Running unit tests + +Run `ng test ucap-webmessenger-protocol-inner` to execute the unit tests via [Karma](https://karma-runner.github.io). + +## Further help + +To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). diff --git a/projects/ucap-webmessenger-protocol-inner/karma.conf.js b/projects/ucap-webmessenger-protocol-inner/karma.conf.js new file mode 100644 index 00000000..4f105f4f --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/karma.conf.js @@ -0,0 +1,35 @@ +// Karma configuration file, see link for more information +// https://karma-runner.github.io/1.0/config/configuration-file.html + +module.exports = function(config) { + config.set({ + basePath: '', + frameworks: ['jasmine', '@angular-devkit/build-angular'], + plugins: [ + require('karma-jasmine'), + require('karma-chrome-launcher'), + require('karma-jasmine-html-reporter'), + require('karma-coverage-istanbul-reporter'), + require('@angular-devkit/build-angular/plugins/karma') + ], + client: { + clearContext: false // leave Jasmine Spec Runner output visible in browser + }, + coverageIstanbulReporter: { + dir: require('path').join( + __dirname, + '../../coverage/ucap-webmessenger-protocol-inner' + ), + reports: ['html', 'lcovonly', 'text-summary'], + fixWebpackSourcePaths: true + }, + reporters: ['progress', 'kjhtml'], + port: 9876, + colors: true, + logLevel: config.LOG_INFO, + autoWatch: true, + browsers: ['Chrome'], + singleRun: false, + restartOnFileChange: true + }); +}; diff --git a/projects/ucap-webmessenger-protocol-inner/ng-package.json b/projects/ucap-webmessenger-protocol-inner/ng-package.json new file mode 100644 index 00000000..504cfd2d --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/ng-package.json @@ -0,0 +1,7 @@ +{ + "$schema": "../../node_modules/ng-packagr/ng-package.schema.json", + "dest": "../../dist/ucap-webmessenger-protocol-inner", + "lib": { + "entryFile": "src/public-api.ts" + } +} diff --git a/projects/ucap-webmessenger-protocol-inner/package.json b/projects/ucap-webmessenger-protocol-inner/package.json new file mode 100644 index 00000000..26a1a208 --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/package.json @@ -0,0 +1,8 @@ +{ + "name": "@ucap-webmessenger/protocol-inner", + "version": "0.0.1", + "peerDependencies": { + "@angular/common": "^8.2.5", + "@angular/core": "^8.2.5" + } +} diff --git a/projects/ucap-webmessenger-protocol-inner/src/lib/models/conn.ts b/projects/ucap-webmessenger-protocol-inner/src/lib/models/conn.ts new file mode 100644 index 00000000..dc5b908d --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/src/lib/models/conn.ts @@ -0,0 +1,8 @@ +import { DeviceType, LocaleCode } from '@ucap-webmessenger/core'; +import { ProtocolRequest, ProtocolResponse } from '@ucap-webmessenger/protocol'; + +// tslint:disable-next-line: no-empty-interface +export interface ConnRequest extends ProtocolRequest {} + +// tslint:disable-next-line: no-empty-interface +export interface ConnResponse extends ProtocolResponse {} diff --git a/projects/ucap-webmessenger-protocol-inner/src/lib/services/inner-protocol.service.spec.ts b/projects/ucap-webmessenger-protocol-inner/src/lib/services/inner-protocol.service.spec.ts new file mode 100644 index 00000000..2651f4e6 --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/src/lib/services/inner-protocol.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { InnerProtocolService } from './inner-protocol.service'; + +describe('InnerProtocolService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: InnerProtocolService = TestBed.get(InnerProtocolService); + expect(service).toBeTruthy(); + }); +}); diff --git a/projects/ucap-webmessenger-protocol-inner/src/lib/services/inner-protocol.service.ts b/projects/ucap-webmessenger-protocol-inner/src/lib/services/inner-protocol.service.ts new file mode 100644 index 00000000..26131ed0 --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/src/lib/services/inner-protocol.service.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core'; + +import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; + +import { ProtocolService } from '@ucap-webmessenger/protocol'; + +import { ConnResponse, ConnRequest } from '../models/conn'; +import { SVC_TYPE_INNER, SSVC_TYPE_CONN_REQ } from '../types/service'; + +@Injectable({ + providedIn: 'root' +}) +export class InnerProtocolService { + constructor(private protocolService: ProtocolService) {} + + public conn(req: ConnRequest): Observable { + return this.protocolService.call(SVC_TYPE_INNER, SSVC_TYPE_CONN_REQ).pipe( + map(res => { + return {} as ConnResponse; + }) + ); + } +} diff --git a/projects/ucap-webmessenger-protocol-inner/src/lib/types/service.ts b/projects/ucap-webmessenger-protocol-inner/src/lib/types/service.ts new file mode 100644 index 00000000..d3ac078b --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/src/lib/types/service.ts @@ -0,0 +1,4 @@ +export const SVC_TYPE_INNER = 1000; + +export const SSVC_TYPE_CONN_REQ = 1; // (클라이언트 접속 시) +export const SSVC_TYPE_CONN_RES = 2; // (클라이언트 접속 시) diff --git a/projects/ucap-webmessenger-protocol-inner/src/lib/ucap-inner-protocol.module.ts b/projects/ucap-webmessenger-protocol-inner/src/lib/ucap-inner-protocol.module.ts new file mode 100644 index 00000000..ce928948 --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/src/lib/ucap-inner-protocol.module.ts @@ -0,0 +1,19 @@ +import { NgModule, ModuleWithProviders } from '@angular/core'; + +import { InnerProtocolService } from './services/inner-protocol.service'; + +const SERVICES = [InnerProtocolService]; + +@NgModule({ + declarations: [], + imports: [], + exports: [] +}) +export class UCapInnerProtocolModule { + public static forRoot(): ModuleWithProviders { + return { + ngModule: UCapInnerProtocolModule, + providers: [...SERVICES] + }; + } +} diff --git a/projects/ucap-webmessenger-protocol-inner/src/public-api.ts b/projects/ucap-webmessenger-protocol-inner/src/public-api.ts new file mode 100644 index 00000000..1fdd7fef --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/src/public-api.ts @@ -0,0 +1,7 @@ +/* + * Public API Surface of ucap-webmessenger-protocol-inner + */ + +export * from './lib/services/inner-protocol.service'; + +export * from './lib/ucap-inner-protocol.module'; diff --git a/projects/ucap-webmessenger-protocol-inner/src/test.ts b/projects/ucap-webmessenger-protocol-inner/src/test.ts new file mode 100644 index 00000000..978c64fb --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/src/test.ts @@ -0,0 +1,21 @@ +// This file is required by karma.conf.js and loads recursively all the .spec and framework files + +import 'zone.js/dist/zone'; +import 'zone.js/dist/zone-testing'; +import { getTestBed } from '@angular/core/testing'; +import { + BrowserDynamicTestingModule, + platformBrowserDynamicTesting +} from '@angular/platform-browser-dynamic/testing'; + +declare const require: any; + +// First, initialize the Angular testing environment. +getTestBed().initTestEnvironment( + BrowserDynamicTestingModule, + platformBrowserDynamicTesting() +); +// Then we find all the tests. +const context = require.context('./', true, /\.spec\.ts$/); +// And load the modules. +context.keys().map(context); diff --git a/projects/ucap-webmessenger-protocol-inner/tsconfig.lib.json b/projects/ucap-webmessenger-protocol-inner/tsconfig.lib.json new file mode 100644 index 00000000..bd23948e --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/tsconfig.lib.json @@ -0,0 +1,26 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "../../out-tsc/lib", + "target": "es2015", + "declaration": true, + "inlineSources": true, + "types": [], + "lib": [ + "dom", + "es2018" + ] + }, + "angularCompilerOptions": { + "annotateForClosureCompiler": true, + "skipTemplateCodegen": true, + "strictMetadataEmit": true, + "fullTemplateTypeCheck": true, + "strictInjectionParameters": true, + "enableResourceInlining": true + }, + "exclude": [ + "src/test.ts", + "**/*.spec.ts" + ] +} diff --git a/projects/ucap-webmessenger-protocol-inner/tsconfig.spec.json b/projects/ucap-webmessenger-protocol-inner/tsconfig.spec.json new file mode 100644 index 00000000..16da33db --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/tsconfig.spec.json @@ -0,0 +1,17 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "../../out-tsc/spec", + "types": [ + "jasmine", + "node" + ] + }, + "files": [ + "src/test.ts" + ], + "include": [ + "**/*.spec.ts", + "**/*.d.ts" + ] +} diff --git a/projects/ucap-webmessenger-protocol-inner/tslint.json b/projects/ucap-webmessenger-protocol-inner/tslint.json new file mode 100644 index 00000000..bb354259 --- /dev/null +++ b/projects/ucap-webmessenger-protocol-inner/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tslint.json", + "rules": { + "directive-selector": [true, "attribute", "ucapProtocolInner", "camelCase"], + "component-selector": [true, "element", "ucap-protocol-inner", "kebab-case"] + } +} diff --git a/projects/ucap-webmessenger-protocol/src/lib/services/protocol.service.ts b/projects/ucap-webmessenger-protocol/src/lib/services/protocol.service.ts index e02a2ec0..1c29b42c 100644 --- a/projects/ucap-webmessenger-protocol/src/lib/services/protocol.service.ts +++ b/projects/ucap-webmessenger-protocol/src/lib/services/protocol.service.ts @@ -54,63 +54,70 @@ export class ProtocolService { this.input$ = new QueueingSubject(); } - public connect(serverIp: string | null = null): void { - this.socket$ = makeWebSocketObservable( - this.moduleConfig.urls.base + serverIp ? serverIp : '' - ); - this.messages$ = this.socket$.pipe( - switchMap(getResponses => getResponses(this.input$)), - retryWhen(errors => - errors.pipe(delay(this.moduleConfig.reconnect.delay)) - ), - share() - ); + public connect(serverIp: string | null = null): Promise { + return new Promise((resolve, reject) => { + this.socket$ = makeWebSocketObservable( + `${this.moduleConfig.urls.base}${serverIp ? serverIp : ''}` + ); + this.messages$ = this.socket$.pipe( + switchMap(getResponses => { + resolve(); + return getResponses(this.input$); + }), + retryWhen(errors => + errors.pipe(delay(this.moduleConfig.reconnect.delay)) + ), + share() + ); - this.messagesSubscription = this.messages$.subscribe( - (message: string) => { - const arg = message.split(PacketBodyDivider); - if (2 > arg.length) { - // OnError(3); - return; - } + this.messagesSubscription = this.messages$.subscribe( + (message: string) => { + console.log(`message`, message); - const res = this.decodePacket(arg); + const arg = message.split(PacketBodyDivider); + if (2 > arg.length) { + // OnError(3); + return; + } - let requestState: RequestState | null = null; - if (res.requestId) { - requestState = this.pendingRequests.get(res.requestId); - this.pendingRequests.delete(res.requestId); - } + const res = this.decodePacket(arg); - if (SSVC_TYPE_ERROR_RES === res.response.subServiceType) { - const errorCode: ServerErrorCode = res.response - .bodyList[0] as ServerErrorCode; + let requestState: RequestState | null = null; + if (res.requestId) { + requestState = this.pendingRequests.get(res.requestId); + this.pendingRequests.delete(res.requestId); + } + + if (SSVC_TYPE_ERROR_RES === res.response.subServiceType) { + const errorCode: ServerErrorCode = res.response + .bodyList[0] as ServerErrorCode; + + if (requestState) { + requestState.subject.error(errorCode); + } + return; + } if (requestState) { - requestState.subject.error(errorCode); + requestState.subject.next(res.response); } - return; + }, + (error: Error) => { + const { message } = error; + if (message === NormalClosureMessage) { + console.log('server closed the websocket connection normally'); + } else { + console.log('socket was disconnected due to error:', message); + } + }, + () => { + // The clean termination only happens in response to the last + // subscription to the observable being unsubscribed, any + // other closure is considered an error. + console.log('the connection was closed in response to the user'); } - - if (requestState) { - requestState.subject.next(res.response); - } - }, - (error: Error) => { - const { message } = error; - if (message === NormalClosureMessage) { - console.log('server closed the websocket connection normally'); - } else { - console.log('socket was disconnected due to error:', message); - } - }, - () => { - // The clean termination only happens in response to the last - // subscription to the observable being unsubscribed, any - // other closure is considered an error. - console.log('the connection was closed in response to the user'); - } - ); + ); + }); } public disconnect(): void { @@ -194,7 +201,7 @@ export class ProtocolService { } const senderSeq = Number(seqArg[0]); - const bodyList: any[] | null = null; + const bodyList: any[] = []; let requestId: number | null = null; for (let i = 2; i < arg.length; i++) { diff --git a/tsconfig.json b/tsconfig.json index 6acdbb3b..a6e377fa 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -68,6 +68,9 @@ "@ucap-webmessenger/protocol-info": [ "projects/ucap-webmessenger-protocol-info/src/public-api" ], + "@ucap-webmessenger/protocol-inner": [ + "projects/ucap-webmessenger-protocol-inner/src/public-api" + ], "@ucap-webmessenger/protocol-option": [ "projects/ucap-webmessenger-protocol-option/src/public-api" ],