I have this code:
this.http.get(url)
.subscribe(
data => {
},
error=>{
}
)
How I can close an HTTP connection?
If you're talking about the analogue to something like it's done in Java:
//Create connection
URL url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
...
connection.disconnect();
then you don't need to do that. Angular uses XHR browser API to make HTTP requests. This API doesn't require explicitly opening or closing the connection, it does so automatically once the request is sent and response obtained or the timeout exceeded.
Also, you don't need to explicitly unsubscribe from the observable since the observable returned by the this.http.get completes by itself. Here is the relevant sources:
export class XHRConnection implements Connection {
...
response: Observable<Response>;
constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
this.request = req;
this.response = new Observable<Response>((responseObserver: Observer<Response>) => {
...
// load event handler
const onLoad = () => {
...
const response = new Response(responseOptions);
response.ok = isSuccess(status);
if (response.ok) {
responseObserver.next(response);
responseObserver.complete(); <--------------------
return;
}
responseObserver.error(response);
};
The observable created inside XHRConnection is the observable you receive when you do http.get().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With