initial commit
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { Routes, RouterModule } from '@angular/router';
|
||||
|
||||
const routes: Routes = [];
|
||||
|
||||
@NgModule({
|
||||
imports: [RouterModule.forRoot(routes)],
|
||||
exports: [RouterModule]
|
||||
})
|
||||
export class AppRoutingModule { }
|
||||
@@ -0,0 +1,49 @@
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
import {NgModule} from '@angular/core';
|
||||
|
||||
import {AppRoutingModule} from './app-routing.module';
|
||||
import {LandingComponent} from './component/landing/landing.component';
|
||||
import {RestService} from './services/rest.service';
|
||||
import {HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
|
||||
import {InterceptorService} from './services/interceptor.service';
|
||||
import {WebsocketService} from './services/websocket.service';
|
||||
import {LoggerModule, NgxLoggerLevel} from 'ngx-logger';
|
||||
import {environment} from '../environments/environment';
|
||||
import {TestSubCompComponent} from './component/testsubcomp/test-sub-comp.component';
|
||||
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
|
||||
import {MatFormFieldModule} from '@angular/material/form-field';
|
||||
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
|
||||
import {MatButtonModule} from '@angular/material/button';
|
||||
import {MatInputModule} from '@angular/material/input';
|
||||
import {MatSlideToggleModule} from '@angular/material/slide-toggle';
|
||||
import {MatSliderModule} from "@angular/material/slider";
|
||||
|
||||
@NgModule({
|
||||
declarations: [LandingComponent, TestSubCompComponent],
|
||||
imports: [
|
||||
ReactiveFormsModule,
|
||||
BrowserModule,
|
||||
BrowserAnimationsModule,
|
||||
AppRoutingModule,
|
||||
LoggerModule.forRoot({level: environment.log_level, serverLogLevel: NgxLoggerLevel.ERROR}),
|
||||
HttpClientModule,
|
||||
MatFormFieldModule,
|
||||
FormsModule,
|
||||
MatButtonModule,
|
||||
MatInputModule,
|
||||
MatSlideToggleModule,
|
||||
MatSliderModule
|
||||
],
|
||||
// enables injecting
|
||||
providers: [
|
||||
RestService,
|
||||
WebsocketService,
|
||||
{
|
||||
provide: HTTP_INTERCEPTORS, useClass: InterceptorService, multi: true
|
||||
},
|
||||
|
||||
],
|
||||
bootstrap: [LandingComponent]
|
||||
})
|
||||
export class AppModule {
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<h1>landing works!</h1>
|
||||
|
||||
<mat-slider min="1" max="5" step="0.5" [value]="myValue"></mat-slider>
|
||||
|
||||
|
||||
<h2>Two way data binding, passing data, triggering child event...</h2>
|
||||
<mat-form-field *ngIf="true">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Placeholder"
|
||||
matInput
|
||||
[(ngModel)]="exampleInputText"
|
||||
>
|
||||
</mat-form-field>
|
||||
|
||||
<app-test-sub-comp [message]="exampleInputText" (buttonClickedEvent)="alert()"
|
||||
[anotherInput]=""
|
||||
></app-test-sub-comp>
|
||||
|
||||
|
||||
|
||||
<h2>Hide display with *ngIf</h2>
|
||||
<mat-slide-toggle [(ngModel)]="showPar">Show paragraph</mat-slide-toggle>
|
||||
<p *ngIf="showPar">Toggle me</p>
|
||||
<app-test-sub-comp *ngIf="showPar"
|
||||
[message]="exampleInputText" (buttonClickedEvent)="alert()"
|
||||
[anotherInput]=""
|
||||
></app-test-sub-comp>
|
||||
|
||||
|
||||
|
||||
<h2>Display a test list with *ngFor</h2>
|
||||
<span *ngFor="let dict of testList">{{dict.value}}; </span>
|
||||
@@ -0,0 +1,88 @@
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {RestService} from '../../services/rest.service';
|
||||
import {WebsocketService} from '../../services/websocket.service';
|
||||
import {NGXLogger} from 'ngx-logger';
|
||||
import {Subscription} from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'app-landing',
|
||||
templateUrl: './landing.component.html',
|
||||
styleUrls: ['./landing.component.css']
|
||||
})
|
||||
export class LandingComponent implements OnInit {
|
||||
|
||||
private wsSubscription: Subscription;
|
||||
private wsMessageCounter: number;
|
||||
exampleInputText: string;
|
||||
showPar = false;
|
||||
testList = [{'value': 1}, {'value': 2}, {'value': 3}];
|
||||
myValue: any = 15;
|
||||
|
||||
constructor(
|
||||
private logger: NGXLogger,
|
||||
private restService: RestService,
|
||||
private wsService: WebsocketService
|
||||
) {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
// perform test rest call as observable
|
||||
this.observableCall();
|
||||
|
||||
// perform test rest call as promise (shorter - will only be executed exactly once)
|
||||
this.promiseCall();
|
||||
|
||||
// perform test ws send / receive
|
||||
setTimeout(() => {
|
||||
this.wsCall();
|
||||
},
|
||||
1000);
|
||||
}
|
||||
|
||||
private observableCall(): void {
|
||||
|
||||
const subscription = this.restService.testCall(1).subscribe(
|
||||
// subscribe with lambda function
|
||||
response => {
|
||||
this.logger.debug('Execute obs test call', response);
|
||||
subscription.unsubscribe();
|
||||
},
|
||||
error => {
|
||||
this.logger.error('Error while executing obs test call', error);
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private promiseCall(): void {
|
||||
this.restService.testCall(2).toPromise()
|
||||
// lambda for success
|
||||
.then(response => {
|
||||
this.logger.debug('Execute obs test call', response);
|
||||
})
|
||||
// lambda for fail
|
||||
.catch(error => {
|
||||
this.logger.error('Error while executing obs test call', error);
|
||||
});
|
||||
}
|
||||
|
||||
private wsCall(): void {
|
||||
this.wsMessageCounter = 0;
|
||||
this.wsSubscription = this.wsService.wsTestCall('Test message').message.subscribe(
|
||||
result => {
|
||||
this.logger.debug('ws call result', result);
|
||||
if (this.wsMessageCounter >= 2) {
|
||||
this.wsSubscription.unsubscribe();
|
||||
}
|
||||
},
|
||||
error => {
|
||||
this.logger.error('ws call error', error);
|
||||
this.wsSubscription.unsubscribe();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
alert() {
|
||||
alert(this.exampleInputText);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<p>{{message}} <button mat-raised-button (click)="buttonClickedEvent.emit()">Click me</button></p>
|
||||
@@ -0,0 +1,24 @@
|
||||
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-test-sub-comp',
|
||||
templateUrl: './test-sub-comp.component.html',
|
||||
styleUrls: ['./test-sub-comp.component.css']
|
||||
})
|
||||
export class TestSubCompComponent implements OnInit {
|
||||
@Input()
|
||||
message: string;
|
||||
@Input()
|
||||
anotherInput: string;
|
||||
|
||||
@Output()
|
||||
buttonClickedEvent: EventEmitter<void> = new EventEmitter<void>();
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import {Observable} from 'rxjs';
|
||||
|
||||
export interface WSEvents {
|
||||
message: Observable<Event>;
|
||||
error: Observable<Event>;
|
||||
close: Observable<Event>;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Observable} from 'rxjs';
|
||||
import {tap} from 'rxjs/operators';
|
||||
import {NGXLogger} from 'ngx-logger';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
/**
|
||||
* The InterceptorService intercepts EVERY http event.
|
||||
*/
|
||||
export class InterceptorService implements HttpInterceptor {
|
||||
|
||||
constructor(private logger: NGXLogger) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercepts every HTTP request and for example adds a token
|
||||
* @param req http request
|
||||
* @param next http response handler
|
||||
*/
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
// TODO get the real token
|
||||
const token = 'A_TOKEN';
|
||||
|
||||
// the original request is immutable, so we need to clone it
|
||||
req = req.clone({
|
||||
setHeaders: {
|
||||
// FIXME e.g. if Bearer is used
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
this.logger.debug('Interceptor works');
|
||||
|
||||
// pipe the response observable
|
||||
return next.handle(req).pipe(
|
||||
// perform a side effect for every emission on the source Observable
|
||||
// return an Observable that is identical to the source
|
||||
tap(event => {
|
||||
// check if it is the response message
|
||||
if (event instanceof HttpResponse) {
|
||||
// TODO so something if necessary
|
||||
}
|
||||
}, error => {
|
||||
// TODO handle error here
|
||||
this.logger.error('HTTP Response interception error', error);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {NGXLogger} from 'ngx-logger';
|
||||
import {environment} from '../../environments/environment';
|
||||
import {Observable} from 'rxjs';
|
||||
|
||||
@Injectable()
|
||||
export class RestService {
|
||||
private currentLocation = 'http://' + environment.location + ':' + environment.port + '/';
|
||||
|
||||
constructor(
|
||||
private logger: NGXLogger,
|
||||
private http: HttpClient
|
||||
) {
|
||||
}
|
||||
|
||||
testCall(i: number): Observable<string> {
|
||||
const url = this.currentLocation + 'test/';
|
||||
this.logger.debug('Performing ' + i + '. test rest call on', url);
|
||||
return this.http.get<string>(url);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {NGXLogger} from 'ngx-logger';
|
||||
import {fromEvent, interval, Observable} from 'rxjs';
|
||||
|
||||
import {environment} from '../../environments/environment';
|
||||
import {WSEvents} from '../interfaces/interface';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class WebsocketService {
|
||||
private wsEndpoint = environment.ws_location + ':' + environment.ws_port + '/test-ws-endpoint/';
|
||||
private readonly ws: WebSocket;
|
||||
private wsEvents: WSEvents;
|
||||
|
||||
constructor(private logger: NGXLogger) {
|
||||
this.logger.debug('Initiating ws connection on', this.wsEndpoint);
|
||||
this.ws = new WebSocket(this.wsEndpoint);
|
||||
|
||||
interval(5000).subscribe(() => {
|
||||
// continuously check if the connection is still open
|
||||
if (this.ws.readyState !== WebSocket.OPEN) {
|
||||
this.logger.error('Lost websocket connection ...', this.ws);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
wsTestCall(msg: string): WSEvents {
|
||||
this.logger.debug(
|
||||
'Performing ws test call',
|
||||
'current ws ready state ==', this.ws.readyState + ',',
|
||||
'expected == 1 == open');
|
||||
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(msg);
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!this.wsEvents) {
|
||||
this.wsEvents = {
|
||||
message: fromEvent(this.ws, 'message'),
|
||||
error: fromEvent(this.ws, 'error'),
|
||||
close: fromEvent(this.ws, 'close')
|
||||
};
|
||||
}
|
||||
|
||||
return this.wsEvents;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user