<script src="js/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
function GetUser() {
WebApplication1.WebService1.GetCountPerson(onSuccess, onFailed);
}
function onSuccess(result) {
$('#spcount').text(result);
}
function onFailed() {
$('#spcount').text('获取失败');
}
setInterval(GetUser, 2000);
})
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<div>
<div>当前在线总人数:<span id="spcount"></span></div>
</div>
</form>
<script type="text/javascript">
$(function () {
function GetUser() {
WebApplication1.WebService1.GetCountPerson(onSuccess, onFailed);
}
function onSuccess(result) {
$('#spcount').text(result);
}
function onFailed() {
$('#spcount').text('获取失败');
}
setInterval(GetUser, 2000);
})
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<div>
<div>当前在线总人数:<span id="spcount"></span></div>
</div>
</form>
</body>
WebService
[WebMethod]
public string GetCountPerson()
{
return Application["count"].ToString();
}
Global
protected void Application_Start(object sender, EventArgs e)
{
Application["count"] = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
int count = Convert.ToInt32(Application["count"]);
count++;
Application["count"] = count;
}