长轮询冻结浏览器并阻止其他ajax请求

Dan*_*ush 7 java ajax jquery spring-mvc long-polling

我试图在我的Spring-MVC Web App中实现长轮询,但是在4-5继续AJAX请求之后它冻结了我的浏览器和其他请求.我不知道这里是什么是我的相关代码.

控制器方法:(服务器端): -

@Asynchronous
    @RequestMapping("/notify")
    public @ResponseBody
    Events notifyEvent(HttpServletRequest request) {
        Events events = null;
        try {
            events = (Events) request.getSession(false).getServletContext().getAttribute("events");
            System.out.println("Request Came from" + ((com.hcdc.coedp.safe.domain.User) request.getSession(false).getAttribute(Constants.KEY_LOGGED_IN_USER)).getLoginId());
            if (!events.getTypeOfEvents().isEmpty()) {
                System.out.println("Removing older entries");
                events.getTypeOfEvents().clear();
            }
            while (!events.isHappend()) {
                //Waiting for event to happen.
            }
            events = Events.getInstance();
            events.setHappend(false);
            request.getSession(false).getServletContext().setAttribute("events", events);

        }catch (Exception e) {
            e.printStackTrace();
        }
        return events;
    }
Run Code Online (Sandbox Code Playgroud)

长轮询脚本(客户端): -

$(document).ready(function() {
                    $.ajaxSetup({
                        async:true//set a global ajax requests as asynchronus
                    });
                     alert('Handler for .onload() called.');
                    waitForMsg();

                });
                function waitForMsg(){

                    xhr=  $.ajax({
                        type: "POST",
                        url: '<%=request.getContextPath()%>/notification/notify',

                        async: true, /* If set to non-async, browser shows page as "Loading.."*/
                        cache: false,
                        timeout:50000, /* Timeout in ms */
                        global:false,
                        success: function(data){ /* called when request to notifier completes */
                          /* Doing smthing with response **/
                            setTimeout(
                            waitForMsg, /* Request next message */
                            1000 /* ..after 1 seconds */
                        );
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown){
                            addmsg("error", textStatus + " (" + errorThrown + ")");
                            setTimeout(
                            waitForMsg, /* Try again after.. */
                            15000); /* milliseconds (15seconds) */
                        }
                    });
                };
Run Code Online (Sandbox Code Playgroud)

更新:

function updateFeed(event, data) {
                var f=eval(data);
                alert(f.typeOfEvents.length);
            }

            function catchAll(event, data, type) {
                console.log(data);
                alert("error");
                console.log(type);
            }

            $.comet.connect('<%=request.getContextPath()%>/notification/notify');
            $(document).bind('feed.comet', updateFeed);
            $(document).bind('.comet', catchAll);
Run Code Online (Sandbox Code Playgroud)

弹出警报框.. :(

Dom*_*hoc 9

好像你遇到了会话文件锁

对于PHP

使用session_write_close()时不需要会话值


lrs*_*jng 5

您的浏览器代码中似乎有一个空的while循环..这是一种非常CPU的等待事件的方式.

如果没有事件发生,客户端将在您所需的超时50秒后终止请求.但我不确定服务器线程是否也被杀死,或者它是否"永远"(除非有事件).下一个请求将启动第二个服务器线程,该线程在while循环中也会挂起.也许空的while循环的数量对于服务器而言是过度的,因此它会停止接受任何更多的请求.因此,在一些请求(每个都触发了无休止的服务器线程)之后,客户端会在新请求上永远等待...因为它无法由服务器处理.

ps:成功时你评论等待1秒,但将超时设置为10000(10秒)