Terminating unused http connections that are a res

2019-08-13 14:21发布

问题:

I am developing an iOS and Android app (using ionic/angular)

I have the following situation:

I have a view that basically does this:

<div ng-repeat="monitor in monitors">
  <img ng-src = 'http://myserver.com/cgi-bin/videoFeed?id={{monitor}}' />
</div>

Each monitor is actually an IP camera and what we are doing here is transmitting live feed as continuous JPG images (multipart/mime).

What therefore happens is the img tag keeps showing new images continuously making it look like a live video feed

The problem I am facing is when we exit the view, it looks like angular still keeps the HTTP connection open and the server keeps the connection open on its side as a result. The server launches an instance of 'videoFeed' per connection, and I can see the instances don't go away even long after (15-20 minutes) I exit the view.

Is there any possible way to clear out these connections forcibly from the client?

I searched in SO for other such questions and they all talk about HTTP timeouts - which is not relevant in my case, as this is a continuously updating image feed with one image tag and the only time it needs to abort is when we exit the view.

Thanks

回答1:

I have a theory but no idea if it will work. I am guessing as long your url is pointed at image and that image is being updated the connection will stay open, or re-open when the image changes. So how about when the user leaves the state you remove the list from the dom using ng-if.

so here is how that is achieved.

In the controller:

$scope.$on('$ionicView.beforeEnter', function(){
    $scope.inView = true;
})

$scope.$on('$ionicView.beforeLeave', function(){
    $scope.inView = false;
})

In the HTML:

<div ng-repeat="monitor in monitors" ng-if="inView">
  <img ng-src = 'http://myserver.com/cgi-bin/videoFeed?id={{monitor}}' />
</div>

can you give that a try and let me know what happens.