什么是长轮询Ajax和的WebAPI瘦......是要杀我的服务器?和字符串比较 [英] what's the skinny on long polling with ajax and webapi...is it going to kill my server? and string comparisons

查看:191
本文介绍了什么是长轮询Ajax和的WebAPI瘦......是要杀我的服务器?和字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个非常简单的长轮询AJAX调用:

I have a very simple long polling ajax call like this:

(function poll(){
    $.ajax({ url: "myserver", success: function(data){
        //do my stuff here

    }, dataType: "json", complete: poll, timeout: 30000 });
})();

我只是拿起这个例子在今天下午,似乎工作的伟大。我用它来构建我的网页上一些HTML,它几乎是瞬间尽我可以告诉。我有点担心,虽然,这是要保持工作线程我的服务器上开放的,如果我有太大的服务器上的负载,它会完全停止。有人可以提供一些线索这一理论?在后端我有一个的WebAPI服务(.NET MVC 4)调用数据库,构建对象,然后将对象回落。它还在我看来,为了使这项工作,服务器就必须不断地调用数据库...这不可能是好的吗???

I just picked this example up this afternoon and it seems to work great. I'm using it to build out some html on my page and it's nearly instantaneous as best I can tell. I'm a little worried though that this is going to keep worker threads open on my server and that if I have too big of a load on the server, it's going to stop entirely. Can someone shed some light on this theory? On the back end I have a webapi service (.net mvc 4) that calls a database, build the object, then passes the object back down. It also seems to me that in order for this to work, the server would have to be calling the database constantly...and that can't be good right???

我的下一个问题是什么是客户的最佳方式,以确定是否需要更新我的页面的HTML?目前,我米使用JSON.stringify()把我的对象转换为字符串,再对比归结到老的字符串,如果有一个三角形,它重新写入HTML页面上的字符串。现在有没有一个整体很多对象下来,但它可能会变得非常大,我觉得这样做字符串比较可能是pretty的资源密集型的客户端上......特别是如果它这样做几乎不断。

My next question is what is the best way on the client to determine if I need to update the html on my page? Currently I"m using JSON.stringify() to turn my object into a string and comparing the string that comes down to the old string and if there's a delta, it re-writes the html on the page. right now there's not a whole lot in the object coming down, but it could potentially get very large and I think doing that string comparison could be pretty resource intensive on the client...especially if it's doing it nearly constantly.

底线对我来说是这样的:我不能确定完全知道如何长轮询工作我只是用Google搜索了一下,发现上面的例子中code和实现它,并在表面上,它的真棒我只是害怕。这是怎么回事陷入困境下来(在服务器上)和我比较旧的结果,以新的方式将陷入thigns下(客户端)。

bottom line for me is this: I"m not sure exactly sure how long polling works. I just googled it and found the above sample code and implemented it and, on the surface, it's awesome. I just fear that it's going to bog things down (on the server) and my way of comparing old results to new is going to bog thigns down (on the client).

您可以提供的任何及所有信息,大大AP preciated。

any and all information you can provide is greatly appreciated.

TIA。

推荐答案

我agreee与SLaks - 即使用SignalR,如果你需要实时的网络使用的WebAPI http://www.asp.net/signalr 。长轮询是很难落实好,让别人去处理这些复杂性,即使用SignalR(对的WebAPI自然的选择)或彗星。

I agreee with SLaks - i.e. use SignalR if you need realtime web with WebApi http://www.asp.net/signalr. Long polling is difficult to implement well, let someone else handle that complexity i.e. use SignalR (natural choice for WebApi) or Comet.

SignalR尝试3其他形式的通讯诉诸长轮询,网络插座,发送服务器事件,永远框架(在此之前,)。

SignalR attempts 3 other forms of communication before resorting to long polling, web sockets, server sent events and forever frame (here).

在某些情况下,你可能会用简单的投票更即打每一秒左右更新......看看文章。但这里是报价:

In some circumstances you may be better of with simple polling i.e. a hit every second or so to update... take a look at this article. But here is a quote:

当你有很高的消息量,长轮询没有提供任何实质性的   性能改进了传统的轮询。事实上,它可能会更糟,   因为长轮询可能会失控成   即时民调节流,连续循环。

when you have a high message volume, long-polling does not provide any substantial performance improvements over traditional polling. In fact, it could be worse, because the long-polling might spin out of control into an unthrottled, continuous loop of immediate polls.

令人担心的是,与在网页上任何显著装入30秒阿贾克斯查询的可以最终会被自己的拒绝服务攻击。

The fear is that with any significant load on your web page your 30 second ajax query could end up being your own denial of service attack.

即使贝叶(的 CometD )将诉诸简单的投票,如果负载得到太多:

Even Bayeux (CometD) will resort to simple polling if the load gets too much:

增加服务器负载和资源耗尽通过使用解决   重新连接和区间意见领域节流的客户,这在   在最坏情况下退化到传统的轮询行为。

Increased server load and resource starvation are addressed by using the reconnect and interval advice fields to throttle clients, which in the worst-case degenerate to traditional polling behaviour.

至于你问题的第二部分。

如果您使用的是长轮询那么你的服务器最好应该只返回一个更新,如果事情确实发生了变化。因此您的UI或许应该信任的反应,假设一个应答装置新的数据。这同样适用于任何服务器推送式的方法。

If you are using long polling then your server should ideally only be returning an update if something actually has changed thus your UI should probably "trust" the response and assume that a response means new data. The same goes for any of the Server Push type approaches.

如果您是对简单的投票回落移动的 pullmethod 的,那么你可以使用内置的HTTP方法使用检测更新了If-Modified-Since头这将让你返回一个304未修改,所以服务器将检查对象的时间戳和仅返回200,如果它已经从上次的请求修改的一个对象。

If you did move back down towards simple polling pullmethod then you can use the inbuilt http methods for detecting an update using the If-Modified-Since header which would allow you to return a 304 Not Modified, so the server would check the timestamp of an object and only return a 200 with an object if it had been modified since the last request.

这篇关于什么是长轮询Ajax和的WebAPI瘦......是要杀我的服务器?和字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆