成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring整合DWR comet 實現無刷新 多人聊天室

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

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

為什么使用Comet?

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

盡管如此,吸引人們使用 Comet 策略的其中一個優點是其顯而易見的高效性??蛻魴C不會像使用輪詢方法那樣生成煩人的通信量,并且事件發生后可立即發布給客戶機。但是保持長期連接處于打開狀態也會消耗服務器資源。當等待狀態的 servlet 持有一個持久性請求時,該 servlet 會獨占一個線程。這將限制 Comet 對傳統 servlet 引擎的可伸縮性,因為客戶機的數量會很快超過服務器棧能有效處理的線程數量。

如果本示例結合Jetty應用服務器效果會更好。

開發環境:

System:Windows

WebBrowser:IE6+、Firefox3+

JavaEE Server:tomcat5.0.2.8、tomcat6

IDE:eclipse、MyEclipse 8

開發依賴庫:

JavaEE5、Spring 3.0.5、dwr 3

Email:hoojo_@126.com

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

#p#

一、準備工作

1、 下載dwr的相關jar包

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

程序中還需要spring的相關jar包

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

需要的jar包如下

 

2、 建立一個WebProject,名稱DWRComet

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

  1. <-- 加載Spring容器配置 --> 
  2. <listener> 
  3.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  4. </listener> 
  5.    
  6. <-- 設置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配置,這個配置專門配置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> 

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

#p#

二、聊天室相關業務實現

1、 聊天實體類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>發送聊天信息事件  
  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,構造參數用于傳遞發送過來的消息。這個事件需要一個監聽器監聽,一旦觸發了這個事件,我們就可以向客戶端發送消息。

3、 發送消息服務類,用戶客戶端發送消息。dwr需要暴露這個類里面的發送消息的方法

  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>客戶端發消息服務類業務  
  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> 向服務器發送信息,服務器端監聽ChatMessageEvent事件,當有事件觸發就向所有客戶端發送信息  
  27.      * @author hoojo  
  28.      * @createDate 2011-6-8 下午12:37:24  
  29.      * @param msg  
  30.      */ 
  31.     public void sendMessage(Message msg) {  
  32.         //發布事件  
  33.         ctx.publishEvent(new ChatMessageEvent(msg));  
  34.     }  

上面的sendMessage需要瀏覽器客戶端調用此方法完成消息的發布,傳遞一個Message對象,并且是觸發ChatMessageEvent事件。

#p#

4、 編寫監聽器監聽客戶端是否觸發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>監聽客戶端事件,想客戶端推出消息  
  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就執行下面操作  
  37.         if (event instanceof ChatMessageEvent) {  
  38.             Message msg = (Message) event.getSource();  
  39.             ServerContext context = ServerContextFactory.get();  
  40.             //獲得客戶端所有chat頁面script session連接數  
  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.                 //執行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.                 //執行客戶端script session方法,相當于瀏覽器執行JavaScript代碼  
  57.                   //上面就會執行客戶端瀏覽器中的showMessage方法,并且傳遞一個對象過去  
  58.    
  59.                 session.addScript(sb);  
  60.             }  
  61.         }  
  62.     }  

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

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

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對象。里面的參數的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("發送的內容不能為空!");  
  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> 

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

上面的js中調用的charService類中的sendMessage方法,所以在jsp頁面中導入的是ChatService.js。

在body的onload事件中,需要設置反轉Ajax,這個很重要。

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

每當發送sendMessage方法后就會觸發ChatMessageEvent事件,然后監聽的地方就會執行onApplicationEvent方法,在這個方法中又會執行瀏覽器中的showMessage方法。

 

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

【編輯推薦】

  1. Oracle計劃修復Java SE中的17個漏洞
  2. Java SE 6新年***次更新 修復Bug超300個
  3. JDK 5和Java SE 6小更新
  4. Java SE 6更新 修復重大安全問題
  5. Java SE 6中的垃圾回收器G1收費是虛驚一場

 

 

責任編輯:艾婧 來源: hoojo的博客
相關推薦

2023-01-13 00:02:41

2023-01-05 09:17:58

2011-12-15 11:11:51

JavaNIO

2023-02-10 08:16:48

WebSocket簡易聊天室

2015-07-06 10:42:18

PHP聊天室應用

2022-07-26 14:53:10

WebSocket網絡通信協議

2025-05-09 08:35:00

聊天室FastAPIWebSocket

2021-11-16 09:38:10

鴻蒙HarmonyOS應用

2025-06-09 02:00:00

項目技術棧Spring

2022-11-14 08:01:48

2022-12-01 08:25:23

eTsTCP聊天室

2011-11-30 16:37:58

sync

2021-10-14 18:46:29

Websocket瀏覽器API

2015-08-06 17:17:33

swoole聊天室

2024-10-07 10:45:12

2022-04-18 10:36:48

社交軟件聊天平臺rocket.cha

2011-05-18 13:28:46

jQueryPHPAJAX

2021-02-06 23:26:25

聊天室開發WebSocket

2021-12-09 16:48:25

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久精品一区二区三区四区 | 国产一区二区三区四区在线观看 | 久久精品国产免费一区二区三区 | 99久久精品国产一区二区三区 | 国产精品99久久久久久宅男 | 青青草一区二区 | 国产精品夜色一区二区三区 | 久久久久国产一区二区三区 | 欧美一区二区三区 | 亚洲精品一区二区网址 | 午夜爽爽爽男女免费观看影院 | 亚洲永久免费 | 午夜精品久久久久久久久久久久久 | 91免费视频| 日韩免费一区二区 | 久久蜜桃资源一区二区老牛 | 人人玩人人添人人澡欧美 | 毛片一区二区三区 | 亚洲国产精品成人 | 欧美成人一区二区三区 | 亚洲精品视频在线看 | 日本一卡精品视频免费 | 精品无码久久久久久久动漫 | 在线国产中文字幕 | 中文字幕在线三区 | 自拍第1页 | 殴美一级片| 午夜日韩视频 | 一级日批片 | 国产羞羞视频在线观看 | 四虎成人免费视频 | 少妇一区在线观看 | 亚洲国产激情 | 黄色福利| 99免费精品视频 | 精品久久久久久亚洲精品 | 亚洲一区中文字幕 | 欧美一区二区在线观看 | 国产精品高潮呻吟久久av野狼 | 欧美成人久久 | 欧美日韩免费在线 |