【发布时间】:2012-08-01 06:39:14
【问题描述】:
在创建聊天系统时,我使用 long life request 来获取消息,并使用 jquery 请求来发送消息,如下所示:
*发送:*
$("#btn").click(function () {
$.ajax({
type: "POST",
url: "Chat.aspx/Insert",
data: "{ 'Str' :'" + $("#txtStr").val() + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
},
error: function () {
}
});
});
接收:
function Refresh() {
$.ajax({
type: "POST",
url: "Chat.aspx/GetRecords",
data: "{ 'Id' : " + $("#hdnV1").val() + "}",
success: function (data) {
$.each($(data.d), function () {
//Show it to user
});
},
complete: function () {
return Refresh();
},
contentType: "application/json; charset=utf-8",
dataType: "json",
traditional: true,
async: true
});
}
这是我获取消息的服务器端代码:
[WebMethod]
public static object GetRecords(int Id)
{
TestEntities db = new TestEntities();
int count = 0;
while (true)
{
if (count++ >= 300)
return null;
System.Threading.Thread.Sleep(1000);
var list = db.Commets.Where(rec => rec.Id > Id).Select(rec => new { Id = rec.Id, Str = rec.Str }).ToList();
if (list.Count > 0)
return list;
}
}
当用户写东西并点击发送按钮时,请求进入挂起状态,我的事情是因为长寿命请求正在执行
我在萤火虫中检查它们,有没有人可以帮助我!?! 更多细节请评论我,对不起我的英语语法不好,我是英语新手
谢谢
【问题讨论】:
-
我的问题可以理解吗?!!
-
您是否在应用程序中使用 Session 状态?
-
@user1429080 ,你是什么意思?!是的,我在整个项目中都使用它们!
标签: c# jquery asp.net long-polling