ajax長輪詢

來源:互聯網
上載者:User

標籤:

javaWeb項目中需要一個即時提醒的功能,也就是某人做了某一操作,可以立即提醒到當前線上的使用者

最開始想在使用者做了操作後,儲存一個狀態到資料庫中然後用每隔幾秒用ajax去請求後台查詢資料庫來確定是否顯示提醒視窗

提醒視窗使用jquery easyui 的messager 在右下角彈出如

後查得可通過AJAX長輪詢的方法來解決頻繁對背景請求,進一步減小壓力

在實現過程發現AJAX的多次請求會出現多線程並發的問題又使用線程同步來解決該問題

 

個人對ajax長輪詢的一點愚見

ajax請示後台時,背景程式並沒有立即返回資訊而是掛起,當符合條件時才會返回資訊

 

文筆不好,只能白話出來,如有不對的地方還請各位指正,一起探討學習!

現貼出部分代碼 

 

js  ajax實現部分

 

[javascript] view plain copy  
  1. function getRtmMsg(show){  
  2.         $.ajax({  
  3.            type:"POST",  
  4.            url:"rtmAction!getMsg.action",  
  5.            data:"show="+show,  
  6.            success: function(msg){  
  7.              if(msg!=null){  
  8.                 bottomRight();  
  9.                 getRtmMsg("0");  
  10.              }  
  11.            }  
  12.         });  
  13.     }  


使用 jquery easyui的messager彈出提示視窗代碼

 

 

[javascript] view plain copy  
  1. function bottomRight(){    
  2.         $.messager.show({    
  3.             title:‘提醒‘,    
  4.             msg:‘下載考生‘,    
  5.             showType:‘show‘  
  6.         });   
  7.     }  



 

後台Action

 

[java] view plain copy  
    1. /** 
    2.  * 即時訊息Action 
    3.  * @author wangwei 
    4.  * May 23, 2013 
    5.  */  
    6. public class RtmAction  extends ActionSupport{  
    7.       
    8.     HttpServletResponse response = ServletActionContext.getResponse();  
    9.     HttpServletRequest request = ServletActionContext.getRequest();  
    10.       
    11.     private static final ThreadLocal threadLocal = new ThreadLocal();  
    12.     private static Object lock = new Object();  
    13.     public void getMsg(){  
    14.         String show = request.getParameter("show");  
    15.         if("0".equals(show)){  
    16.             RtmTemplate.cjzxShow = false;  
    17.         }  
    18.         try {  
    19.             //多線程同步解決並發問題  
    20.             synchronized(lock){  
    21.                 while(!RtmTemplate.cjzxShow){  
    22.                     System.out.println("檢測中。。。。");  
    23.                     Thread.sleep(5000);  
    24.                 }  
    25.             }  
    26.             response.getWriter().println("1");  
    27.         } catch (IOException e) {  
    28.             e.printStackTrace();  
    29.         } catch (InterruptedException e) {  
    30.             e.printStackTrace();  
    31.         }  
    32.     }  
    33.       
    34. } 

ajax長輪詢 (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.