长轮询冻结浏览器并阻止其他ajax请求 [英] Long polling freezes browser and block other ajax request
问题描述
我正在尝试在我的 Spring-MVC Web App中实现长轮询但是在4-5继续AJAX请求之后它冻结了我的浏览器和其他请求。我不知道什么是这里是我的相关代码。
I am trying to implement long polling in my Spring-MVC Web App but it freezes my browser and other request after 4-5 continues AJAX requests.I have no clue whats goin on here is my relevant code.
控制器方法:(服务器端): -
@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;
}
长轮询脚本(客户端): -
$(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) */
}
});
};
更新:
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);
两个警报框都没有弹出.. :(
Neither alert box pops up..:(
推荐答案
您的浏览器代码中似乎有一个空的while循环..这是一种非常CPU的等待事件的方式。
It seems you have an empty while loop in your browser code.. this is a very CPU instensive way to wait for an event.
如果没有发生任何事件,客户端会在你想要的超时50秒后终止请求。但我不确定服务器线程是否也被杀死,或者它是否永远处于打开状态(除非有下一个请求将启动第二个服务器线程,该线程在while循环中也会挂起。也许空while循环的数量对于服务器来说是一种过度杀伤,因此它会停止接受任何更多的请求。所以在一些请求之后(那个)每个触发一个无休止的服务器线程)客户端在新请求上等待永远...因为它无法由服务器处理。
If no events happen the client will kill the request after your desired timeout of 50 seconds. But I'm not sure if the server thread is killed too, or if it "whiles" on forever (unless there is an event). The next request will start a second server thread that hangs in the while loop too then. Maybe the amount of empty while loops is an overkill for the server, so that it stops accepting any more requests. So after some requests (that each triggered an endless server thread) the client waits forever on a new request.. because it can't be handled by the server.
ps:成功后你评论到等待1秒,但将超时设置为10000(10秒)
ps: on success you commented to wait 1 second, but set the timeout to 10000 (10 seconds)
这篇关于长轮询冻结浏览器并阻止其他ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!