application实时统计功能的实现。。。求指教。。

来源:互联网 发布:anywhere软件源 编辑:程序博客网 时间:2024/06/05 12:52
java代码:我这里已我项目中的id作为
application的key值
ServletContext application = request.getSession().getServletContext();//直播实时人数统计功能HttpSession session =request.getSession();String sessionid=session.getId();//如果application中没有点播表id时新增mapif(application.getAttribute(trialDto.getTtrial().getId())==null){List<Map<String, Long>> sessionlist = new ArrayList<Map<String, Long>>();Map<String, Long> map = new HashMap<String, Long>();map.put(sessionid, new Date().getTime());sessionlist.add(map);application.setAttribute(trialDto.getTtrial().getId(), sessionlist);}else{//有id时判断list中map 的key是否包含当前的sessionidList<Map<String, Long>> onList =(List<Map<String, Long>>) application.getAttribute(trialDto.getTtrial().getId());Set<String> setkeyAll = new  TreeSet<String>();for(int j=0;j<onList.size();j++){Set<String> setKey = onList.get(j).keySet();setkeyAll.addAll(setKey);}if(!setkeyAll.contains(sessionid)){//mLog1.info("不包含增加");Map<String, Long> map = new HashMap<String, Long>();map.put(sessionid, new Date().getTime());onList.add(map);}//定义过期时间,设定半小时为到期时间  这个循环其实key值只有一个,为了规范这么写for(int i=0; i<onList.size();i++){Set<String> setKey = onList.get(i).keySet();for(String key:setKey){//if(onList.get(i).get(key)<(new Date().getTime()-2000)){if(onList.get(i).get(key)<(new Date().getTime()-1800000)){onList.remove(i);i--;}}}application.setAttribute(trialDto.getTtrial().getId(), onList);}List<Map<String, Long>> onListCount =(List<Map<String, Long>>) application.getAttribute(trialDto.getTtrial().getId());//实时观看点播数int onlineCount=onListCount.size();
将onlineCount传到前台使用就可以了。
关闭页面和浏览器直播数减1,java代码:
public String clearOnlineCount(){String restid="";if(contentsearch!=null){restid=contentsearch.getTcontent().getId();//mLog1.info("点播删除");}else{restid=trialDto.getTtrial().getId();//mLog1.info("直播删除");}HttpServletRequest request = ServletActionContext.getRequest();//点播实时人数统计功能HttpSession session =request.getSession();String sessionid=session.getId();ServletContext application = request.getSession().getServletContext();List<Map<String, Long>> onList =(List<Map<String, Long>>) application.getAttribute(restid);for(int i=0; i<onList.size();i++){Set<String> setKey = onList.get(i).keySet();for(String key:setKey){if(key.equals(sessionid)){onList.remove(i);i--;}}}application.setAttribute(restid, onList);mLog1.info("删除成功  "+sessionid+"  本视屏观看人数   "+onList.size());return SUCCESS;}

js关闭访问action代码:
<script type="text/javascript">    window.onbeforeunload = onbeforeunload_handler;      window.onunload = onunload_handler;      function onbeforeunload_handler(){    var tcontent='<ww:property value="#request['searchVodBySearch'][0].tcontent.id" />';    xmlHttp=GetXmlHttpObject();//  iscbrshow=iscbrshow;if (xmlHttp==null){alert ("您的浏览器不支持AJAX!");return;}xmlHttp.onreadystatechange=function(){if(xmlHttp.readyState==4 && xmlHttp.status==200){ // alert("删除成功"); }};xmlHttp.open("post","clearOnlineCount.action",true);xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlHttp.send("contentsearch.tcontent.id="+tcontent);    }    </script> 
function GetXmlHttpObject(){  var xmlHttp=null;  try    {    // Firefox, Opera 8.0+, Safari    xmlHttp=new XMLHttpRequest();    }  catch (e)    {    // Internet Explorer    try      {      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }    catch (e)      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      }    }  return xmlHttp;}



这个不知道访问量很大会怎么样。。。求指教直播平台的观看人数是怎么统计的。。。。