grpc1.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Subject } from "rxjs";
  2. import { GrpcService } from "../services/grpc.service";
  3. import * as fs from 'fs'
  4. import { FisErrorHandlingService } from "../services/error.handling.service.fis";
  5. import { ReportStatus } from "../interfaces/general.interface";
  6. const messagesJSON: any = fs.readFileSync('payload.json')
  7. const errorHandlingService: FisErrorHandlingService = new FisErrorHandlingService()
  8. const gprcService: GrpcService = new GrpcService()
  9. let parsedMessages: any[] = JSON.parse(messagesJSON) // load the fake messages generated for this trial
  10. let dataMessages = stream() // Emulate messges to be sent over to target server
  11. let messageToBePublished: Subject<any> = new Subject()
  12. let statusControl: Subject<ReportStatus> = new Subject()
  13. /* For server streaming */
  14. // errorHandlingService.handleMessage(dataMessages, statusControl).subscribe((messages) => {
  15. // messageToBePublished.next(messages)
  16. // })
  17. // let server1 = 'localhost:3000'
  18. // gprcService.createGrpcInstance(server1, messageToBePublished, statusControl, { instanceType: 'server', serviceMethod: 'server streaming' })
  19. /* For bidiretional streaming*/
  20. errorHandlingService.handleMessage(dataMessages, statusControl).subscribe((messages) => {
  21. messageToBePublished.next(messages)
  22. })
  23. let server1 = 'localhost:3000'
  24. gprcService.createGrpcInstance(server1, messageToBePublished, statusControl, { instanceType: 'server', serviceMethod: 'bidirectional' })
  25. // setTimeout(() => {
  26. // gprcService.stopServer(server1).then((res) => {
  27. // gprcService.getAllGrpcServerConnectionInstance()
  28. // })
  29. // }, 3000)
  30. // setTimeout(() => {
  31. // gprcService.createGrpcServerStreamingServer(server1).then((res) => {
  32. // gprcService.getAllGrpcServerConnectionInstance()
  33. // })
  34. // }, 10000)
  35. // this is just to publish an array of fake data as a Subject
  36. function stream(): Subject<any> {
  37. let result: Subject<any> = new Subject()
  38. let messages: any[] = parsedMessages
  39. let count = 0
  40. const intervalId = setInterval(() => {
  41. result.next(messages[count]);
  42. count++;
  43. if (count >= 1000) {
  44. clearInterval(intervalId);
  45. result.complete();
  46. }
  47. }, 500)
  48. return result
  49. }