[英]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
WaitHandle
, WaitOne
会阻止客户端,可能是超时,也许不是。 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
Set
和Reset
之间的任意数量?
In psuedo code, the waking logic would be 在伪代码中,唤醒逻辑将是
ManualResetEventWaitHandle
获取ManualResetEventWaitHandle
Set
呼叫Set
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 一个想法,在伪代码中
I'd love to get some feedback on this 我很想得到一些反馈
con's to this approach 对这种方法的看法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.