假设您有一个服务器和客户端使用 ajax 递归订阅通知的长时间轮询情况,
var subscribe = function () {
$.get('http://localhost:1234', function (data) {
// use the data
subscribe(); // send another request
});
}
在服务器端,是否有可靠的方法来检测客户端是否真的在那里接收请求——换句话说,是否有人打开了页面并准备好接收推送的数据?您如何处理客户端可能离线但需要接收他们在重新登录时错过的数据的事实?
我目前正在使用基于以下链接的 .NET 反应式扩展来实现服务器,
http://joseoncode.com/2011/06/17/event-driven-http-server-in-c-with-rx-and-httplistener/
using (var server = new HttpServer("http://localhost:1234/"))
{
//the listeners stream and subscription
var listeners = server
.Where(ctx => ctx.Request.HttpMethod == "GET")
//wait the next message to end the request
.Subscribe(ctx => subject.Take(1) .Subscribe(m => ctx.Respond(new StringResponse(m))));