123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { interval as RxjsInterval, Subject, Subscription } from "rxjs"
- import { BehaviorSubject } from "rxjs"
- import { BufferService } from "../services/buffer.service"
- import { ConnectionState, Message } from "../interfaces/general.interface"
- import { v4 as uuidv4 } from 'uuid'
- import { error } from "console"
- console.log(`Testing for HTTP buffer service.`)
- let source: Subject<Message> = new Subject()
- let initialReport: ConnectionState = { status: 'DIRECT_PUBLISH' }
- let connectionStateSubject: BehaviorSubject<ConnectionState> = new BehaviorSubject(initialReport)
- let bufferService: BufferService = new BufferService(source, connectionStateSubject, 'test')
- const interval = RxjsInterval(1000);
- interval.subscribe({
- next: time => {
- let message = {
- id: uuidv4(),
- message: `I am to be posted`
- }
- source.next(message)
- }
- })
- bufferService.getMessages().subscribe({
- next: message => {
-
- fetch('http://localhost:9999/data', {
- method: 'POST',
- body: JSON.stringify(message),
- headers: {
- "Content-type": "application/json; charset=UTF-8"
- }
- }).then((response) => {
- console.log(`sending ${message.id}`)
- console.log(response.status)
- connectionStateSubject.next({ status: 'DIRECT_PUBLISH' })
- }).catch((error) => {
- console.error(error)
- connectionStateSubject.next({ status: 'BUFFER' })
- periodicCheck()
- })
- }
- })
- function periodicCheck() {
- let timer = RxjsInterval(1000).subscribe({
- next: everySecond => {
- fetch('http://localhost:9999/', {
- method: 'GET',
- headers: {
- "Content-type": "application/json; charset=UTF-8"
- }
- }).then((response) => {
- if (response.ok) {
- connectionStateSubject.next({ status: 'DIRECT_PUBLISH' })
- timer.unsubscribe()
- } else {
- connectionStateSubject.next({ status: 'BUFFER' })
- }
- }).catch((error) => {
- connectionStateSubject.next({ status: 'BUFFER' })
- })
- }
- })
- }
|