diff --git a/bulletin-board-server-frontend/package.json b/bulletin-board-server-frontend/package.json index eff5461..f69f8de 100644 --- a/bulletin-board-server-frontend/package.json +++ b/bulletin-board-server-frontend/package.json @@ -4,7 +4,7 @@ "license": "MIT", "scripts": { "ng": "ng", - "start": "ng serve", + "start": "ng serve --proxy-config proxy.conf.json", "build": "ng build", "test": "ng test", "lint": "ng lint", @@ -23,7 +23,7 @@ "@angular/router": "^4.0.0", "@types/long": "^3.0.31", "core-js": "^2.4.1", - "protobuf.js": "^6.8.0", + "protobufjs": "^6.8.0", "rxjs": "^5.1.0", "zone.js": "^0.8.4" }, diff --git a/bulletin-board-server-frontend/proxy.conf.json b/bulletin-board-server-frontend/proxy.conf.json new file mode 100644 index 0000000..96b7df7 --- /dev/null +++ b/bulletin-board-server-frontend/proxy.conf.json @@ -0,0 +1,6 @@ +{ + "/bbserver" : { + "target" : "http://localhost:8081/", + "secure" : false + } +} diff --git a/bulletin-board-server-frontend/src/app/app.component.ts b/bulletin-board-server-frontend/src/app/app.component.ts index adff3f3..07efb8f 100644 --- a/bulletin-board-server-frontend/src/app/app.component.ts +++ b/bulletin-board-server-frontend/src/app/app.component.ts @@ -1,6 +1,6 @@ import {Component, OnInit} from '@angular/core'; -import {BBService} from "./bb.service"; // respectively "./node_modules/protobufjs" - +import {IntervalObservable} from "rxjs/observable/IntervalObservable"; +import {BBService} from "./bb.service"; @Component({ selector: 'app-root', @@ -15,14 +15,39 @@ export class AppComponent implements OnInit { status = 'Not connected'; errorMessage = ''; + private alive: boolean; // used to unsubscribe from the IntervalObservable + // when OnDestroy is called. + constructor(private bbservice : BBService) { } ngOnInit(): void { + + // Get initial status this.bbservice.getStatus().subscribe( - status => this.status = status.name, + status => { this.status = status.name }, error => this.errorMessage = error ); + + this.alive = true; + // Get new status every second + let o = IntervalObservable.create(1000) + //.takeWhile(() => this.alive) // only fires when component is alive + .subscribe(n => { + if (!this.alive) { + o.unsubscribe(); + return; + } + this.bbservice.getStatus().subscribe( + status => this.status = status.name + " " + n, + error => this.errorMessage = error + ); + }); + } + + + ngOnDestroy(){ + this.alive = false; // switches your IntervalObservable off } } diff --git a/bulletin-board-server-frontend/src/app/bb.service.ts b/bulletin-board-server-frontend/src/app/bb.service.ts index 9e678f0..6de878c 100644 --- a/bulletin-board-server-frontend/src/app/bb.service.ts +++ b/bulletin-board-server-frontend/src/app/bb.service.ts @@ -11,7 +11,7 @@ import 'rxjs/add/operator/map'; @Injectable() export class BBService { private headers = new Headers({'Content-Type': 'application/x-protobuf'}); - private bbUrl = 'http://localhost:8081/bbserver'; // URL to web api + private bbUrl = '/bbserver'; // URL to web api private statusPath = '/status'; constructor(private http: Http) { } diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java index 6c8020e..baa9fe0 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java @@ -56,15 +56,15 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL String dbType = servletContext.getInitParameter("dbType"); String dbName = servletContext.getInitParameter("dbName"); - if ("SQLite".equals(dbType)){ + if ("SQLite".equalsIgnoreCase(dbType)){ bulletinBoard = new BulletinBoardSQLServer(new SQLiteQueryProvider(dbName)); - } else if ("H2".equals(dbType)) { + } else if ("H2".equalsIgnoreCase(dbType)) { bulletinBoard = new BulletinBoardSQLServer(new H2QueryProvider(dbName)); - } else if ("MySQL".equals(dbType)) { + } else if ("MySQL".equalsIgnoreCase(dbType)) { String dbAddress = servletContext.getInitParameter("dbAddress"); int dbPort = Integer.parseInt(servletContext.getInitParameter("dbPort")); @@ -93,7 +93,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL @Consumes(MEDIATYPE_PROTOBUF) @Produces(MEDIATYPE_PROTOBUF) public ServerStatus getStatus(@Context HttpServletResponse response) throws CommunicationException { - response.setHeader("Access-Control-Allow-Origin", "*"); // TODO: Remove + // response.setHeader("Access-Control-Allow-Origin", "*"); // TODO: Remove return ServerStatus.newBuilder() .setName("Hello") .build();