ASP.NET - AJAX/JQUERY 的严重连接问题

Posted

技术标签:

【中文标题】ASP.NET - AJAX/JQUERY 的严重连接问题【英文标题】:ASP.NET - Heavy connection problem with AJAX/JQUERY 【发布时间】:2011-04-02 00:52:46 【问题描述】:

我编写了一个更新脚本,它每秒使用服务器端信息更新几个 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'",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                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 分钟内崩溃,遗憾的是只有一个客户端打开。因此,每当更多的客户同时使用该脚本时,我就会遇到真正的问题。

【问题讨论】:

你知道彗星编程风格吗? en.wikipedia.org/wiki/Comet_(programming)。也许这是你需要做的,而不是每秒都请求。 从来没有听说过,你可能有你用过的好链接或什么?顺便说一句,不要让变量名和其他看起来很奇怪的东西看起来很奇怪,它只是为了测试所以没有代码清理等。 【参考方案1】:

由于您想要实时流式传输数据,您可以尝试使用 WebSync 来实现 Comet。您可以压缩更多性能,而且它的可扩展性更强。

【讨论】:

嗯,还有彗星,我真的需要去看看这个,谢谢阿里托斯和亚历山大。如果其他人都得到了他可以帮助我的东西,请这样做。 Websync 确实有一些不错的演示,表明它运行良好。谢谢,虽然 websync 看起来很贵。是否可以用彗星自己制作这些东西?就像推过 1 或 2 个变量一样? 嗯,你也可以试试 lightstreamer。 lightstreamer.com 不过,我没有在任何场景中使用过 Comet——我只是把它当作一个概念来了解。有许多非 .NET 实现 - 如果这适用于您的场景。 Dojo 中还有一些可用的绑定 - infrequently.org/2006/03/comet-low-latency-data-for-the-browser 感谢 Alexander,我正在查看所有链接以了解最适合我们的内容。并且在预算之内^^【参考方案2】:

关于彗星技术。

这是一个类似的问题和答案。 Pushing messages to clients from a server-side application?

Comet 不是每秒都调用一次服务器,它只是打开一个从服务器端保持打开状态的连接,当服务器有东西可以重播它的重播时,然后关闭连接,等等......

http://en.wikipedia.org/wiki/Comet_(programming))

http://www.frozenmountain.com/websync/

http://www.aaronlerch.com/blog/2007/07/08/creating-comet-applications-with-aspnet/

【讨论】:

谢谢,我将查看所有链接,我真的很高兴你在这里帮助了我。这个问题已经有两个多月了..

以上是关于ASP.NET - AJAX/JQUERY 的严重连接问题的主要内容,如果未能解决你的问题,请参考以下文章

ASP.net MVC Core Razor 页面和 Ajax JQuery

我应该学习 ASP.NET AJAX、jQuery 还是两者兼而有之? [关闭]

ASP.Net C#MCV - 将值从Ajax Jquery传递给Controller

asp.net core mvc 异步表单(Ajax.BeginForm)

如何使用 AJAX JQuery ASP .NET MVC 4 将 FormCollection 和文件传递给控制器

在后端 ASP.NET MVC (MEF) 上发布/绑定多个表单