日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Spring整合DWRcomet實(shí)現(xiàn)無(wú)刷新多人聊天室

用dwr的comet(推)來(lái)實(shí)現(xiàn)簡(jiǎn)單的無(wú)刷新多人聊天室,comet是長(zhǎng)連接的一種。通常我們要實(shí)現(xiàn)無(wú)刷新,一般會(huì)使用到Ajax。Ajax 應(yīng)用程序可以使用兩種基本的方法解決這一問(wèn)題:一種方法是瀏覽器每隔若干秒時(shí)間向服務(wù)器發(fā)出輪詢以進(jìn)行更新,另一種方法是服務(wù)器始終打開與瀏覽器的連接并在數(shù)據(jù)可用時(shí)發(fā)送給瀏覽器。***種方法一般利用setTimeout或是setInterval定時(shí)請(qǐng)求,并返回***數(shù)據(jù),這無(wú)疑增加了服務(wù)器的負(fù)擔(dān),浪費(fèi)了大量的資源。而第二種方法也會(huì)浪費(fèi)服務(wù)器資源,長(zhǎng)期的建立連接;而相對(duì)***種來(lái)說(shuō),第二種方式會(huì)更優(yōu)于***種方法;這里有一個(gè)一對(duì)多和多對(duì)一的關(guān)系,而comet向多個(gè)客戶端推送數(shù)據(jù)就是一對(duì)多的關(guān)系。而具體使用哪種方式,要看你當(dāng)前的需求而定,沒(méi)有絕對(duì)的。

成都創(chuàng)新互聯(lián)公司為企業(yè)級(jí)客戶提高一站式互聯(lián)網(wǎng)+設(shè)計(jì)服務(wù),主要包括網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、重慶App定制開發(fā)、微信平臺(tái)小程序開發(fā)、宣傳片制作、LOGO設(shè)計(jì)等,幫助客戶快速提升營(yíng)銷能力和企業(yè)形象,創(chuàng)新互聯(lián)各部門都有經(jīng)驗(yàn)豐富的經(jīng)驗(yàn),可以確保每一個(gè)作品的質(zhì)量和創(chuàng)作周期,同時(shí)每年都有很多新員工加入,為我們帶來(lái)大量新的創(chuàng)意。 

為什么使用Comet?

輪詢方法的主要缺點(diǎn)是:當(dāng)擴(kuò)展到更多客戶機(jī)時(shí),將生成大量的通信量。每個(gè)客戶機(jī)必須定期訪問(wèn)服務(wù)器以檢查更新,這為服務(wù)器資源添加了更多負(fù)荷。最壞的一種情況是對(duì)不頻繁發(fā)生更新的應(yīng)用程序使用輪詢,例如一種 Ajax 郵件 Inbox。在這種情況下,相當(dāng)數(shù)量的客戶機(jī)輪詢是沒(méi)有必要的,服務(wù)器對(duì)這些輪詢的回答只會(huì)是 “沒(méi)有產(chǎn)生新數(shù)據(jù)”。雖然可以通過(guò)增加輪詢的時(shí)間間隔來(lái)減輕服務(wù)器負(fù)荷,但是這種方法會(huì)產(chǎn)生不良后果,即延遲客戶機(jī)對(duì)服務(wù)器事件的感知。當(dāng)然,很多應(yīng)用程序可以實(shí)現(xiàn)某種權(quán)衡,從而獲得可接受的輪詢方法。

盡管如此,吸引人們使用 Comet 策略的其中一個(gè)優(yōu)點(diǎn)是其顯而易見的高效性??蛻魴C(jī)不會(huì)像使用輪詢方法那樣生成煩人的通信量,并且事件發(fā)生后可立即發(fā)布給客戶機(jī)。但是保持長(zhǎng)期連接處于打開狀態(tài)也會(huì)消耗服務(wù)器資源。當(dāng)?shù)却隣顟B(tài)的 servlet 持有一個(gè)持久性請(qǐng)求時(shí),該 servlet 會(huì)獨(dú)占一個(gè)線程。這將限制 Comet 對(duì)傳統(tǒng) servlet 引擎的可伸縮性,因?yàn)榭蛻魴C(jī)的數(shù)量會(huì)很快超過(guò)服務(wù)器棧能有效處理的線程數(shù)量。

如果本示例結(jié)合Jetty應(yīng)用服務(wù)器效果會(huì)更好。

開發(fā)環(huán)境:

System:Windows

WebBrowser:IE6+、Firefox3+

JavaEE Server:tomcat5.0.2.8、tomcat6

IDE:eclipse、MyEclipse 8

開發(fā)依賴庫(kù):

JavaEE5、Spring 3.0.5、dwr 3

Email:hoojo_@126.com

Blog:http://blog.csdn.net/IBM_hoojo or http://hoojo.cnblogs.com/

#p#

一、準(zhǔn)備工作

1、 下載dwr的相關(guān)jar包

https://java.net/downloads/dwr/Development%20Builds/Build%20116/dwr.jar

程序中還需要spring的相關(guān)jar包

http://ebr.springsource.com/repository/app/library/version/detail?name=org.springframework.spring&version=3.0.5.RELEASE

需要的jar包如下

2、 建立一個(gè)WebProject,名稱DWRComet

在web.xml中添加dwr、spring配置如下:

 
 
 
 
  1. <-- 加載Spring容器配置 --> 
  2.  
  3.     org.springframework.web.context.ContextLoaderListener 
  4.  
  5.    
  6. <-- 設(shè)置Spring容器加載配置文件路徑 --> 
  7.  
  8.     contextConfigLocation 
  9.     classpath*:applicationContext-*.xml 
  10.  
  11.    
  12.  
  13.     org.directwebremoting.servlet.DwrListener 
  14.  
  15.    
  16.  
  17.     dwr-invoker 
  18.     org.directwebremoting.servlet.DwrServlet 
  19.      
  20.         debug 
  21.         true 
  22.      
  23.           
  24.     <-- dwr的comet控制 --> 
  25.      
  26.       pollAndCometEnabled 
  27.       true 
  28.      
  29.  
  30.    
  31.  
  32.     dwr-invoker 
  33.     /dwr/* 
  34.  

3、 在src目錄加入applicationContext-beans.xml配置,這個(gè)配置專門配置bean對(duì)象,用來(lái)配置需要注入的對(duì)象。

 
 
 
 
  1.  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.     xmlns:aop="http://www.springframework.org/schema/aop" 
  4.     xmlns:tx="http://www.springframework.org/schema/tx"   
  5.     xmlns:util="http://www.springframework.org/schema/util" 
  6.     xmlns:context="http://www.springframework.org/schema/context" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.     http://www.springframework.org/schema/aop   
  10.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  11.     http://www.springframework.org/schema/tx   
  12.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  13.     http://www.springframework.org/schema/util   
  14.     http://www.springframework.org/schema/util/spring-util-3.0.xsd  
  15.     http://www.springframework.org/schema/context   
  16.     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
  17.  

4、 在WEB-INF目錄添加dwr.xml文件,基本代碼如下

   
   
   
   
  1.  
  2.  
  3.  
  4.  

以上的準(zhǔn)備基本完畢,下面來(lái)完成無(wú)刷新聊天室代碼

#p#

二、聊天室相關(guān)業(yè)務(wù)實(shí)現(xiàn)

1、 聊天實(shí)體類Model

 
 
 
 
  1. package com.hoo.entity;  
  2.    
  3. import java.util.Date;  
  4.    
  5. /**  
  6.  * function:  
  7.  * @author hoojo  
  8.  * @createDate 2011-6-3 下午06:40:07  
  9.  * @file Message.java  
  10.  * @package com.hoo.entity  
  11.  * @project DWRComet  
  12.  * @blog http://blog.csdn.net/IBM_hoojo  
  13.  * @email hoojo_@126.com  
  14.  * @version 1.0  
  15.  */ 
  16. public class Message {  
  17.     private int id;  
  18.     private String msg;  
  19.     private Date time;  
  20.     //getter、setter  

2、 編寫聊天信息的事件

 
 
 
 
  1. package com.hoo.chat;  
  2.    
  3. import org.springframework.context.ApplicationEvent;  
  4.    
  5. /**  
  6.  * function:發(fā)送聊天信息事件  
  7.  * @author hoojo  
  8.  * @createDate 2011-6-7 上午11:24:21  
  9.  * @file MessageEvent.java  
  10.  * @package com.hoo.util  
  11.  * @project DWRComet  
  12.  * @blog http://blog.csdn.net/IBM_hoojo  
  13.  * @email hoojo_@126.com  
  14.  * @version 1.0  
  15.  */ 
  16. public class ChatMessageEvent extends ApplicationEvent {  
  17.    
  18.     private static final long serialVersionUID = 1L;  
  19.    
  20.     public ChatMessageEvent(Object source) {  
  21.         super(source);  
  22.     }  

繼承ApplicationEvent,構(gòu)造參數(shù)用于傳遞發(fā)送過(guò)來(lái)的消息。這個(gè)事件需要一個(gè)監(jiān)聽器監(jiān)聽,一旦觸發(fā)了這個(gè)事件,我們就可以向客戶端發(fā)送消息。

3、 發(fā)送消息服務(wù)類,用戶客戶端發(fā)送消息。dwr需要暴露這個(gè)類里面的發(fā)送消息的方法

 
 
 
 
  1. package com.hoo.chat;  
  2.    
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.ApplicationContextAware;  
  6. import com.hoo.entity.Message;  
  7.    
  8. /**  
  9.  * function:客戶端發(fā)消息服務(wù)類業(yè)務(wù)  
  10.  * @author hoojo  
  11.  * @createDate 2011-6-7 下午02:12:47  
  12.  * @file ChatService.java  
  13.  * @package com.hoo.chat  
  14.  * @project DWRComet  
  15.  * @blog http://blog.csdn.net/IBM_hoojo  
  16.  * @email hoojo_@126.com  
  17.  * @version 1.0  
  18.  */ 
  19. public class ChatService implements ApplicationContextAware {  
  20.     private ApplicationContext ctx;  
  21.     public void setApplicationContext(ApplicationContext ctx) throws BeansException {  
  22.         this.ctx = ctx;  
  23.     }  
  24.       
  25.     /**  
  26.      * function: 向服務(wù)器發(fā)送信息,服務(wù)器端監(jiān)聽ChatMessageEvent事件,當(dāng)有事件觸發(fā)就向所有客戶端發(fā)送信息  
  27.      * @author hoojo  
  28.      * @createDate 2011-6-8 下午12:37:24  
  29.      * @param msg  
  30.      */ 
  31.     public void sendMessage(Message msg) {  
  32.         //發(fā)布事件  
  33.         ctx.publishEvent(new ChatMessageEvent(msg));  
  34.     }  

上面的sendMessage需要瀏覽器客戶端調(diào)用此方法完成消息的發(fā)布,傳遞一個(gè)Message對(duì)象,并且是觸發(fā)ChatMessageEvent事件。

#p#

4、 編寫監(jiān)聽器監(jiān)聽客戶端是否觸發(fā)ChatMessageEvent

 
 
 
 
  1. package com.hoo.chat;  
  2.    
  3. import java.util.Collection;  
  4. import java.util.Date;  
  5. import javax.servlet.ServletContext;  
  6. import org.directwebremoting.ScriptBuffer;  
  7. import org.directwebremoting.ScriptSession;  
  8. import org.directwebremoting.ServerContext;  
  9. import org.directwebremoting.ServerContextFactory;  
  10. import org.springframework.context.ApplicationEvent;  
  11. import org.springframework.context.ApplicationListener;  
  12. import org.springframework.web.context.ServletContextAware;  
  13. import com.hoo.entity.Message;  
  14.    
  15. /**  
  16.  * function:監(jiān)聽客戶端事件,想客戶端推出消息  
  17.  * @author hoojo  
  18.  * @createDate 2011-6-7 上午11:33:08  
  19.  * @file SendMessageClient.java  
  20.  * @package com.hoo.util  
  21.  * @project DWRComet  
  22.  * @blog http://blog.csdn.net/IBM_hoojo  
  23.  * @email hoojo_@126.com  
  24.  * @version 1.0  
  25.  */ 
  26. @SuppressWarnings("unchecked")  
  27. public class ChatMessageClient implements ApplicationListener, ServletContextAware {  
  28.       
  29.     private ServletContext ctx;  
  30.     public void setServletContext(ServletContext ctx) {  
  31.         this.ctx = ctx;  
  32.     }  
  33.       
  34.     @SuppressWarnings("deprecation")  
  35.     public void onApplicationEvent(ApplicationEvent event) {  
  36.         //如果事件類型是ChatMessageEvent就執(zhí)行下面操作  
  37.         if (event instanceof ChatMessageEvent) {  
  38.             Message msg = (Message) event.getSource();  
  39.             ServerContext context = ServerContextFactory.get();  
  40.             //獲得客戶端所有chat頁(yè)面script session連接數(shù)  
  41.    
  42.             Collection 
  43.      
  44.      
  45.      
  46.    
  47.     
  48.    
  49.        
  50.       
     
  51.       昵稱:
     
  52.       消息: 
  53.        
  54.    
  55.  

首先,你需要導(dǎo)入dwr的engine.js文件,這個(gè)很重要,是dwr的引擎文件。其次你使用的那個(gè)類的方法,也需要在導(dǎo)入進(jìn)來(lái)。一般是interface下的,并且在dwr.xml中配置過(guò)的create。

上面的js中調(diào)用的charService類中的sendMessage方法,所以在jsp頁(yè)面中導(dǎo)入的是ChatService.js。

在body的onload事件中,需要設(shè)置反轉(zhuǎn)Ajax,這個(gè)很重要。

showMessage是ChatMessageClient的onApplicationEvent方法中的appendScript中需要執(zhí)行的方法。data參數(shù)也是在那里傳遞過(guò)來(lái)的。

每當(dāng)發(fā)送sendMessage方法后就會(huì)觸發(fā)ChatMessageEvent事件,然后監(jiān)聽的地方就會(huì)執(zhí)行onApplicationEvent方法,在這個(gè)方法中又會(huì)執(zhí)行瀏覽器中的showMessage方法。

原文鏈接:http://www.cnblogs.com/hoojo/archive/2011/06/08/2075201.html

【編輯推薦】

  1. Oracle計(jì)劃修復(fù)Java SE中的17個(gè)漏洞
  2. Java SE 6新年***次更新 修復(fù)Bug超300個(gè)
  3. JDK 5和Java SE 6小更新
  4. Java SE 6更新 修復(fù)重大安全問(wèn)題
  5. Java SE 6中的垃圾回收器G1收費(fèi)是虛驚一場(chǎng)

網(wǎng)站欄目:Spring整合DWRcomet實(shí)現(xiàn)無(wú)刷新多人聊天室
轉(zhuǎn)載來(lái)源:http://www.5511xx.com/article/dhpedph.html