|
// Copyright (c) 2010 Daniel Wojciechowski
// E-mail: at gmail.com daniel.wojciechowski
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
// Called when async postback ends
function EndRequestHandler(sender, args)
{
// this function is run after an Ajax partial postback occurs
if(isPolling == 0) // this "if" prevents from runing two requests
longPolling();
}
function addLongPollingError(type, msg){
$("#DebugLabel").html(
"<div class='msg "+ type +"'>" + msg +"</div>" +
"<button type='Button' onClick='longPolling()'>Connect again</button>"
);
}
var isPolling = 0;
function longPolling()
{
// prevent from runing two requests:
// It is now commented because it is currently done in EndRequestHandler
//if(isPolling>0)
// return;
isPolling++;
$.ajax({
type: "GET",
url: "CometAsyncHandler.ashx?waitTime=60", // one minute
//async: true,
cache: false,
//timeout:12000,
success: function(data){
isPolling--;
if(data == "NEWDATAISAVAILABLE")
RefreshData(); // this function is generated by using RegisterFunctionToPostBack()
else if( data == "TOOLONG-DOITAGAIN" )
setTimeout("longPolling()", 0 );
else
addLongPollingError("error", "Error on server side. Received data: \"" + data + "\"");
},
error: function(XMLHttpRequest, textStatus, errorThrown){
isPolling--;
addLongPollingError("error", textStatus + " (" + errorThrown + ")");
}
});
}
$(document).ready(function(){
longPolling(); // Start the initial request
});
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I graduated from the
Jagiellonian University. After defence of master thesis (
link to presentation) I have started professional programming in C and C++ using OpenGL. After that, my Company gave me a chance to switch to .Net framework and C# and I have betrayed my passion for computer graphics. Now I'm C#, SQL and sometimes ASP.NET programmer.