如何实现超快的 ASP.NET AJAX 回调/PageMethods

发布于 2024-10-16 13:59:22 字数 451 浏览 9 评论 0原文

我正在设计一个 ASP.NET Web 应用程序(.NET 4.0),它基本上有一个页面,该页面应该每 1-2 秒与后面的代码进行交互(通过 ScriptManager 或 jQuery.ajax 使用客户端回调或 PageMethods)它将托管在内联网,因此 1-2 秒的刷新率是比较合理的。

  1. 如何使页面能够及时访问后台代码中的Web服务/页面方法(例如每1秒一次)。我应该使用 javascript 计时器(我对 javascrip 不太熟悉)吗?

  2. 虽然该网站托管在 Intranet 上,但我仍然需要实施一个好的方法来达到所需的刷新率。每次交互中传输的数据量约为 1KB。 您对我的设计有何建议?(使用回调或 ScriptManager 或 jQuery.ajax,...我应该避免的任何陷阱)

谢谢。

I am designing an ASP.NET web application (.NET 4.0) which basically has a page that should interact with the code behind every 1-2 seconds (Using Client callbacks or PageMethods via ScriptManager or jQuery.ajax) It'll be hosted on an intranet, so a 1-2 second refresh rate is kind of reasonable.

  1. How can I make the page to access the web service/pagemthod in the code behind in a timeply manner (e.g. every 1 second). Should I use a javascript timer (I'm not familiar with javascrip very much)?

  2. Although the site is hosted on an intranet, but I still need to implement a good approach to reach the desired refresh rate. the amount of data being transfered is about 1KB in each interaction. What are your recommendations on my design regarding this? (using callbacks or ScriptManager or jQuery.ajax,... any pitfalls I should avoid)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦开始←不甜 2024-10-23 13:59:22

无论使用哪种方法,每 1-2 秒发送 1kb 请求都是合理的。如果您正在处理快速网络/服务器,则不执行任何操作的页面方法或 Web 服务(它们在幕后几乎相同)将在几毫秒内做出响应。

限制因素将是服务器端方法的主要内容需要多长时间才能完成(即,如果它涉及数据库或外部服务,那么这将比服务的开销更慢地减慢您的速度)。

A 1kb request every 1-2 seconds is reasonable using either approach. A page method or web service (they're nearly identical under the hood) that does nothing will respond in just a few milliseconds if you're dealing with a fast network/server.

The limiting factor will be how long the meat of your server-side method takes to complete (i.e. if it's involves a database or external service, that's going to slow you down more than the overhead of the service).

不必了 2024-10-23 13:59:22

我使用 Web 服务,因为它们速度更快。但如果您使用 UpdatePanel,Web 服务就没用了。我还要说,您不应该每 x 秒更新一次页面,而是首先询问是否有更新要做。这可以节省很多;-)

这可能是一个小例子,我没有尝试,但像这样工作过一次。是 ms ajax 版本,需要

Type.registerNamespace("myproject");

myproject.updateControl = function () {
    myproject.updateControl.initializeBase(this);
    this._xhr = null;
    this._updating = false;
    this._timer = null;
}

myproject.updateControl.prototype = {
    initialize: function () {
        myproject.updateControl.callBaseMethod(this, 'initialize');
        this.startTimer();
    },
    startTimer: function () {
        if (this._timer) clearTimeout(this._timer);
        this._timer = setInterval(Function.createDelegate(this, this._timerWork), 2000);
    },
    stopTimer: function () {
        clearTimeout(this._timer);
        this._timer = null;
    },
    _timerWork: function () {
        if (this._updating || !this._checkXhr()) return;
        this._xhr = Sys.Net.WebServiceProxy.invoke("myServicePath Or null if PageMethod", "checkForUpdate",
         false,
         null,
         Function.createDelegate(this, this._onCheckedUpdate));

    },
    _onCheckedUpdate: function (data) {
        this._xhr = null;
        if (data.needsUpdate) {
            this._update();
        }
    },
    _udpate: function () {
        if (!this._checkXhr) return;
        this._updating = true;
        this._xhr = Sys.Net.WebServiceProxy.invoke("servicepath", "updateMe", false, { param: param }, Function.createDelegate(this, this._updateSucces));
    },
    _updateSuccess: function (data) {
        alert("yeah i´m get updated");
        this._updating = false
        this._xhr = null;

    },
    _checkXhr: function () {

        if (this._xhr()) {
            if (confirm("There is an active call to the Server. If you wait to long, it may have been broken. Do you want to Abort the current call?")) {
                this._xhr.get_executor().abort();
                this._xhr = null;
                return true;
            } else {
                return false;
            }
        }

        return true;

    },
    dispose: function () {
        myproject.updateControl.callBaseMethod(this, 'dispose');
    }
}

myproject.updateControl.registerClass('myproject.updateControl', Sys.Component);

使用

$create(myproject.updateControl);

scriptmanager或

var upd = new myproject.updateControl();
upd.initialize();

I use Webservices, since they are much faster. But if you doing with UpdatePanels, Webservices are useless. Futher I would say, you shouldnt update the page every x seconds, but first ask, if there is an update to do at all. This saves a lot ;-)

This might be a little example, i didn´t try but worked once like this. Is a ms ajax version, needs scriptmanager

Type.registerNamespace("myproject");

myproject.updateControl = function () {
    myproject.updateControl.initializeBase(this);
    this._xhr = null;
    this._updating = false;
    this._timer = null;
}

myproject.updateControl.prototype = {
    initialize: function () {
        myproject.updateControl.callBaseMethod(this, 'initialize');
        this.startTimer();
    },
    startTimer: function () {
        if (this._timer) clearTimeout(this._timer);
        this._timer = setInterval(Function.createDelegate(this, this._timerWork), 2000);
    },
    stopTimer: function () {
        clearTimeout(this._timer);
        this._timer = null;
    },
    _timerWork: function () {
        if (this._updating || !this._checkXhr()) return;
        this._xhr = Sys.Net.WebServiceProxy.invoke("myServicePath Or null if PageMethod", "checkForUpdate",
         false,
         null,
         Function.createDelegate(this, this._onCheckedUpdate));

    },
    _onCheckedUpdate: function (data) {
        this._xhr = null;
        if (data.needsUpdate) {
            this._update();
        }
    },
    _udpate: function () {
        if (!this._checkXhr) return;
        this._updating = true;
        this._xhr = Sys.Net.WebServiceProxy.invoke("servicepath", "updateMe", false, { param: param }, Function.createDelegate(this, this._updateSucces));
    },
    _updateSuccess: function (data) {
        alert("yeah i´m get updated");
        this._updating = false
        this._xhr = null;

    },
    _checkXhr: function () {

        if (this._xhr()) {
            if (confirm("There is an active call to the Server. If you wait to long, it may have been broken. Do you want to Abort the current call?")) {
                this._xhr.get_executor().abort();
                this._xhr = null;
                return true;
            } else {
                return false;
            }
        }

        return true;

    },
    dispose: function () {
        myproject.updateControl.callBaseMethod(this, 'dispose');
    }
}

myproject.updateControl.registerClass('myproject.updateControl', Sys.Component);

usage

$create(myproject.updateControl);

or

var upd = new myproject.updateControl();
upd.initialize();
雨后咖啡店 2024-10-23 13:59:22

客户端的“计时器”是一个坏主意。您可以使用 setInterval(method, timespan) 强制每 n 毫秒调用一次,但是如果服务器发生备份,您就可以开始堆叠请求,并且您将开始获得无序的响应(即使在非慢速网络上)。

我建议在呼叫处理逻辑的 ajax 代码中使用 setTimeout(method, timespan) 来设置下一次呼叫。

示例(使用 jQuery):

function getStuff()
{
  $.get(
    'myurl.aspx?r=' + Math.random(), // stop caching issues
    function(data) {
      $('#myDiv').html(data);
      setTimeout(getStuff, 2000); // you might want to set this to 1900 if you need it closer to every 2 seconds  
    }
  );
}
setTimeout(getStuff, 2000); // the initial timer initialization

"Timer" on the client side is a bad idea. You can use setInterval(method, timespan) to force a call every n milliseconds, but if the server ever gets backed up, you can then start stacking requests and you'll start getting responses out of order (even on a non-slow network).

I recommend using setTimeout(method, timespan) in your ajax code in the call processing logic to setup the next call.

Example (using jQuery):

function getStuff()
{
  $.get(
    'myurl.aspx?r=' + Math.random(), // stop caching issues
    function(data) {
      $('#myDiv').html(data);
      setTimeout(getStuff, 2000); // you might want to set this to 1900 if you need it closer to every 2 seconds  
    }
  );
}
setTimeout(getStuff, 2000); // the initial timer initialization
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文