我有一个网页,可以通过长轮询来轮询来自服务器的数据。我的javascript代码是:
function getData(){
$.ajax({
url: "graph.htm",
type:"POST",
dataType:"json",
timeout: 30000,
success: successFunc,
complete: getData
});
}
function successFunc(data, textStatus){
console.log(data);
//......
}
我在服务器上有一个Controller,每秒检查一次,如果数据库被修改,如果是,则它会发送数据:
@RequestMapping(value = "/graph", method = RequestMethod.POST)
public void graphPOST(HttpServletResponse response) {
while(!daoService.isModified()){
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
try {
String jsonData = daoService.getJsonData();
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(jsonData);
System.out.println("=== sent!");
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException("IOError writing file to output stream");
}
}
通常它工作正常。 但有时(可能是十次一次)我有这种情况: