socket-test-server.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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<any> {
  26. return new Observable((observer) => {
  27. prepareResponseMessages(20, 300).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. observer.next({ header: { messageID: request.header.messageID }, complete: true })
  36. observer.complete()
  37. }
  38. })
  39. })
  40. }