ASP.NET – AJAX / JQUERY的重连接问题
发布时间:2020-12-16 06:43:35 所属栏目:asp.Net 来源:网络整理
导读:我写了一个更新文件,每秒都会通过服务器端信息更新几个div. 现在问题是它似乎有点大量使用,在谷歌浏览器我的鼠标动画甚至不断加载. 我希望有人可以告诉我如何改进这个脚本或者可能有其他解决方案. 这是我的aspx页面: %@ Page Language="C#" AutoEventWireup
|
我写了一个更新文件,每秒都会通过服务器端信息更新几个div.
现在问题是它似乎有点大量使用,在谷歌浏览器我的鼠标动画甚至不断加载. 我希望有人可以告诉我如何改进这个脚本或者可能有其他解决方案. 这是我的aspx页面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' >
<head id="Head1" runat='server'>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery.countdown.js" type="text/javascript"></script>
<title>Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript">
$(function hello() {
// function that does something exciting?
var liftOff = function () {
// ....
};
// Get a date that is some (short) time in the future
var getDeadline = function () {
var shortly = new Date();
shortly.setSeconds(shortly.getSeconds() + 5.5);
return shortly;
};
// Attach click handler to all our buttons
$("button.resetButton").click(function (event) {
$.ajax({
type: "POST",url: "WebService.asmx/HelloWorld2",data: "{}",contentType: "application/json; charset=utf-8",dataType: "json",success: function (msg) {},failure: function () { alert("Uh oh"); }
});
});
function highlightLast5() {
$.ajax({
type: "POST",url: "WebService.asmx/HelloWorld",data: "{'number':'0'}",success: function (msg) { $("div.shortly").countdown('change',{ until: msg.d }); },failure: function () { alert("Uh oh"); }
});
}
// Start all countdowns going on page load
$('div.shortly').countdown({
until: getDeadline(),onTick: highlightLast5,onExpiry: liftOff,layout: '{sn}'
});
});
</script>
<div class="mainpanel">
<div>
test
</div>
<div class="shortly" >
</div>
<button id="button" type="button" class="resetButton">
Reset
</button>
</div>
</form>
</body>
</html>
以及返回信息的webservice: using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script,using ASP.NET AJAX,uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
List<Auction> auctions;
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
auctions = (List<Auction>)Application["Auction"];
}
[WebMethod]
public string HelloWorld(int number) {
return auctions[0].seconds.ToString();
}
[WebMethod]
public void HelloWorld2()
{
auctions[0].seconds = 30;
}
}
现在您可以看到jquery脚本每秒获取数据,这是有必要的,因为它是用于实时拍卖.如果我在真实的服务器上使用这个脚本,因为我尝试了服务器因为大量使用而在2分钟内崩溃,这很遗憾只有一个客户端打开.因此,每当有更多客户端同时使用该脚本时,我就会遇到真正的问题. 解决方法
由于您需要实时数据流,您可以尝试使用
WebSync实现
Comet.您可以挤出更多性能,并且它将更具可扩展性.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – ASP.NET MVC 3解析简单的html标记/提取属
- asp.net-mvc-3 – 为什么Visual Studio不会破坏这个未处理的
- asp.net-mvc – 使用IoC在Controller中注入HttpContextBase
- 通过asp.net网页进行Web服务器监控
- 我可以在ASP.NET应用程序中禁用IP地址(或一系列地址)吗?
- asp.net-mvc – CORS在使用OWIN认证的web api中不起作用
- asp.net-mvc – 重置asp.net mvc路由而不重置应用程序
- webmatrix – ASP.NET页面中的_PageStart.cshtml与_AppStar
- asp.net-core – 使用.net核心进行Hangfire依赖注入
- asp-classic – 旧ASP中的“on error goto 0”和“error re
推荐文章
站长推荐
热点阅读
