I am currently working on Angular 10 and implementing a feature that checks connectivity by sending a request to the server every 5 seconds.
The issue I'm facing is that the request does not seem to be sent out; instead, it just logs a console.warn
message every 5 seconds.
This problem only occurs when the response from the server is a status 200
. Strangely, if the response is status 404
, everything works fine. Once a status 200
response is received, no more HTTP requests are sent every 5 seconds and only the previous data is printed out.
PS: This issue is being tested on a localhost server.
Below is my code snippet:
checkConnection(): Subscription {
return interval(2000)
.subscribe(
x => {
this.httpService.testConnectivity().subscribe(
data => console.warn(`Success: ${data}`),
error => console.warn(`Error: ${error}`),
() => console.warn("Completed")
)
}
);
}
The testConnectivity
function is as follows:
testConnectivity(): Observable<any> {
if (isPlatformBrowser(this.platformId)) {
return this.http
.get(`${this.url}/api/testconnection`, { observe: 'response' })
.pipe(
catchError(this.handleError)
);
}
}