• ajax 长轮询


    未完成,因为需要换成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;
        }
    

      

  • 相关阅读:
    xml ui
    xml ui
    xml ui
    debug
    centOS7 mini配置linux服务器(一)安装centOs7
    数据结构之__链表
    数据结构之__队列
    数据结构之__栈
    在树莓派上使用 SSD1306 OLED 屏幕
    git官方手册
  • 原文地址:https://www.cnblogs.com/yanqin/p/6672394.html
Copyright © 2020-2023  润新知