I'm trying to update the totalRequests, payloadSize, payloadSizeType properties every time I receive a message from the WebSocket. This works successfully, and I can confirm by the console.log's as they update correctly every log according to what's being sent from the server.
The problem is that my template is not updating to reflect these changes for some reason.
I can confirm that there is no other underlying error because when I make a test function and update those values on click, they update and the changes are indeed reflected in the template, so I've concluded that the error must be coming from the way I am passing the WS callbacks in...unless there's something else I'm unaware of.
import { Component, OnInit } from '@angular/core';
import { AttackService } from './attack.service';
import { WebSocketService } from '../../../lib/service/websocket/websocket.service';
import { WebSocketConfig } from '../../../lib/service/websocket/websocket.config';
@Component({
selector: 'attack',
templateUrl: './attack.component.html',
styleUrls: ['./attack.component.css'],
providers: [AttackService, WebSocketService]
})
export class AttackComponent implements OnInit {
private model: Object = {
url: "http://localhost",
kb_per_request: 5,
duration: 5
};
private hasReceivedMessage: boolean = false;
private totalRequests: number = 0;
private payloadSize: number = 0;
private payloadSizeType: string = 'KB';
constructor(private attackService: AttackService, private socket: WebSocketService) {}
ngOnInit(): void {
this.socket.create(new WebSocketConfig(this.sOnOpen, this.sOnClose, this.sOnMessage, this.sOnError));
}
sOnOpen(): void {
console.log('WebSocket connection successfully established.');
}
sOnClose(code: number): void {
console.log(`WebSocket connection closed (${code}).`);
}
sOnMessage(data: any): void {
this.hasReceivedMessage = true;
this.totalRequests = data.total_requests;
this.payloadSize = data.payload_size;
this.payloadSizeType = data.payload_size_type;
console.log('====================================');
console.log('Total requests: ', this.totalRequests);
console.log('Payload size: ', this.payloadSize);
console.log('Payload type: ', this.payloadSizeType);
}
sOnError(data: any): void {
console.log('WebSocket error occurred: ', data);
}
submit(): void {
this.attackService.attack(this.model).subscribe(
res => {
let data = res.json();
if (data.success) {
console.log(data.message);
}
},
err => {
console.log('Error:', err);
}
);
}
}
I thought I was passing in the methods with the wrong this context, but even when binding the correct context, the template still does not update to reflect the changes.
constructor(private attackService: AttackService, private socket: WebSocketService) {
this.sOnOpen = this.sOnOpen.bind(this);
this.sOnClose = this.sOnClose.bind(this);
this.sOnMessage = this.sOnMessage.bind(this);
this.sOnError = this.sOnError.bind(this);
}
Anyone know a solution to this problem?
UPDATE:
WebSocketService:
import { Injectable } from '@angular/core';
import { WebSocketConfig } from './websocket.config';
@Injectable()
export class WebSocketService {
private socket = null;
private uri = "ws://localhost:80/ws";
create(config: WebSocketConfig): void {
window.onload = () => {
this.socket = new WebSocket(this.uri);
this.socket.onopen = config.onOpen;
this.socket.onclose = config.onClose;
this.socket.onmessage = res => config.onMessage(JSON.parse(res.data));
this.socket.onerror = config.onError;
};
}
send(data: any): void {
this.socket.send(data);
}
}