jQuery是否具有内置函数来进行长轮询?

时间:2013-03-13 07:01:13

标签: java jquery long-polling

我在做Java Chat application

当我的应用程序启动时,我会在我的外部Jquery中调用pingAction()

我使用此网站作为使用JQUERY进行长轮询的参考 - http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

Jquery pingAction将是,

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);
            } 

            }                           
    );

}

在使用Long Poling之前,我会使用pingAction() in javaScript每隔5秒拨打setTimeInterval()

现在,我需要使用聊天应用LONG POLLING concept中的(Wait until the server gives the new messages)。所以我修改了我在上面看到的Jquery pinAction()

built in methodLong polling是否有JQUERY

1 个答案:

答案 0 :(得分:0)

不,jQuery中没有内置方法来进行长轮询。您必须自己开发或找到解决问题的现有代码。

如果您正在寻找想法,可以从Google search开始,以便在jQuery中进行“长轮询”。有很多例子需要学习。