Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import { EventEmitter, Injectable } from '@angular/core';
  2. import { HubConnection,HubConnectionBuilder, IStreamResult } from '@aspnet/signalr'
  3.  
  4.  
  5. @Injectable()
  6. export class stockSignalRService {
  7. connectionEstablished = new EventEmitter<Boolean>();
  8. marketOpened = new EventEmitter<Boolean>();
  9. marketClosed = new EventEmitter<Boolean>();
  10.  
  11. private connectionIsEstablished = false;
  12. private _stockHubConnection: HubConnection;
  13.  
  14.  
  15. constructor() {
  16. this.createConnection();
  17. this.registerOnServerEvents();
  18. this.startConnection();
  19. }
  20.  
  21. private createConnection() {
  22. this._stockHubConnection = new HubConnectionBuilder()
  23. .withUrl('/stock')
  24. .build();
  25. }
  26.  
  27. private startConnection(): void {
  28. this._stockHubConnection
  29. .start()
  30. .then(() => {
  31. this.connectionIsEstablished = true;
  32. console.log('stock connection started');
  33. this.connectionEstablished.emit(true);
  34. }).catch(err => {
  35. setTimeout(this.startConnection(), 5000);
  36. });
  37. }
  38.  
  39. private registerOnServerEvents(): void {
  40. this._stockHubConnection.on("marketOpened", () => {
  41. console.log("marketOpened");
  42. this.marketOpened.emit(true);
  43. });
  44.  
  45. this._stockHubConnection.on("marketClosed",() => {
  46. console.log("marketClosed");
  47. this.marketClosed.emit(true);
  48. });
  49. }
  50.  
  51. public startStreaming(): IStreamResult<any> {
  52. return this._stockHubConnection.stream("StreamStocks");
  53. }
  54.  
  55. public getAllStocks(): Promise<any> {
  56. return this._stockHubConnection.invoke("getAllStocks");
  57. }
  58.  
  59. public openMarket() {
  60. this._stockHubConnection.invoke("OpenMarket");
  61. }
  62.  
  63. public CloseMarket() {
  64. this._stockHubConnection.invoke("CloseMarket");
  65. }
  66.  
  67. public ResetMarket() {
  68. this._stockHubConnection.invoke("Reset");
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement