concept client in category angular

appears as: client, clients, clients, client, The client
Angular Development with Typescript, Second Edition

This is an excerpt from Manning's book Angular Development with Typescript, Second Edition.

The router is responsible for managing client-side navigation, and later in this chapter we provide a high-level overview of the router. In the non-SPA world, site navigation is implemented by a series of requests to a server, which refreshes the entire page by sending the appropriate HTML documents to the browser. With SPAs, the code for rendering components is already on the client (except for the lazy-loading scenarios covered in section 4.3 of chapter 4), and you need to replace one view with another.

Check the server/build/public directory—the client’s bundles should be there.

Listing 13.8. wsservice/app.component.ts
import {Component, OnDestroy} from "@angular/core";
import {WebSocketService} from "./websocket.service";
import {Subscription} from "rxjs";

@Component({
  selector: 'app-root',
  providers: [ WebSocketService ],
  template: `<h1>Angular client for a WebSocket server</h1>
  {{messageFromServer}}<br>
  <button (click)="sendMessageToServer()">Send Message to Server</button>
  <button (click)="closeSocket()">Disconnect</button>
  <div>{{status}}</div>
  `})
export class AppComponent implements OnDestroy {

  messageFromServer: string;
  wsSubscription: Subscription;                                         #1
   status;

  constructor(private wsService: WebSocketService) {                    #2

    this.wsSubscription =
      this.wsService.createObservableSocket("ws://localhost:8085")      #3
       .subscribe(
        data => this.messageFromServer = data,                          #4
         err => console.log( 'err'),
        () =>  console.log( 'The observable stream is complete')
      );
  }

  sendMessageToServer(){
    this.status = this.wsService.sendMessage("Hello from client");      #5
   }

  closeSocket(){
    this.wsSubscription.unsubscribe();                                  #6
     this.status = 'The socket is closed';
  }

  ngOnDestroy() {
    this.closeSocket();
  }
}