ajax联系人数,setInterval定时调用ajax实现在线人数统计
index頁(yè)面把onLineCount.jsp給include進(jìn)來(lái),onLineCount.jsp每隔一段時(shí)間刷新一次從數(shù)據(jù)庫(kù)中統(tǒng)計(jì)數(shù)據(jù),很顯然這個(gè)方法非常的錯(cuò)誤,onLineCount.jsp每次刷新的同時(shí)index整個(gè)頁(yè)面也要刷新,首頁(yè)數(shù)據(jù)量大的情況下肯定不行。而且還出現(xiàn)了一個(gè)我更郁悶的問(wèn)題
:用戶a登錄后頁(yè)面顯示a的相關(guān)信息,用戶b
登錄后應(yīng)該顯示b的相關(guān)信息,如果把刷新的間隔時(shí)間設(shè)的短假如10秒鐘,那么在服務(wù)器反映慢的情況下,如果a
已經(jīng)登錄并顯示a的內(nèi)容,但是剛好在b登錄的時(shí)候,a頁(yè)面正好刷新,等b登陸成功后b的頁(yè)面顯示b的信息,但這個(gè)時(shí)候a的頁(yè)面也顯示了b的信息,如果我把這個(gè)間隔的時(shí)間設(shè)長(zhǎng),加入1分鐘,即使服務(wù)器再慢1分鐘應(yīng)該反映的過(guò)來(lái)吧,這樣就沒(méi)有出現(xiàn)亂套的問(wèn)題,如果間隔時(shí)間為10秒,服務(wù)器反映夠快,也不會(huì)出現(xiàn)亂套的情況。
于是還得想辦法,ajax以前經(jīng)常聽(tīng)說(shuō),實(shí)際中也沒(méi)用過(guò)那么就用這個(gè)試一下,結(jié)果發(fā)現(xiàn)還真的像傳說(shuō)中的那么好用
Count.jsp
language="java" %>
import="cn.sh.guanghua.mediamex.common.CountServlet"%>
import="cn.sh.guanghua.mediamex.db.dataunit.MonitorServer"%>
import="cn.sh.guanghua.mediamex.common.Db"%>
import="java.util.List"%>
import="cn.sh.guanghua.monitor.SnmpValueObject"%>
import="java.util.Hashtable"%>
import="cn.sh.guanghua.mediamex.common.OnlineCounter"%>
long? monitorServer_Id=1;
MonitorServer monitorServer=(MonitorServer)
Db.monitorServer().getObject(monitorServer_Id);
String strIp;
String strCommunity;
long port;
long type;
long timeout;
long version;
strIp=monitorServer.getMonitorServerIp();
strCommunity=monitorServer.getMonitorServerCommunity();
port=monitorServer.getMonitorServerSnmpPort();
type=monitorServer.getMonitorServerType();
timeout=monitorServer.getMonitorServerTimeout();
version=monitorServer.getMonitorServerVersion();
List
result=Db.monitorItem().getSnmpValue(type,port,strIp,strCommunity,version,timeout);
SnmpValueObject snmp=null;
//onlineUsercount
// int
onlineUserCount=OnlineCounter.getOnline();
int
onlineUserCount=Db.sivaUsers().geOnlineCount();
%>
var
getOnline=setInterval("send_request()",2000);
var http_request = false;
function send_request()
{//初始化、指定處理函數(shù)、發(fā)送請(qǐng)求的函數(shù)
http_request = false;
//開(kāi)始初始化XMLHttpRequest對(duì)象
if(window.XMLHttpRequest) { //Mozilla
瀏覽器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{//設(shè)置MiME類別
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { //
IE瀏覽器
try {
http_request = new
ActiveXObject("Msxml2.XMLHTTP");
} catch (e)
{
try {
http_request = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (e)
{}
}
}
if (!http_request) { //
異常,創(chuàng)建對(duì)象實(shí)例失敗
window.alert("不能創(chuàng)建XMLHttpRequest對(duì)象實(shí)例.");
return false;
}
http_request.onreadystatechange
= processRequest;
//
確定發(fā)送請(qǐng)求的方式和URL以及是否同步執(zhí)行下段代碼
http_request.open("post",
'getCount.jsp',
true);
http_request.send(null);
}
// 處理返回信息的函數(shù)
function processRequest()
{
if
(http_request.readyState == 4) { // 判斷對(duì)象狀態(tài)
if (http_request.status == 200) { //
信息已經(jīng)成功返回,開(kāi)始處理信息
//alert(http_request.responseText);
onlineCount.innerHTML=http_request.responseText;
} else { //頁(yè)面不正常
alert("您所請(qǐng)求的頁(yè)面有異常。");
}
}
}
文章轉(zhuǎn)載請(qǐng)注明來(lái)源。新輝網(wǎng)主頁(yè) > 網(wǎng)頁(yè)基礎(chǔ) > Javascript/Ajax >
標(biāo)題:setInterval定時(shí)調(diào)用ajax實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)
地址:http://www.a55.com.cn/a/1662.html
關(guān)鍵詞:setInterval定時(shí)調(diào)用ajax實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)_無(wú)夢(mèng)
總結(jié)
以上是生活随笔為你收集整理的ajax联系人数,setInterval定时调用ajax实现在线人数统计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 空之轨迹手游服务器维护,《空之轨迹》手游
- 下一篇: 云服务器变成多个挂机宝销售,云服务器变成