简体   繁体   English

我应该如何为ajax请求实现长轮询的C#服务器端部分?

[英]How should I implement the C# server side portion of long-polling for ajax requests?

I've got an architecture that involves browsers polling via ajax every 3 seconds for updates and I'd like to change that to long-polling . 我有一个架构,涉及浏览器每隔3秒通过ajax轮询更新,我想将其更改为长轮询

I'd like to have 1, 2.. {n} clients long-polling, waiting for updates and have something happen on the server to signal the waiting clients to return. 我想要1,2 ... {n}个客户端进行长轮询,等待更新,并在服务器上发生一些事情,以通知等待的客户端返回。 My first thought was to use an EventWaitHandle , and I can do this easily if I just want to support 1 client. 我的第一个想法是使用EventWaitHandle ,如果我只想支持1个客户端,我可以轻松地做到这一点。 I'd just have an AutoResetEvent WaitHandle that would WaitOne to block the client, maybe with a timeout, maybe not. 我只有一个AutoResetEvent WaitHandleWaitOne会阻止客户端,可能是超时,也许不是。 Either way, an AutoResetEvent will only allow me to support 1 client (since it only wakes 1 waiting thread) and I want n clients. 无论哪种方式, AutoResetEvent只允许我支持1个客户端(因为它只唤醒1个等待线程),我想要n个客户端。

I'm pretty sure I need to use a ManualResetEvent WaitHandle , but I'm not sure when to call Reset after I Set it (when waking the threads). 我很确定我需要使用ManualResetEvent WaitHandle ,但是我不确定何时在Set它之后调用Reset (当唤醒线程时)。 Should I simply Thread.Sleep some arbitrary amount in between the Set and Reset ? 我应该只是Thread.Sleep SetReset之间的任意数量?

In psuedo code, the waking logic would be 在伪代码中,唤醒逻辑将是

  • get ManualResetEventWaitHandle 获取ManualResetEventWaitHandle
  • call Set 呼叫Set
  • ensure all waiting clients have woken, while preventing new requests from blowing through 确保所有等待的客户端都已醒来,同时防止新的请求通过
  • call Reset now that all waiting clients have received their updates call现在Reset所有等待的客户端都已收到更新

Its that 3rd line that i'm having a hard time with. 它的第3行我很难过。 Currently I am tossing around the idea of having a LastTxID that the client / server maintain and potentially using 2 wait handles. 目前我正在抛弃拥有客户端/服务器维护的LastTxID并可能使用2个等待句柄的想法。 However, before I went crazy with this implementation I wanted to get feedback here to see how they would implement the waking logic. 然而,在我对这个实现感到疯狂之前,我想在这里获得反馈,看看他们将如何实现唤醒逻辑。

Edit: assume I've got the problems associated with having max concurrent users figured out, either by tweaking IIS or hosting via WCF or some other solution. 编辑:假设我已经解决了与最大并发用户相关的问题,通过调整IIS或通过WCF或其他解决方案托管。 I only want to focus on the waking logic. 我只想专注于清醒的逻辑。

One idea, in pseudo code 一个想法,在伪代码中

  • Maintain a thread-safe list of connection id's, possibly use the session id 维护一个线程安全的连接ID列表,可能使用会话ID
  • Give every connection its own AutoResetEventWaitHandle 为每个连接提供自己的AutoResetEventWaitHandle
  • In a thread safe manner, loop through those wait handles and set them when there is an update 以线程安全的方式,循环遍历那些等待句柄并在有更新时设置它们
  • on session end, in a thread safe manner, remove that connection / session id from the list 在会话结束时,以线程安全的方式从列表中删除该连接/会话ID

I'd love to get some feedback on this 我很想得到一些反馈

con's to this approach 对这种方法的看法

  • have to maintain a list of connections 必须维护一个连接列表
  • have to generate {n} wait handles for {n} clients 必须为{n}个客户端生成{n}等待句柄

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM