自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring整合DWR comet 實(shí)現(xiàn)無刷新 多人聊天室

開發(fā) 后端
用dwr的comet(推)來實(shí)現(xiàn)簡單的無刷新多人聊天室,comet是長連接的一種。通常我們要實(shí)現(xiàn)無刷新,一般會使用到Ajax。

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

為什么使用Comet?

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

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

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

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

System:Windows

WebBrowser:IE6+、Firefox3+

JavaEE Server:tomcat5.0.2.8、tomcat6

IDE:eclipse、MyEclipse 8

開發(fā)依賴庫:

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

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

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

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

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> 
  3. <dwr> 
  4. </dwr> 

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

#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.  * <b>function:</b>  
  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.  * <b>function:</b>發(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ā)送過來的消息。這個(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.  * <b>function:</b>客戶端發(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.      * <b>function:</b> 向服務(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對象,并且是觸發(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.  * <b>function:</b>監(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頁面script session連接數(shù)  
  41.    
  42.             Collection<ScriptSession> sessions = context.getScriptSessionsByPage(ctx.getContextPath() + "/chat.jsp");  
  43.             for (ScriptSession session : sessions) {  
  44.                 ScriptBuffer sb = new ScriptBuffer();  
  45.                 Date time = msg.getTime();  
  46.                 String s = time.getYear() + "-" + (time.getMonth() + 1) + "-" +  time.getDate() + " "   
  47.                         +  time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();  
  48.                 //執(zhí)行setMessage方法  
  49.    
  50.                 sb.appendScript("showMessage({msg: '")  
  51.                 .appendScript(msg.getMsg())  
  52.                 .appendScript("', time: '")  
  53.                 .appendScript(s)  
  54.                 .appendScript("'})");  
  55.                 System.out.println(sb.toString());  
  56.                 //執(zhí)行客戶端script session方法,相當(dāng)于瀏覽器執(zhí)行JavaScript代碼  
  57.                   //上面就會執(zhí)行客戶端瀏覽器中的showMessage方法,并且傳遞一個(gè)對象過去  
  58.    
  59.                 session.addScript(sb);  
  60.             }  
  61.         }  
  62.     }  

上面的代碼主要是監(jiān)聽客戶端的事件,一旦客戶端有觸發(fā)ApplicationEvent事件或是其子類,就會執(zhí)行onApplicationEvent方法。代碼中通過instanceof判斷對象實(shí)例,然后再執(zhí)行。如果有觸發(fā)ChatMessageEvent事件,就獲取所有連接chat.jsp這個(gè)頁面的ScriptSession。然后像所有的ScriptSession中添加script。這樣被添加的ScriptSession就會在有連接chat.jsp的頁面中執(zhí)行。

所以這就是客戶端為什么會執(zhí)行服務(wù)器端的JavaScript代碼。但前提是需要在web.xml中添加dwrComet配置以及在chat頁面添加ajax反轉(zhuǎn)。

5、 下面開始在bean容器和dwr的配置中添加我們的配置

applicationContext-beans.xml配置

  1. <bean id="chatService" class="com.hoo.chat.ChatService"/> 
  2. <bean id="chatMessageClient" class="com.hoo.chat.ChatMessageClient"/> 

上面的chatService會在dwr配置中用到

dwr.xml配置

  1. <allow> 
  2.     <convert match="com.hoo.entity.Message" converter="bean"> 
  3.         <param name="include" value="msg,time" /> 
  4.     </convert> 
  5.    
  6.     <create creator="spring" javascript="ChatService"> 
  7.         <param name="beanName" value="chatService" /> 
  8.     </create> 
  9. </allow> 

charService的sendMessage方法傳遞的是Message對象,所以要配置Message對象的convert配置。

上面的create的creator是spring,表示在spring容器中拿chatService對象。里面的參數(shù)的beanName表示在spring容器中找name等于charService的bean對象。

#p#

6、 客戶端chat.jsp頁面代碼

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %> 
  6.    
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  8. <html> 
  9.   <head> 
  10.     <base href="<%=basePath%>"> 
  11.       
  12.     <title>Chat</title> 
  13.       
  14.     <meta http-equiv="pragma" content="no-cache"> 
  15.     <meta http-equiv="cache-control" content="no-cache"> 
  16.     <meta http-equiv="expires" content="0">      
  17.     <script type="text/javascript" src="${pageContext.request.contextPath }/dwr/engine.js"></script> 
  18.     <script type="text/javascript" src="${pageContext.request.contextPath }/dwr/util.js"></script> 
  19.     <script type="text/javascript" src="${pageContext.request.contextPath }/dwr/interface/ChatService.js"></script> 
  20.     <script type="text/javascript"> 
  21.         function send() {  
  22.             var time = new Date();  
  23.             var content = dwr.util.getValue("content");  
  24.             var name = dwr.util.getValue("userName");  
  25.             var info = encodeURI(encodeURI(name + " say:\n" + content));  
  26.             var msg = {"msg": info, "time": time};  
  27.             dwr.util.setValue("content", "");  
  28.             if (!!content) {  
  29.                 ChatService.sendMessage(msg);  
  30.             } else {  
  31.                 alert("發(fā)送的內(nèi)容不能為空!");  
  32.             }  
  33.         }  
  34.    
  35.         function showMessage(data) {  
  36.             var message = decodeURI(decodeURI(data.msg));  
  37.             var text = dwr.util.getValue("info");  
  38.             if (!!text) {    
  39.                 dwr.util.setValue("info", text + "\n" + data.time + "  " + message);  
  40.             } else {  
  41.                 dwr.util.setValue("info", data.time + "  " + message);  
  42.             }  
  43.         }  
  44.     </script> 
  45.   </head> 
  46.     
  47.   <body onload="dwr.engine.setActiveReverseAjax(true);"> 
  48.       <textarea rows="20" cols="60" id="info" readonly="readonly"></textarea> 
  49.       <hr/> 
  50.       昵稱:<input type="text" id="userName"/><br/> 
  51.       消息:<textarea rows="5" cols="30" id="content"></textarea> 
  52.       <input type="button" value=" Send " onclick="send()" style="height: 85px; width: 85px;"/> 
  53.   </body> 
  54. </html> 

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

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

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

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

每當(dāng)發(fā)送sendMessage方法后就會觸發(fā)ChatMessageEvent事件,然后監(jiān)聽的地方就會執(zhí)行onApplicationEvent方法,在這個(gè)方法中又會執(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ù)重大安全問題
  5. Java SE 6中的垃圾回收器G1收費(fèi)是虛驚一場

 

 

責(zé)任編輯:艾婧 來源: hoojo的博客
相關(guān)推薦

2023-01-13 00:02:41

2023-01-05 09:17:58

2023-02-10 08:16:48

WebSocket簡易聊天室

2011-12-15 11:11:51

JavaNIO

2015-07-06 10:42:18

PHP聊天室應(yīng)用

2022-07-26 14:53:10

WebSocket網(wǎng)絡(luò)通信協(xié)議

2021-11-16 09:38:10

鴻蒙HarmonyOS應(yīng)用

2022-12-01 08:25:23

eTsTCP聊天室

2022-11-14 08:01:48

2011-11-30 16:37:58

sync

2015-08-06 17:17:33

swoole聊天室

2021-10-14 18:46:29

Websocket瀏覽器API

2024-10-07 10:45:12

2022-04-18 10:36:48

社交軟件聊天平臺rocket.cha

2021-02-06 23:26:25

聊天室開發(fā)WebSocket

2024-01-18 11:15:46

Pythonsocket聊天室

2021-12-09 16:48:25

鴻蒙HarmonyOS應(yīng)用

2021-11-15 11:32:12

Linux Linux sockeLinux 系統(tǒng)

2012-05-25 10:41:33

StrutsDWRJava
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號