nats is added

This commit is contained in:
병준 박 2022-08-01 05:33:57 +00:00
parent 7c6da7b882
commit 8572a5ea35
8 changed files with 89 additions and 0 deletions

View File

@ -5,6 +5,8 @@ import { ExtraOptions, PreloadAllModules, RouterModule } from '@angular/router';
import { MatIconRegistry, MatIconModule } from '@angular/material/icon';
import { DomSanitizer } from '@angular/platform-browser';
import { MarkdownModule } from 'ngx-markdown';
import * as nats from 'nats.ws';
import { FuseModule } from '@fuse';
import { FuseConfigModule } from '@fuse/services/config';
import { FuseMockApiModule } from '@fuse/lib/mock-api';
@ -14,12 +16,30 @@ import { mockApiServices } from 'app/mock-api';
import { LayoutModule } from 'app/layout/layout.module';
import { AppComponent } from 'app/app.component';
import { appRoutes } from 'app/app.routing';
import { NATS_CONNECTION } from 'app/core/nats/token';
import { MemberModule } from 'app/modules/polyglot/member/member.module';
const routerConfig: ExtraOptions = {
preloadingStrategy: PreloadAllModules,
scrollPositionRestoration: 'enabled',
};
const natsConnection = () => {
return new Promise<nats.NatsConnection>((resolve, reject) => {
nats
.connect({
servers: ['ws://192.168.50.200:8088'],
})
.then((conn) => {
console.log('NATS connected', conn.info);
resolve(conn);
})
.catch((e) => {
reject(e);
});
});
};
@NgModule({
declarations: [AppComponent],
imports: [
@ -41,7 +61,10 @@ const routerConfig: ExtraOptions = {
// 3rd party modules that require global configuration via forRoot
MarkdownModule.forRoot({}),
MatIconModule,
MemberModule.forRoot(),
],
providers: [{ provide: NATS_CONNECTION, useFactory: natsConnection }],
bootstrap: [AppComponent],
})
export class AppModule {

View File

@ -0,0 +1,7 @@
import { NgModule } from '@angular/core';
@NgModule({
exports: [],
providers: [],
})
export class NatsCoreModule {}

View File

@ -0,0 +1,6 @@
import { InjectionToken } from '@angular/core';
import * as nats from 'nats.ws';
export const NATS_CONNECTION = new InjectionToken<nats.NatsConnection>(
'@bet nats connection'
);

View File

@ -33,6 +33,7 @@ import { User } from '../models/user';
import { UserPagination } from '../models/user-pagination';
import { UserService } from '../services/user.service';
import { Router } from '@angular/router';
import { IdentityService } from 'app/modules/polyglot/member/services/identity.service';
@Component({
selector: 'user-list',
@ -87,6 +88,7 @@ export class ListComponent implements OnInit, AfterViewInit, OnDestroy {
private _fuseConfirmationService: FuseConfirmationService,
private _formBuilder: FormBuilder,
private _userService: UserService,
private _identityService: IdentityService,
private router: Router
) {}
@ -195,6 +197,8 @@ export class ListComponent implements OnInit, AfterViewInit, OnDestroy {
*/
__onClickSearch(): void {
this.__isSearchOpened = !this.__isSearchOpened;
console.log('kkkk', this._identityService.getClientIp());
}
/**

View File

@ -0,0 +1,16 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { SERVICES } from './services';
@NgModule({})
export class MemberRootModule {}
@NgModule({})
export class MemberModule {
public static forRoot(): ModuleWithProviders<MemberRootModule> {
return {
ngModule: MemberRootModule,
providers: [...SERVICES],
};
}
}

View File

@ -0,0 +1,28 @@
import { Inject, Injectable } from '@angular/core';
import { NATS_CONNECTION } from 'app/core/nats/token';
import * as nats from 'nats.ws';
@Injectable({
providedIn: 'root',
})
export class IdentityService {
/**
* Constructor
*/
constructor(
@Inject(NATS_CONNECTION) private __nats_connection: nats.NatsConnection
) {}
// -----------------------------------------------------------------------------------------------------
// @ Accessors
// -----------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
getClientIp(): number | undefined {
return this.__nats_connection.info?.client_id;
}
}

View File

@ -0,0 +1,5 @@
import { Type } from '@angular/core';
import { IdentityService } from './identity.service';
export const SERVICES: Type<any>[] = [IdentityService];