简体   繁体   English

如何在没有轮询的情况下通知我的JS客户端?

[英]How to notify my JS client without polling?

Context: From my javascript web UI, I launch a long-running (several minutes) operation that is conducted on the .NET2.0 backend. 上下文:在我的javascript Web UI中,我启动了在.NET2.0后端进行的长时间运行(几分钟)操作。 The call returns immediately with operation ID while the long-running operation run parallel. 当长时间运行并行运行时,调用立即返回操作ID。 The operations are not CPU intensive, but conduct slow network calls. 这些操作不是CPU密集型的,而是进行慢速网络呼叫。 Once the operation is completed, I want to see the results in the web UI. 操作完成后,我希望在Web UI中看到结果。

Question: How can I notify the client when the job is done? 问题:如何在作业完成后通知客户?

Options I have considered: 我考虑的选项:

Option 1 : I launch the long-running operation asynchronously directly from JS and I expect the return value to be the endresult instead of an operation ID. 选项1 :我直接从JS异步启动长时间运行的操作,我希望返回值是endresult而不是操作ID。 My AJAX library takes care of everything, and life looks very easy and neat. 我的AJAX库可以处理所有事情,生活看起来非常简单和整洁。 The problem is that on the server side the thread is a ThreadPool thread which I now lock up for several minutes . 问题是在服务器端,线程是一个ThreadPool线程,我现在锁定几分钟 You do not need too many long-running parallel requests to cause the ThreadPool to starve and bring the entire server to it knees even if there is sufficient processing power. 即使有足够的处理能力,您也不需要太多长时间运行的并行请求来导致ThreadPool饿死并使整个服务器陷入困境。

Option 2 : With the operation ID, I start to poll the server if the operation is completed. 选项2 :使用操作ID,如果操作完成,我将开始轮询服务器。 However, this is a denial of service attack against my own server. 但是,这是针对我自己的服务器的拒绝服务攻击。 Furthermore, there must be fair ajax solution to it. 此外,必须有公平的ajax解决方案。 This is not a unique problem. 这不是一个独特的问题。

Polling, if done properly, will not have a noticeable effect on your server load. 轮询,如果操作正确,将不会对您的服务器负载产生明显影响。 You should customize the interval to best cover the specifics of your server job - eg you don't poll the server for at least 2 minutes if you know that the operation usually takes longer. 您应该自定义间隔以最好地覆盖服务器作业的细节 - 例如,如果您知道操作通常需要更长时间,则不会轮询服务器至少2分钟。 Even after that you choose a big enough interval - eg 10 sec. 即使在那之后你选择了足够大的间隔 - 例如10秒。 Using a web service request or a custom handler will minimize the traffic and the server time needed to process the request. 使用Web服务请求或自定义处理程序可以最大限度地减少处理请求所需的流量和服务器时间。

Check out WebSync , a comet implementation for .NET. 查看WebSync ,一个.NET的彗星实现。 Should be exactly what you're looking for. 应该是你正在寻找的。

Perhaps reverse ajax is useful if you wont have too many concurrent clients. 如果您没有太多的并发客户端,也许反向ajax很有用。 This is sometimes called comet. 这有时被称为彗星。

Reverse Ajax refers to an Ajax design pattern that uses long-lived HTTP connections to enable low-latency communication between a web server and a browser. 反向Ajax是指使用长期HTTP连接的Ajax设计模式,以实现Web服务器和浏览器之间的低延迟通信。 Basically it is a way of sending data from client to server and a mechanism for pushing server data back to the browser(what you need). 基本上,它是一种从客户端向服务器发送数据的方式,以及将服务器数据推送回浏览器的机制(您需要的)。

http://en.wikipedia.org/wiki/Reverse_Ajax http://en.wikipedia.org/wiki/Reverse_Ajax

Sounds like it might suit your needs, i just used it and it worked really well. 听起来它可能适合您的需求,我只是用它而且效果非常好。

what about having hidden (or small) iframe run the long-running script and making javascript to check only the content of the iframe? 那么隐藏(或小)iframe运行长时间运行的脚本并使javascript只检查iframe的内容呢? eg script writes ID to the iframe on its end. 例如,脚本在其末尾将ID写入iframe。

Your option 2 is exactly what I've done before. 您的选项2正是我之前所做的。 Use setTimeout to limit your polling to every 30 seconds or so. 使用setTimeout将轮询限制为每30秒左右。

The ICEfaces Framework provides an AJAX Push technology. ICEfaces框架提供了AJAX推送技术。 They support Scalable Asynchronous Request Processing on various application servers like Jetty, Tomcat and Glassfish. 它们支持Jetty,Tomcat和Glassfish等各种应用服务器上的可扩展异步请求处理

While this is all Java stuff, it might help finding the right search terms for google. 虽然这都是Java的东西,但它可能有助于为谷歌找到合适的搜索词。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不轮询的情况下通知我的C#Windows应用程序网络状态的变化? - How can my C# Windows app be notified of changes in network status without polling? 实时.NET应用程序监视,无需客户端轮询 - Real-time .NET app monitoring without client polling 如何让服务器在防火墙后通知客户端 - How to let Server Notify Client Behind Firewall 如何为大约 100 个客户实现“新邮件图标”? 中央服务器轮询还是本地客户端轮询? - How to implement "new mail icon" for roughly 100 clients? Central server polling or local client polling? 如何通知表数据更改到客户端程序? - How to notify table-data change to a client program? 如何在不轮询ThreadState的情况下判断线程是否已完成执行? - How can I tell if a thread is finished executing without polling ThreadState? WCF服务器如何通知WCF客户端有关更改? (更好的解决方案然后是简单的轮询,例如Comet或长轮询) - How does a WCF server inform a WCF client about changes? (Better solution then simple polling, e.g. Comet or long polling) 在Windows Form上计时而不进行轮询? - Clock on Windows Form without polling? 如何在不丢失 PostBack 功能的情况下将我自己的 JavaScript 链接到客户端 - How do I chain my own JavaScript into the client side without losing PostBack functionality 如何在不安装Microsoft Lync的情况下在我的业务应用程序中创建自定义Lync客户端? - How to create a custom Lync client within my business app without installing Microsoft Lync?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM