Skip to content

Commit

Permalink
Modified retrival of backend url (#4)
Browse files Browse the repository at this point in the history
- migrated url to use configuration file
- started changing Dockerfile to make sure the config exists
  • Loading branch information
J4yTr1n1ty authored Nov 14, 2024
1 parent 21e0b90 commit c20fa52
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
3 changes: 3 additions & 0 deletions public/assets/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"serverEndpoint": "http://localhost:5200"
}
37 changes: 21 additions & 16 deletions src/app/overlay/overlay.component.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { TrackerComponent } from '../tracker/tracker.component';
import { ActivatedRoute } from '@angular/router';
import { SocketService } from '../services/SocketService';

import { AfterViewInit, Component, OnInit, ViewChild } from "@angular/core";
import { TrackerComponent } from "../tracker/tracker.component";
import { ActivatedRoute } from "@angular/router";
import { SocketService } from "../services/SocketService";
import { Config } from "../shared/config";

@Component({
selector: 'app-overlay',
templateUrl: './overlay.component.html',
styleUrls: ['./overlay.component.scss']
selector: "app-overlay",
templateUrl: "./overlay.component.html",
styleUrls: ["./overlay.component.scss"],
})
export class OverlayComponent implements OnInit, AfterViewInit {

@ViewChild(TrackerComponent) trackerComponent!: TrackerComponent;
groupCode: string = "UNKNOWN";
socketService!: SocketService;

constructor(private route: ActivatedRoute) {
this.route.queryParams.subscribe(params => {
this.groupCode = params['groupCode']?.toUpperCase() || "UNKNOWN";
constructor(
private route: ActivatedRoute,
private config: Config,
) {
this.route.queryParams.subscribe((params) => {
this.groupCode = params["groupCode"]?.toUpperCase() || "UNKNOWN";
console.log(`Requested group code is ${this.groupCode}`);
});
}

ngOnInit(): void {
const siteUrl = window.location.hostname;
this.socketService = new SocketService(`https://${siteUrl}:5200`, this.groupCode);
this.socketService = new SocketService(
this.config.serverEndpoint,
this.groupCode,
);
}

ngAfterViewInit(): void {
this.socketService.subscribe((data: any) => {this.trackerComponent.updateMatch(data)});
this.socketService.subscribe((data: any) => {
this.trackerComponent.updateMatch(data);
});
}

}
18 changes: 12 additions & 6 deletions src/app/services/SocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ export class SocketService {
this.groupCode = groupCode;
this.socketEndpoint = socketEndpoint;

this.socket = io.connect(this.socketEndpoint, { autoConnect: true, reconnection: true });
this.socket = io.connect(this.socketEndpoint, {
autoConnect: true,
reconnection: true,
});

this.socket.once("logon_success", () => { console.log("Logged on successfully") });
this.socket.once("logon_success", () => {
console.log("Logged on successfully");
});

//registering main data handler
this.socket.on("match_data", (data: string) => {
this.subscribers.forEach(e => e(JSON.parse(data)));
this.subscribers.forEach((e) => e(JSON.parse(data)));
});

//setting up reconnection attempt handler
this.socket.io.on("reconnect_attempt", (attempt: number) => {
console.log(`Connection lost, attempting to reconnect to server (Attempt: ${attempt})`);
console.log(
`Connection lost, attempting to reconnect to server (Attempt: ${attempt})`,
);
});

//setting up reconnection handler
Expand All @@ -37,5 +44,4 @@ export class SocketService {
subscribe(handler: Function) {
this.subscribers.push(handler);
}

}
}
3 changes: 3 additions & 0 deletions src/app/shared/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Config {
serverEndpoint!: string;
}
24 changes: 15 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from "@angular/core";
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { AppModule } from "./app/app.module";
import { environment } from "./environments/environment";
import { Config } from "./app/shared/config";

if (environment.production) {
enableProdMode();
}
fetch("/assets/config/config.json").then(async (res) => {
const configuration: Config = await res.json();

platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
if (environment.production) {
enableProdMode();
}

platformBrowserDynamic([{ provide: Config, useValue: configuration }])
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
});

0 comments on commit c20fa52

Please sign in to comment.