I am trying to create a web-app for a game that involves starting and joining rooms. I am using Angular and Socket.io for the project. I have components named Startpage and Lobby. Startpage contains text boxes for player name and Room ID. It also has buttons for "Start Game" and "Join Game". Clicking on the "Start Game" button triggers the startThisGame() function.
//startpage.component.ts
import { Component, OnInit, Output, NgZone } from '#angular/core';
import { Router } from '#angular/router';
import { SocketioService } from '../socketio.service';
#Component({
selector: 'app-startpage',
templateUrl: './startpage.component.html',
styleUrls: ['./startpage.component.css']
})
export class StartpageComponent implements OnInit {
constructor( private router: Router, public zone: NgZone, private socketService: SocketioService ) {}
ngOnInit() {
this.socketService.setupSocketConnection();
}
testRoomId = 'xxxx'
startThisGame() {
var playerName = (document.getElementById("nameInput") as HTMLInputElement).value;
if (playerName) {
this.testRoomId = this.socketService.startGame(playerName);
alert("In component: " + this.testRoomId)
this.zone.run(() => {this.router.navigate(['/lobby']); });
}
else{
alert("Enter a player name!")
}
}
}
The code inside socketService goes as follows.
import { Injectable } from '#angular/core';
import * as io from 'socket.io-client';
import { environment } from 'src/environments/environment';
#Injectable({
providedIn: 'root'
})
export class SocketioService {
socket;
public roomId: string;
public playerName: string;
constructor() {}
setupSocketConnection() {
this.socket = io(environment.SOCKET_ENDPOINT)
alert("Connection set-up")
}
startGame(inputPlayerName) {
this.socket.emit('startGame', {name: inputPlayerName})
this.playerName = inputPlayerName
this.socket.on('recieveRoomId', (data: any) => {
this.roomId = data.code
alert("In service startGame: " + this.roomId)
})
return this.roomId
}
getRoomId() {
alert("In service getRoomId: " + this.roomId)
return this.roomId;
}
}
The server code creates a unique room ID and passes it to the roomId variable in socketService. My first question starts here. If you notice, I have left a trail of alert messages along the way. I would expect the order of alert messages to be alert("In service startGame: " + this.roomId) and then alert("In component: " + this.testRoomId). However, I get the alert messages in the opposite order and the roomId's are undefined even though the server generates and emits a unique roomId. I do not seem to understand why this is happening.
Secondly, you can see that the startThisGame() function causes socketService to store the generated roomId and playerName inside the class and then reroutes the app to the lobby component. The lobby code goes as follows.
import { Component, OnInit, OnChanges, AfterContentInit, SimpleChanges, Input } from '#angular/core';
import { SocketioService } from '../socketio.service';
#Component({
selector: 'app-lobby',
templateUrl: './lobby.component.html',
styleUrls: ['./lobby.component.css']
})
export class LobbyComponent implements OnInit, AfterContentInit {
constructor(private socketService: SocketioService) { }
#Input() roomId: string;
ngOnInit(): void {
// alert("On init fetched")
this.socketService.setupSocketConnection()
this.roomId = this.socketService.getRoomId()
if (this.roomId) {
alert("Value obtained in init: " + this.roomId)
}
else {
alert("No value obtained inside init")
}
document.getElementById("generated_code").innerHTML = this.roomId
}
}
Here, OnInit, the socketService.getRoomId() function returns an undefined value. If I press back, go to the startpage again, enter a playerName and start a new game again, the previously generated roomId is being rendered. What am I missing here? How do I load the roomId and display the same when I am rerouted to the lobby?
What you are missing is the concept of asynchronous calls and observable.
Try:
service.ts
export class SocketioService {
socket;
public roomId: string;
public playerName: string;
private roomData$ = new BehaviorSubject<string>(null);
constructor() {}
setupSocketConnection() {
this.socket = io(environment.SOCKET_ENDPOINT)
alert("Connection set-up")
}
startGame(inputPlayerName) : Observable<string>{
this.socket.emit('startGame', {name: inputPlayerName})
this.playerName = inputPlayerName
this.socket.on('recieveRoomId', (data: any) => {
this.roomId = data.code;
this.roomData$.next(this.roomId); // <=== emitting event when the roomId is set.
alert("In service startGame: " + this.roomId)
})
return this.roomData$.asObservable(); // <== returns observable
}
getRoomId(){
alert("In service getRoomId: " + this.roomId)
return this.roomData$.asObservable(); // <== returns observable
}
}
StartpageComponent.ts
startThisGame() {
// not sure why you are getting value in this way. I can't comment because
// I dont have access to HTML code
var playerName = (document.getElementById("nameInput") as HTMLInputElement).value;
if (playerName) {
// subscribe to the observable and get the values when prepared.
// make sure to unsubscribe it in "ngOnDestroy" to avoid memory leaks
this.socketService.startGame(playerName).subscribe(data => {
this.testRoomId = data;
alert("In component: " + this.testRoomId)
// again, not sure why would you need "this.zone.run()",
// Handle input values as Angular way and you wont need these patches.
this.zone.run(() => {this.router.navigate(['/lobby']); });
});
}
else{
alert("Enter a player name!")
}
}
In LobbyComponent.ts
// You can also use RouteParam to pass RoomId rather than using Observable as I have done.
ngOnInit(): void {
this.socketService.setupSocketConnection()
// make sure to unsubscribe in ngOnDestroy
this.socketService.getRoomId().subscribe(data => {
this.roomId = data;
document.getElementById("generated_code").innerHTML = this.roomId;
})
}
I have added lot of comments to explain you why I have done those changes. I would recommend you to read about Observables and Promises in Javascript to have better understanding of async calls.
You need to wait for the values to come before you use that value. That is why your code is not running in sequence as you are expecting.
Happy learning :)
The problem here is that that every socket is asynchronous.
The reason why you are getting the alerts in reversed orer is that you wrote
if (playerName) {
this.testRoomId = this.socketService.startGame(playerName);
alert("In component: " + this.testRoomId)
this.zone.run(() => {this.router.navigate(['/lobby']); });
}
this.socket.on('recieveRoomId', (data: any) => {
this.roomId = data.code
alert("In service startGame: " + this.roomId)
})
This way the alert is emitted on the event of reciving a new room id which is asnyc. This way the the code finishes to execute the startgame function and the returns to the main function which then alerts the 'In component' alert and when the socket receives the event it alerts the dialog.
The solution what I think shouléd be the the best is to work with RxJs and put roomId into a ReplaySubject.
So modify the service to
private roomId: ReplaySubject<string>=new ReplaySubject();
startGame(inputPlayerName) {
this.socket.emit('startGame', {name: inputPlayerName})
this.playerName = inputPlayerName
this.socket.on('recieveRoomId', (data: any) => {
this.roomId.next(data.code)
alert("In service startGame: " + this.roomId)
})
}
getRoomId() {
alert("In service getRoomId: " + this.roomId)
return this.roomId.asObservable();
}
And in the component
ngOnInit(): void {
// alert("On init fetched")
this.socketService.setupSocketConnection()
this.socketService.getRoomId().subscribe((roomId)=>{
this.roomId=roomId;
//More logic here when getting a new roomId
});
}
And dont forget to call the startGame method :)
If you want to learn more about ReplaySubject check out its awesome docs.
Related
I'm new to angular and I wasn't sure how to implement synchronous api calls. I implemented async/await from a few articles I read but it still seems like the variables are undefined meaning the console is printing before even initializing the variable. I need it to be synchronous because code further down the cycle function depends on accurate variables.
I'm making a small program where people can upload their own images and it will be displayed on the stage component. I'm saving the images as a blob on a mysql database and retrieving them one at a time depending on the names provided in my nameList array variable
What am I doing wrong when calling the api via synchronous call?
stage.component.html
<div class="container">
<div class="slideshow" *ngIf="retrievedImage">
<ng-container>
<img [src]="retrievedImage"/>
<h1 *ngIf="!database_populated" style="color: red;">No Photo's to show. Please go back and upload</h1>
</ng-container>
</div>
</div>
stage.component.ts
import { HttpClient } from '#angular/common/http';
import { Component, OnInit } from '#angular/core';
import { interval } from 'rxjs';
import { ImagingService } from '../../services/imaging.service';
#Component({
selector: 'app-stage',
templateUrl: './stage.component.html',
styleUrls: ['./stage.component.css']
})
export class StageComponent implements OnInit {
constructor(private httpClient: HttpClient, private imageService: ImagingService) { }
retrieveResponse: any;
public namesList: any;
imageName: string = "eating.jpg";
base64Data: any;
retrievedImage: any = null;
currentImage = 0;
public database_populated: boolean = false;
totalImages: any;
ngOnInit(): void {
this.checkCount().then(count => {
if (count > 0 ) {
this.database_populated = true
console.log("database is populated. going to cycle")
this.cycle()
}
else {
this.database_populated = false;
}
}) }
cycle(){
console.log("entering cycle")
interval(10000).subscribe(x =>
{
// update how many images there are in the database
this.checkCount().then(data => {
this.totalImages = data
})
console.log(this.totalImages)
//update the list of image names found in the database
this.updateNamesList().then(nameList => {
this.namesList = nameList;
})
console.log(this.namesList)
if (this.currentImage == this.totalImages){
console.log("inside mod")
this.currentImage = this.currentImage % this.totalImages
}
else
{
console.log("printing pictures")
// display the Nth image in the list
this.imageName = this.namesList[this.currentImage]
// increment the image count in case there is another image added to the database
this.currentImage = this.currentImage + 1
this.getImage()
}
});
}
getImage() {
//Make a call to Sprinf Boot to get the Image Bytes.
this.httpClient.get('http://localhost:8080/halloween/get/' + this.imageName)
.subscribe(
res => {
this.retrieveResponse = res;
this.base64Data = this.retrieveResponse.picByte;
this.retrievedImage = 'data:image/jpeg;base64,' + this.base64Data;
}
);
}
async updateNamesList(){
return await this.imageService.updateNamesList()
}
async checkCount(){
return await this.imageService.checkCount()
}
}
imaging.service.ts
import { Injectable } from '#angular/core';
import { HttpClient } from '#angular/common/http';
#Injectable({
providedIn: 'root'
})
export class ImagingService {
constructor(private httpClient: HttpClient) { }
public updateNamesList() {
return this.httpClient.get('http://localhost:8080/halloween/allnames').toPromise();
}
public checkCount() {
return this.httpClient.get('http://localhost:8080/halloween/check').toPromise();
}
}
this is a snippet of the browser console errors and it shows the variables as undefined even though I place the promise prior to the console.log
Your code will not work with asynch. Here is the order of execution.
// command 1
this.checkCount().then(data => {
//command 3
this.totalImages = data
});
// command 2, totalImages will be undefined.
console.log(this.totalImages)
There is no guarantee about time at command 2, because we fetch data through network, so delay time may take few seconds.
You can await the result of checkCount to make sure we have data through rest api.:
this.totalImages = await this.checkCount();
Or you can do other things after rest api have an data.
this.checkCount().then(data => {
this.totalImages = data
doSomethingWithTotalImagesHere();
});
I'm new to this. I want to get data from Rest API. Loading data from the endpoint is ok, but I want to use it later, outside the method. For example I want to sum one of the attributes of the todos in another function. In funcion loadTodos() the first console log shows the data, but the second one shows only "undefined". How can I save the values what loadTodos() gives back and use it later?
import { Component, OnInit } from '#angular/core';
import { Router } from '#angular/router';
import { TodoDomainService } from '../services/todo-domain.service';
import { Todo } from 'app/model/todo';
#Component({
selector: 'app-todo-listing',
templateUrl: './todo-listing.component.html',
styleUrls: ['./todo-listing.component.scss']
})
export class TodoListingComponent implements OnInit {
todo: Todo;
constructor(private todoService: TodoDomainService, private router:Router) { }
public todos;
ngOnInit() {
this.loadTodos();
this.todo = new Todo();
}
private loadTodos() {
this.todoService.getTodos().subscribe(
data => { this.todos = data },
err => console.error(err),
() => console.log("todos loaded." +this.todos)
);
console.log(this.todos)
}
}
private getSum(todos) {
var sum = 0;
for(var i = 0; i < todos.length; i++){
sum += todos.price[i]}
return this.aggregatedSales;
}
console.log("todos loaded." +this.todos) will show a response because it is executed after the observable has completed.
console.log(this.todos) after your .subscribe(...) shows undefined because the observable hasn't yet finished, that is, the line data => { this.todos = data } hasn't been executed.
You are saving the data correctly for use. If you update your next called for the subscription to look like the following then the sum will execute:
// from
data => { this.todos = data }
// to
data => {
this.todos = data;
this.getSum(this.todos);
}
Here is a stackblitz example of fetching a todos array and adding up the userId values into a sum variable then displaying the value.
On my web-app written in angular I am posting data to a Database and I am displaying this data in a table on the same html. Each data record has an ID. And every time I am adding new data, the ID is going to be increased. The first input field shows the actual ID, see the screenshot below:
In my ngOnInit-method I am initialising the id and I call the function fbGetData() in order to display the data.
But now I am facing one odd problem:
Everytime I starting the application the initial value which is displayed in the ID-field is NaN.
Obviously I cannot post any data to the database because the ID is not a number. So I have to switch to another page on my application and then switch back. After that the correct ID is displayed. I also tried to move my methods from the ngOnInit-method to the constructor but this didn't help.
Somehow I think that I need to implement the methods asynchronously, but I have no idea how to do this, since I am quite new to Angular/Typscript.
I hope you guys can help me with this problem or give me any hint or idea.
I appreciate your answers!
Here is my .ts Code:
import { Component, OnInit, ViewEncapsulation } from '#angular/core';
import { Router, ActivatedRoute, Params } from '#angular/router';
import { DataService } from '../data.service';
import { Http } from '#angular/http';
import { rootRoute } from '#angular/router/src/router_module';
import { SearchNamePipe } from '../search-name.pipe';
import { LoginComponent } from '../login/login.component';
import {NavbarService} from '../navbar.service';
declare var firebase: any;
const d: Date = new Date();
#Component({
selector: 'app-business-function',
templateUrl: './business-function.component.html',
styleUrls: ['./business-function.component.css'],
encapsulation: ViewEncapsulation.None,
providers: [DataService, SearchNamePipe, LoginComponent]
})
export class BusinessFunctionComponent implements OnInit {
id;
name: String;
descr: String;
typ: String;
bprocess: String;
appsystem: String;
applications: String;
datum: String;
liste = [];
bprocessliste = [];
applicationliste = [];
appsystemliste = [];
isDesc: boolean = false;
column: String = 'Name';
direction: number;
loginName: String;
statusForm: Boolean = false;
private idlist = [];
constructor(
private dataService: DataService,
private router: Router,
private route: ActivatedRoute,
private searchName: SearchNamePipe,
private navbarService: NavbarService
) {
this.datum = Date().toString();
}
ngOnInit() {
this.navbarService.show();
firebase.database().ref().child('/AllID/').
on('child_added', (snapshot) => {
this.idlist.push(snapshot.val()
)})
this.id = this.idlist[0];
console.log("ID: "+this.id);
console.log("IDlist: "+this.idlist[0]);
this.id++;
console.log("ID: "+this.id);
this.fbGetData();
}
fbGetData() {
firebase.database().ref().child('/BFunctions/').orderByChild('CFlag').equalTo('active').
on('child_added', (snapshot) => {
//firebase.database().ref('/BFunctions/').orderByKey().on('child_added', (snapshot) => {
// alter code ... neuer Code nimmt nur die Validen mit dem X Flag
this.liste.push(snapshot.val())
});
// firebase.database().ref().child('/ID/').on('child_added', (snapshot) => {
//Bprocess DB Zugriff
firebase.database().ref().child('/BProcess/').orderByChild('CFlag').equalTo('active').
on('child_added', (snapshot) => {
this.bprocessliste.push(snapshot.val())
});
//Appsystem DB Zugriff
firebase.database().ref().child('/Appsystem/').orderByChild('CFlag').equalTo('active').
on('child_added', (snapshot) => {
this.applicationliste.push(snapshot.val())
})
//Application DB Zugriff
firebase.database().ref().child('/Application/').orderByChild('CFlag').equalTo('active').
on('child_added', (snapshot) => {
this.applicationliste.push(snapshot.val())
});
console.log(this.applicationliste);
}
You need to update the id inside your callback:
firebase.database().ref().child('/AllID/').on('child_added', (snapshot) => {
this.idlist.push(snapshot.val())
this.id = this.idlist[0];
console.log("ID: "+this.id);
console.log("IDlist: "+this.idlist[0]);
this.id++;
console.log("ID: "+this.id);
this.fbGetData();
})
Otherwise id retains it initial undefined value. This is because the call to firebase is asynchronous.
Here is what happens in your original code:
call to firebase API... wait your response
set id to this.idlist[0], which is empty (undefined)
...some time later, getting response from firebase
id does not get updated because the code in point 2. has already been executed.
Anything that you need to do when you get the result from an asynchronous call, must be executed inside the callback function.
I have an array. I am running to issues, so...
In my code, I placed the following debugging code:
console.log(this.pages);
console.log(this.pages.length);
The output in Chrome's debug window is like the following. You will see the first one shows a length: 38 but the second console.log shows 0. Why does the second one not show 38 also?
import { Injectable } from '#angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
#Injectable()
export class SitemapService {
pagesObservable: FirebaseListObservable<any[]>;
pages: any[] = [];
data: string = '';
constructor(
protected af: AngularFire,
private datePipe: DatePipe,
private urlPipe: UrlPipe
){
this.pagesObservable = this.af.database.list('/pages', {
query: {
orderByChild: 'sortOrder',
limitToLast: 100
},
preserveSnapshot: true
})
this.pagesObservable.subscribe(snapshots => {
snapshots.forEach(snapshot => {
this.pages.push(JSON.stringify(snapshot.val()));
})
})
}
getSitemapData(): string {
let urlBase = location.protocol + '//' + location.host;
console.log(this.pages);
console.log(this.pages.length);
return (this.data);
}
}
Try this one may be its work for you
alert(Object.keys(this.pages).length);
Don't do async stuff like subscribe in your constructor. You will have no way to know when it's done. new is not asynchronous. It does not wait for some some async logic in the constructor to finish before continuing. Wherever you're calling getSiteMapData from, it's almost certainly before the async stuff in the constructor has had a chance to finish. In your case, just set up the observable in your constructor.
You're very confused about how AngularFire list observables work. When you subscribe, you get the data, itself, right there. It doesn't give you snapshots that you have to forEach over and take val() of and do something with. In your case you don't need, and don't want, the preserveSnapshots option, unless you're doing something special.
import { Injectable } from '#angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
#Injectable()
export class SitemapService {
pagesObservable: FirebaseListObservable<any[]>;
pages: any[] = [];
data: string = '';
constructor(
protected af: AngularFire,
private datePipe: DatePipe,
private urlPipe: UrlPipe
){
this.pagesObservable = this.af.database.list('/pages', {
query: {
orderByChild: 'sortOrder',
limitToLast: 100
}
});
}
getSitemapData(): string {
let urlBase = location.protocol + '//' + location.host;
this.pagesObservable.subscribe(pages => {
console.log(pages);
console.log(pages.length);
});
}
}
But, you say, I want to keep pages as a property on my component. Before you decide you really want to do that, make sure you can't do the obvious:
<div *ngFor="pagesObservable | async">
which is often a better solution--let Angular do the subscribing (and unsubscribing) for you. If you really want a pages property on your component, then you could do
ngOnInit() {
this.pagesSubscription = this.pagesObservable.subscribe(pages => this.pages = pages);
}
// MAKE SURE TO DO THIS!
ngOnDestroy() {
this.pagesSubscription.unsubcribe();
}
But you won't have this.pages until some point in the future. Therefore, if you want to use it in a template, or somewhere else, you'll have to make sure it's been set:
<div *ngIf="pages">I now have the pages!!</div>
I am running two checks and then conditionally populating some data to certain routes if both conditions are met. In my room.component.html file I am using an *ngIf for this:
<div *ngIf="isLoggedIn() && isRoomRoute()" class="others">
... do some work
</div>
My room.component.ts file looks like this:
import { RouteService } from './../../data/route.service';
import { Component } from '#angular/core';
import { AuthenticationService } from './../../data/authentication.service';
import { Router } from '#angular/router';
#Component({
selector: 'app-room',
templateUrl: './room.component.html',
styleUrls: ['./room.component.less']
})
export class RoomComponent {
model: any = {};
loading = false;
username;
password;
routeUrl;
private url = 'http://localhost:5000';
constructor(private authenticationService: AuthenticationService,
private router: Router,
private routeService: RouteService,
private appComponent: AppComponent) { }
isLoggedIn() {
this.loading = true;
if (this.authenticationService.isAuthenticated()) {
return true;
}
}
isRoomRoute(routeUrl) {
if (this.routeService.isRoomRoute(this.routeUrl)) {
return true;
}
}
}
As you can see above, the second check is using a function from my routeService. That function looks like this:
isRoomRoute(routeUrl) {
if (routeUrl.includes('staff')) {
console.log('This url: ' + routeUrl + ' is a roomRoute');
return true;
} else {
console.log('This url: ' + routeUrl + ' is NOT a room route');
return false;
}
}
This urls are being tracked in my app.component, which is using the routeService in the constructor, and looks like this:
constructor(private routeService: RouteService,
private router: Router)
{
this.router.events.subscribe((route) => {
let routeUrl = route.url;
this.routeService.sendRoute(routeUrl);
this.routeService.isRoomRoute(routeUrl);
});
}
I am successfully getting the right result from my routerService's "isRoomRoute" function, but I am getting an "undefined" error when I try and pass that result to my room component, even though I though I am calling the routeService in that component. So my question is, what am I missing that's making the result "undefined" from the room component? How do I pass a boolean value from the result of the isRoomRoute() function in the routeService to my room component?