簡體   English   中英

在JAVA中長時間輪詢Jquery?

[英]Long polling Jquery in JAVA?

這是我的Java Chat application的問題。

啟動應用程序時,我將在外部Jquery中調用pingAction()

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

            }                           
    );

}

我的PingAction Servlet是,

public class PingAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private String secureKey;
    private String userId;
    private int fromPosition ;
    private FlexChatProtocol protocol = null;
    private Ping ping = null;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("UTF-8");
        PrintWriter out = response.getWriter();

        request.setCharacterEncoding("UTF-8");

        secureKey = request.getParameter("secureKey");
        userId = request.getParameter("userId");
        CustomerInfo customer = ApplicationInfo.customerDetails.get(userId);
        if(customer != null){
            fromPosition = customer.getFromPosition();
        }

        if(ApplicationInfo.flexProtocol != null ){

            protocol = ApplicationInfo.flexProtocol;

            ping = new Ping();
            ping.sendPing(secureKey, userId, fromPosition+1, protocol, serverMessage);

            if(customer != null){
            message = customer.getFullMessage();
            }

            out.println(message);
        }
    }

}

使用前Long Poling ,我會打電話給pingAction() in javaScript為每5 seconds使用setTimeInterval()刷新連接,並獲取服務器信息。

現在我需要在Chat應用程序中使用LONG POLLING concept ,因此我修改了Jquery pinAction()上面的內容。

如何使用JQUERY實現LONG POLLING

希望我們的會員能幫助我!

private ChatContext context = ChatContext.getInstance();

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    Long lastmessage = // just put this somewhere

    List<String> messages = context.getMessagesIHaventGotYet(lastmessage); // blocking call
    Object formatedMessages = formatmessages(messages);
    out.write(formatedMessages);

 }

context.getMessagesIHaventGotYet(); 應該是一個阻止操作,因此它將一直等待直到有新消息到達,然后開始執行。 或類似的規定。

基本上,長輪詢意味着服務器“掛起”,直到從某處檢索到所需的信息,然后將其寫入其輸出緩沖區並關閉連接,客戶端再次實例化連接,以開始另一個長輪詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM