2

这是我的问题Java Chat application

当我的应用程序启动时,我将pingAction()在我的外部 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);
            } 

            }                           
    );

}

我的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 javaScriptfor5 secondssetTimeInterval()刷新连接并获取服务器消息。

现在我需要LONG POLLING concept在聊天应用程序中使用。所以我修改Jquery pinAction()了你在上面看到的内容。

我怎样才能实现LONG POLLING使用JQUERY

希望我们的堆栈成员能帮助我!

4

1 回答 1

1
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(); 应该是一个阻塞操作,所以它会一直等待直到新消息到达,然后它才会开始行动。或类似的规定。

基本上长轮询意味着服务器“挂起”,直到从某个地方检索到它需要的信息,然后将其写入其输出缓冲区并关闭连接,客户端再次实例化连接以开始另一个长轮询。

于 2013-03-12T14:47:16.617 回答