During create a chat system , I use a long life request to get message , and use a jquery request to send message like this :
*Send: *
$("#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 () {
}
});
});
Receive :
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
});
}
and this is my server side code to get messages :
[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;
}
}
When user writes something and clicks on the Send button , request goes to pending state , and I thing because the long life request is executing
I check them up in firebug, is there anybody out there to help me !?! For more details comment me please , and sorry about my bad English syntax , I am new in English
Thanks