dwr3推送

来源:互联网 发布:中国网络被黑客攻击 编辑:程序博客网 时间:2024/06/04 17:50

最近研究dwr3。网上大多是dwr2,为了防止以后用到的时候忘记,写个blog记录下。研究有新发现会不定时更新。


dwr3下载地址: http://directwebremoting.org/dwr/downloads/index.html   需要jar包,war包运行后,部分配置文件可以复制。

或者我已经打包好的jar+war: http://download.csdn.net/detail/s594656726/6619513

导入jar包:commons-logging-1.0.4.jar(从war包中可以获得),dwr.jar。

配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <display-name></display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <listener>    <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>  </listener>    <servlet>    <servlet-name>dwr-invoker</servlet-name>    <display-name>DWR Servlet</display-name>    <description>Direct Web Remoter Servlet</description>    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class><init-param>     <param-name>fileUploadMaxBytes</param-name>     <param-value>25000</param-value>    </init-param>    <!-- This should NEVER be present in live -->    <init-param>      <param-name>debug</param-name>      <param-value>true</param-value>    </init-param>  <init-param>      <param-name>accessLogLevel</param-name>      <param-value>runtimeexception</param-value>    </init-param>        <!-- Remove this unless you want to use active reverse ajax -->    <init-param>      <param-name>activeReverseAjaxEnabled</param-name>      <param-value>true</param-value>    </init-param>    <!-- By default DWR creates application scope objects when they are first    used. This creates them when the app-server is started -->    <init-param>      <param-name>initApplicationScopeCreatorsAtStartup</param-name>      <param-value>true</param-value>    </init-param>    <!-- WARNING: allowing JSON-RPC connections bypasses much of the security    protection that DWR gives you. Take this out if security is important -->    <init-param>      <param-name>jsonRpcEnabled</param-name>      <param-value>true</param-value>    </init-param>    <!-- WARNING: allowing JSONP connections bypasses much of the security    protection that DWR gives you. Take this out if security is important -->    <init-param>      <param-name>jsonpEnabled</param-name>      <param-value>true</param-value>    </init-param>    <!-- data: URLs are good for small images, but are slower, and could OOM for    larger images. Leave this out (or keep 'false') for anything but small images -->    <init-param>      <param-name>preferDataUrlSchema</param-name>      <param-value>false</param-value>    </init-param>    <!--    For more information on these parameters, see:    - http://getahead.org/dwr/server/servlet    - http://getahead.org/dwr/reverse-ajax/configuration    -->    <load-on-startup>1</load-on-startup>  </servlet>     <servlet-mapping>    <servlet-name>dwr-invoker</servlet-name>    <url-pattern>/dwr/*</url-pattern>  </servlet-mapping></web-app>

web.xml同级目录下新建dwr.xml,参考war包中的dwr.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"><dwr><allow><create creator="new" javascript="SendMsg"><param name="class" value="com.demo.SendMsg" /></create><!-- this is a bad idea for live, but can be useful in testing --><convert converter="exception" match="java.lang.Exception" /><convert converter="bean" match="java.lang.StackTraceElement" /></allow></dwr>
新建个类,负责创建监听器

public class ScriptUtil extends DwrServlet {/** *  */private static final long serialVersionUID = -168439468012960941L;public void init() throws ServletException {Container container = ServerContextFactory.get().getContainer();ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);ScriptSessionListener listener = new ScriptSessionListener() {@Overridepublic void sessionDestroyed(ScriptSessionEvent arg0) {// TODO Auto-generated method stubSystem.out.println("a ScriptSession is distroyed");System.out.println("distroyed ScriptSession id:"+arg0.getSession().getId());System.out.println("--------------------------");}@Overridepublic void sessionCreated(ScriptSessionEvent arg0) {// TODO Auto-generated method stubSystem.out.println("a ScriptSession is created");System.out.println("created ScriptSession id:"+arg0.getSession().getId());System.out.println("--------------------------");}};//设置监听,dwr3才有manager.addScriptSessionListener(listener);}}

实现推送类,我使用的是推送到指定页面

public class SendMsg {public void send(String Msg) {final String FMsg = Msg;//获取指定页面路径String page = WebContextFactory.get().getContextPath() + "/index.jsp";Browser.withPageFiltered(page, new ScriptSessionFilter() {@Overridepublic boolean match(ScriptSession arg0) {// TODO Auto-generated method stub//设置推送条件HttpSession session = WebContextFactory.get().getSession();if (session == null) {return false;} else {return true;}}}, new Runnable() {//推送线程@Overridepublic void run() {// TODO Auto-generated method stubScriptSessions.addFunctionCall("getMsg", FMsg);}});       }}

接收页面js设置

<script type='text/javascript' src='<%=basePath%>dwr/util.js'></script><script type='text/javascript' src='<%=basePath%>dwr/engine.js'></script>dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true);
写一个js函数负责接收推送信息
function getMsg(Msg){alert(Msg);}

项目运行后,输入项目路径/dwr  进入配置的modules页面
本文配置的为 SendMsg ,点击进入后在send方法中输入推送内容,点击execute进行推送(推送前先打开指定的接收页面。)
如果需要推送页面,只需要在推送页面配置如下,然后调用推送方法即可

<script type='text/javascript' src='<%=basePath%>dwr/util.js'></script><script type='text/javascript' src='<%=basePath%>dwr/engine.js'></script><script type='text/javascript' src='<%=basePath%>dwr/interface/SendMsg.js'></script>

原创粉丝点击