如何清除浏览器缓存时最大尺寸数据达到

问题描述:

我使用socket.io重流媒体直播接收大约每分钟10,000条消息,下面的代码,我设置问题Bufferlimit 2MB所以一旦其覆盖面最大尺寸我有shift()先reomve来自$scope.event的项目。但后来我看到$scope.event.length开始增加和冻结浏览器。如何清除浏览器缓存时最大尺寸数据达到

如何管理大量流媒体并删除集合BufferLimit上的项目?

有没有更好的方法来处理这个问题?

main.html中

<div class="panel-body display-logs" scroll-bottom="event" style="width:100%;"> 
       <ul style="list-style: none;"> 
        <li ng-repeat="message in event" ng-class="{lastItem: $last}"><span>{{message.value}}</span></li> 
       </ul> 
      </div> 

ctrl.js

var Bufferlimit = 1024 * 1024 * 2; 
    $scope.event = []; 
    var totalReceived = 0; 

    socket.on(searchEnv + 'Consumer', function(data) { 
     var messageSize = getBytesForBuffer(data); 
     safelyAdd({ 
      id: $scope.event.length, 
      value: data, 
      messageSize: messageSize 
     }); 
    }); 

    function safelyAdd(element) { 
     if (totalReceived > Bufferlimit && $scope.event.length) { 
      totalReceived -= $scope.event[0].messageSize; 
      $scope.event.shift(); //delete first element in $scope.event 
      console.log('totalReceivedBytes', totalReceived); 
      console.log('Length', $scope.event.length); 
     } 
     $scope.event.push(element); //then push new item.. 

    } 

    function getBytesForBuffer(str) { 
     var strBytes = str.length * 2; 
     checkLimit(strBytes); 
     return strBytes; 
    } 

function checkLimit (val){ 
     totalReceived += val; 
    } 
+1

你试过https://github.com/AngularClass/angular-websocket总是好的,有一个适当的执行angularWay – aorfevre

如果这是所有相关的代码,那么它看起来像你只当它超过了缓冲区限制更新totalReceived,但由于你不会添加收到的项目的长度,它仍然是零,你的数组只是继续增长没有限制。

我认为你只是缺少沿着线的东西:

else { totalReceived += element.length }