在一般聊天应用程序中,客户端的浏览器总是轮询服务器以检查新消息。
// the function to check new messages in server
function check(){
// but this question is less about jQuery.
$.ajax({
type: "POST",
url: "check.aspx",
data: "someparam=123",
success: function(msg){
// process msg here
// CHECK IT AGAIN, but sometimes we need to make delay here
check();
}
});
}
然后我阅读了Nicholas Allen 关于在 IIS 中保持连接打开的博客。
这让我想到是否可以通过传输分块的 HTTP 将数据从我的服务器推送到客户端的浏览器(这意味着像流,对吗?)并保持连接打开。
在保持连接打开的同时,在服务器中,我有想法保持运行以检查新消息。像这样的东西,也许
while(connectionStillOpen) {
// any new message?
if( AnyMessage() )
{
// send chunked data, can I?
SendMessageToBrowser();
// may be we need to make delay here
Sleep(forSomeTime);
}
}
这是一个原始的想法。
我在 ASP.net 中创建的聊天应用程序。由于我对 WCF 和高级 IIS 流模块的了解较少,我需要您的建议来实现这个想法。
是的,不可能是答案。但我需要知道为什么如果它仍然不可能。
更新(3年后):
这正是我一直在寻找的: Microsoft ASP.NET SignalR