未完成,因为需要换成webscoket来做,该ajax长轮询有待完善
function poll(){ $.ajax({ type:"POST", url:"ajax", async:true, data:{ "receiverType":"single", "receiverId":"${receiverId}", "senderId":"${senderId}", }, success:function(data){ $("#content").append("<p><span>${receiverId}:"+data+"</span></p>"); console.log(data); } }); setTimeout(poll,5000); } $(function(){ poll(); }
@RequestMapping("/ajax") @ResponseBody public String ajaxMessage(String receiverType,String receiverId,String senderId) throws InterruptedException { System.out.println("receiverType = " + receiverType); System.out.println("receiverId = " + receiverId); System.out.println("senderId = " + senderId); String content = null; int i =0; boolean end = false; while (!end) { if(messages!=null) { System.out.println(messages.size()+"===================================="); i++; for(Message m : messages){ if(m.getReceiver().getType().equals(receiverType) && m.getFromUserName().equals(receiverId) && m.getReceiver().getId().equals(senderId)){ System.out.println("temp = " + m.getContent()); return m.getContent(); } } if(i==20){ end = true; } } Thread.sleep(5000); } return content; }