Преглед изворни кода

added websocket function so that the app can listen from the server it opens a whole new arean for multiple direction chat capability.

tags/2.037
Patrick Sun пре 4 година
родитељ
комит
cab5d9eb11
6 измењених фајлова са 72 додато и 9 уклоњено
  1. +12
    -2
      src/app/app.component.ts
  2. +2
    -0
      src/app/app.module.ts
  3. +3
    -3
      src/app/auth/auth-http-interceptor.service.ts
  4. +1
    -1
      src/app/canvas/canvas.component.ts
  5. +4
    -3
      src/app/service/auth.service.ts
  6. +50
    -0
      src/app/websocket.ts

+ 12
- 2
src/app/app.component.ts Прегледај датотеку

import { AuthService } from './service/auth.service'; import { AuthService } from './service/auth.service';
import { MenuService } from './service/menu.service'; import { MenuService } from './service/menu.service';
import {apiV1LoginResponse} from './models/api-v1-login-response'; import {apiV1LoginResponse} from './models/api-v1-login-response';
import {WebSocketService} from './websocket';


@Component({ @Component({
selector: 'app-root', selector: 'app-root',


private loginSub: Subscription; private loginSub: Subscription;


constructor(private menuService: MenuService, private authService: AuthService){
}
constructor(private menuService: MenuService, private authService: AuthService, private wsService: WebSocketService){
wsService.createObservableSocket(this.authService.apiWsUrl)
.subscribe(m => {
console.log('websocket server send this :', m);
});
}


// check menuItem has fontawesome // check menuItem has fontawesome
public menuItemHasFontawesome (item: any) : boolean { public menuItemHasFontawesome (item: any) : boolean {
); );


this.authService.AutoLogin(); this.authService.AutoLogin();

setTimeout(() => {
const specialCommand = 'send dummy string for 500 times';
this.wsService.sendMessage(specialCommand);
}, 2000);
} }


// tslint:disable-next-line:typedef // tslint:disable-next-line:typedef

+ 2
- 0
src/app/app.module.ts Прегледај датотеку

import { TransDetailsComponent } from './trans-details/trans-details.component'; import { TransDetailsComponent } from './trans-details/trans-details.component';
import { TransTailsComponent } from './trans-details/trans-tails/trans-tails.component'; import { TransTailsComponent } from './trans-details/trans-tails/trans-tails.component';
import {AuthHttpInterceptor} from './auth/auth-http-interceptor.service'; import {AuthHttpInterceptor} from './auth/auth-http-interceptor.service';
import {WebSocketService} from './websocket';






MenuService, MenuService,
AuthGuard, AuthGuard,
AuthService, AuthService,
WebSocketService,
{ {
provide: HTTP_INTERCEPTORS, provide: HTTP_INTERCEPTORS,
useClass: AuthHttpInterceptor, useClass: AuthHttpInterceptor,

+ 3
- 3
src/app/auth/auth-http-interceptor.service.ts Прегледај датотеку

}); });
return next.handle(authReq).pipe( return next.handle(authReq).pipe(
tap(event => { tap(event => {
console.log(event);
//console.log(event);
if (event.type === HttpEventType.Response){ if (event.type === HttpEventType.Response){
console.log('Response received'); console.log('Response received');
console.log(event.body);
this.auth.logout();
//console.log(event.body);
//this.auth.logout();
} }
}) })
); );

+ 1
- 1
src/app/canvas/canvas.component.ts Прегледај датотеку

} }


onButtonClick(){ onButtonClick(){
console.log("ok");
console.log("on button click default");
this.anyhttp(); this.anyhttp();
} }



+ 4
- 3
src/app/service/auth.service.ts Прегледај датотеку

@Injectable() @Injectable()
export class AuthService { export class AuthService {


public apiUrl = 'https://svr2021.lawipac.com:8080/api/v1/';
public apiWsUrl = 'wss://svr2021.lawipac.com:8080/api/v1/ws';
public loggedIn = new apiV1LoginResponse(false, '','',0 ); public loggedIn = new apiV1LoginResponse(false, '','',0 );
loginSuccess = new EventEmitter <apiV1LoginResponse>(); loginSuccess = new EventEmitter <apiV1LoginResponse>();




// tslint:disable-next-line:typedef // tslint:disable-next-line:typedef
public AutoLogin() { public AutoLogin() {
console.log("autologin");
const sfm: apiV1LoginResponse = JSON.parse(localStorage.getItem('sfm')); const sfm: apiV1LoginResponse = JSON.parse(localStorage.getItem('sfm'));
if (!sfm) { if (!sfm) {
console.log('no auto login'); console.log('no auto login');
sfm.session, sfm.session,
sfm.sessionExpire sfm.sessionExpire
); );
console.log("check variable", this.loggedIn);
} }


// tslint:disable-next-line:typedef // tslint:disable-next-line:typedef
// tslint:disable-next-line:typedef // tslint:disable-next-line:typedef
login(email: string, password: string) { login(email: string, password: string) {


this.http.post<apiV1LoginResponse>('https://svr2021.lawipac.com:8080/api/v1/login', {u: email, p: password}).subscribe(
this.http.post<apiV1LoginResponse>(this.apiUrl + 'login', {u: email, p: password}).subscribe(
responseData => { responseData => {
this.loggedIn.session = responseData['Biukop-Session']; this.loggedIn.session = responseData['Biukop-Session'];
this.loggedIn.login = responseData.login; this.loggedIn.login = responseData.login;
this.loggedIn.machineId = responseData['Biukop-Mid']; this.loggedIn.machineId = responseData['Biukop-Mid'];
this.loggedIn.sessionExpire = responseData.sessionExpire; this.loggedIn.sessionExpire = responseData.sessionExpire;
localStorage.setItem('sfm', JSON.stringify(this.loggedIn)); localStorage.setItem('sfm', JSON.stringify(this.loggedIn));
localStorage.setItem('mid', this.loggedIn.machineId);
this.loginSuccess.emit(responseData); this.loginSuccess.emit(responseData);
}, },
error => { error => {

+ 50
- 0
src/app/websocket.ts Прегледај датотеку

import { Injectable } from '@angular/core';
import {Observable, Subscriber} from 'rxjs';



@Injectable()
export class WebSocketService {

public ws: WebSocket;
private url: string;
private observer: Subscriber<string>;

public createObservableSocket(url: string): Observable<string> {
this.url = url;
const ret = new Observable<string> ( observer => {
this.observer = observer;
});
this.startWebsocket();
return ret;
}

// tslint:disable-next-line:typedef
public onMessage(event){
this.observer.next(event.data);
}

// tslint:disable-next-line:typedef
public onError(event){
console.log('on error ', event);
}

// tslint:disable-next-line:typedef
public onClose(event){
console.log('websocket closed.. reconnect in 1s ..');
setTimeout(() => { this.startWebsocket(); } , 1000);
}

// tslint:disable-next-line:typedef
public startWebsocket() {
console.log('starting websocket now..', this.url);
this.ws = new WebSocket(this.url);
this.ws.onmessage = this.onMessage.bind(this);
this.ws.onerror = this.onError.bind(this);
this.ws.onclose = this.onClose.bind(this);
}

public sendMessage(message: any): void {
this.ws.send(message);
}
}

Loading…
Откажи
Сачувај