0

I am doing Java Chat application .

I will call the pingAction() in my external Jquery when my application get initiated.

I used this site for reference of long polling with JQUERY - http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

The Jquery pingAction will be ,

function pingAction(){

    $.ajax(
            {
                type: "post",
                url: "PingAction",
                async:     false,
                data : "userId="+encodeURIComponent(userId)+"&secureKey="+encodeURIComponent(secureKey)+"&sid="+Math.random() ,
                cache:false,
                complete: pingAction,
                timeout: 5000 ,
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                scriptCharset: "utf-8" ,
                dataType: "html",

                error: function (xhr, ajaxOptions, thrownError) {
                alert("xhr.status : "+xhr.status);

                if(xhr.status == 12029 || xhr.status == 0){
                    //alert("XMLHttp status : "+xhr.status);
                    $("#serverMsg").css("backgroundColor" , "yellow");
                    $("#serverMsg").text("Your Network connection is failed !");
                    $("#serverMsg").show();
                }
                //setTimeout('pingAction()', 5000);
                xhr.abort();
            },

            success: function( responseData , status){
                if($("#serverMsg").text() == "" || $("#serverMsg").text() == "Your Network connection is failed !"){
                    disableServerMessage();
                }

                if(responseData != "null" && responseData.length != 0  && responseData != null){

                    var stringToArray = new Array;
                    stringToArray = responseData.split("<//br//>");
                    var len = stringToArray.length;
                    for(var i=0;i<len-1;i++){
                        getText(stringToArray[i]);

                    }
                }

                //setTimeout('pingAction()', 5000);
            } 

            }                           
    );

}

Before using the Long Poling , I will call the pingAction() in javaScript for every 5 seconds using the setTimeInterval().

Now I need to use the LONG POLLING concept in the Chat application (Wait until the server gives the new messages).So I modified my Jquery pinAction() what you have seeing above.

Is there any built in method to do the Long polling in JQUERY ?

Human Being
  • 8,269
  • 28
  • 93
  • 136
  • The main "long" aspect of long-polling is server-side. From a client-side perspective, you need to ensure the requests either don't time out or are renewed when they do time out. – Beetroot-Beetroot Mar 13 '13 at 07:11
  • @Beetroot Thanks for your reply.Can U please suggest me some site or resources how to implement Long Polling using JQUERY ? – Human Being Mar 13 '13 at 07:14
  • I don't know of anything specific and would have to search for one using a search engine, which is something you can do for yourself. – Beetroot-Beetroot Mar 13 '13 at 07:18

1 Answers1

0

No, there is no built in method in jQuery to do long polling. You will have to either develop your own or find existing code that solves your problem.

If you're looking for ideas, you can start with a Google search for "long polling in jQuery". There are plenty of examples to learn from.

jfriend00
  • 683,504
  • 96
  • 985
  • 979