加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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.您可以挤出更多性能,并且它将更具可扩展性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读