ajax长轮询 spring mvc,长时间轮询会冻结浏览器并阻止其他ajax请求

我正在尝试在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;

}

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

$(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: '/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('/notification/notify');

$(document).bind('feed.comet', updateFeed);

$(document).bind('.comet', catchAll);

都没有弹出警告框.. :(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值