Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2.  
  3. import { AttackService } from './attack.service';
  4. import { WebSocketService } from '../../../lib/service/websocket/websocket.service';
  5. import { WebSocketConfig } from '../../../lib/service/websocket/websocket.config';
  6.  
  7. @Component({
  8. selector: 'attack',
  9. templateUrl: './attack.component.html',
  10. styleUrls: ['./attack.component.css'],
  11. providers: [AttackService, WebSocketService]
  12. })
  13. export class AttackComponent implements OnInit {
  14. private model: Object = {
  15. url: "http://localhost",
  16. kb_per_request: 5,
  17. duration: 5
  18. };
  19.  
  20. private hasReceivedMessage: boolean = false;
  21. private totalRequests: number = 0;
  22. private payloadSize: number = 0;
  23. private payloadSizeType: string = 'KB';
  24.  
  25. constructor(private attackService: AttackService, private socket: WebSocketService) {}
  26.  
  27. ngOnInit(): void {
  28. this.socket.create(new WebSocketConfig(this.sOnOpen, this.sOnClose, this.sOnMessage, this.sOnError));
  29. }
  30.  
  31. sOnOpen(): void {
  32. console.log('WebSocket connection successfully established.');
  33. }
  34.  
  35. sOnClose(code: number): void {
  36. console.log(`WebSocket connection closed (${code}).`);
  37. }
  38.  
  39. sOnMessage(data: any): void {
  40. this.hasReceivedMessage = true;
  41.  
  42. this.totalRequests = data.total_requests;
  43. this.payloadSize = data.payload_size;
  44. this.payloadSizeType = data.payload_size_type;
  45.  
  46. console.log('====================================');
  47. console.log('Total requests: ', this.totalRequests);
  48. console.log('Payload size: ', this.payloadSize);
  49. console.log('Payload type: ', this.payloadSizeType);
  50. }
  51.  
  52. sOnError(data: any): void {
  53. console.log('WebSocket error occurred: ', data);
  54. }
  55.  
  56. submit(): void {
  57. this.attackService.attack(this.model).subscribe(
  58. res => {
  59. let data = res.json();
  60.  
  61. if (data.success) {
  62. console.log(data.message);
  63. }
  64. },
  65. err => {
  66. console.log('Error:', err);
  67. }
  68. );
  69. }
  70. }
  71.  
  72. constructor(private attackService: AttackService, private socket: WebSocketService) {
  73. this.sOnOpen = this.sOnOpen.bind(this);
  74. this.sOnClose = this.sOnClose.bind(this);
  75. this.sOnMessage = this.sOnMessage.bind(this);
  76. this.sOnError = this.sOnError.bind(this);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement