dwr使用

2019-09-28  本文已影响0人  不二不二熊
一、起始

尽管现在websocket通信十分流行,但是在很早之前的某个项目中还是使用到了dwr后台主动向前台推送消息,现在记录下用法。

二、使用
<dependency>
    <groupId>org.directwebremoting</groupId>
    <artifactId>dwr</artifactId>
    <version>3.0.2-RELEASE</version>
    <type>pom</type>
</dependency>
  <!-- DWR -->
  <servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
    <!-- <init-param> <param-name>config-1</param-name> <param-value>WEB-INF/dwr.xml</param-value>
        </init-param> -->
    <init-param>
      <param-name>debug</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>pollAndCometEnabled</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>classes</param-name>
      <param-value>java.lang.Object</param-value>
    </init-param>
    <init-param>
      <param-name>activeReverseAjaxEnabled</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>initApplicationScopeCreatorsAtStartup</param-name>
      <param-value>true</param-value>
    </init-param>
    <!-- polling方式:在comet方式的基础之上,再配置以下参数 -->
    <!-- <init-param>
         <param-name>org.directwebremoting.extend.ServerLoadMonitor</param-name>
         <param-value>org.directwebremoting.impl.PollingServerLoadMonitor</param-value>
     </init-param>-->

    <!-- 毫秒数。页面默认的请求间隔时间是5秒 -->
    <init-param>
      <param-name>disconnectedTime</param-name>
      <param-value>3000</param-value> <!-- 60000 -->
    </init-param>

    <init-param>
      <param-name>crossDomainSessionSecurity</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>allowScriptTagRemoting</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>
  <!-- DWR END -->
<script src="${rc.contextPath}/dwr/engine.js"></script>
<script src="${rc.contextPath}/dwr/util.js"></script>
···
   function addAlarm(info){
    var message = $.parseJSON(info);
       //业务逻辑
    }
...
$(function(){
        //消息推送初始化
        dwr.engine.setActiveReverseAjax(true);
        //设置在页面关闭时,通知服务端销毁会话
        dwr.engine.setNotifyServerOnPageUnload(true);
    });
Browser.withPage(ServerContextFactory.get().getContextPath()+pageUrl, new Runnable() {
            @Override
            public void run() {
                ScriptSessions.addFunctionCall(funcName, json);
            }
        });
上一篇下一篇

猜你喜欢

热点阅读