socket-test-server.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(30, 1000).subscribe({
  28. next: (message: BaseMessage) => {
  29. message.header.messageID = request.header.messageID
  30. console.log(`Generating response message for ${request.header.messageID}`)
  31. observer.next(message)
  32. },
  33. error: err => console.error(err),
  34. complete: () => {
  35. prepareResponseMessages(1, 1000).subscribe({
  36. next: message => {
  37. console.log(`Finalizing complete response for request${request.header.messageID}`)
  38. message.header.messageID = request.header.messageID
  39. message.header.messageName = 'Complete'
  40. observer.next(message)
  41. },
  42. complete: () => {
  43. observer.complete()
  44. }
  45. })
  46. }
  47. })
  48. })
  49. }