socket-test-server.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // so This is just for demonstaratin purpose.
  2. /* App logic will accept a request and return responses via subjects. Whether or not
  3. whose request it belongs to, would be presumed to be handled by the service */
  4. import { Observable, Subject } from "rxjs";
  5. import { SocketService } from "./socket.service";
  6. import { BaseMessage } from "../../dependencies/logging/services/logging-service";
  7. import { prepareResponseMessages } from "../../services/utility/prepareFISmessage";
  8. let incomingRequest = new Observable<BaseMessage>()
  9. let outGoingResponses = new Subject<BaseMessage>()
  10. let socketService = new SocketService(outGoingResponses)
  11. incomingRequest = socketService.getIncomingRequest()
  12. socketService.setUpConnection()
  13. incomingRequest.subscribe((request: BaseMessage) => {
  14. returnResponse(request).subscribe({
  15. next: message => {
  16. outGoingResponses.next(message)
  17. },
  18. error: err => console.error(err),
  19. complete: () => {
  20. // no need to do anything
  21. console.log(`Application completed in providing responses for request${request.header.messageID}...`)
  22. }
  23. })
  24. })
  25. function returnResponse(request: BaseMessage): Observable<BaseMessage> {
  26. return new Observable((observer) => {
  27. prepareResponseMessages(10, 500).subscribe({
  28. next: (message: BaseMessage) => {
  29. message.header.messageID = request.header.messageID
  30. observer.next(message)
  31. },
  32. error: err => console.error(err),
  33. complete: () => {
  34. prepareResponseMessages(1, 1000).subscribe({
  35. next: message => {
  36. message.header.messageID = request.header.messageID
  37. message.header.messageName = 'Complete'
  38. observer.next(message)
  39. },
  40. complete: () => {
  41. observer.complete()
  42. }
  43. })
  44. }
  45. })
  46. })
  47. }