dwr push技术,基于长连接

DWRpush技术是让服务器每次发送广播时,把这个广播推送给每个客户端,不用客户端去刷新,据说DWR的推送是基于长连接的,性能不错。可以在后台通过 ScriptSession 来执行一些 javascript 的 function 或者是 style 等操作,可以在服务器上将一些数据推送到客户端机器上。

修改web.xml,在dwrservlet中启用dwrReverse Ajax,即加入

<init-param>
            
<param-name>activeReverseAjaxEnabled</param-name>
            
<param-value>true</param-value>
</init-param>

Web.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      version="2.4"

      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

      <servlet>

            <servlet-name>dwr</servlet-name>

            <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

            <init-param>

                  <param-name>debug</param-name>

                  <param-value>true</param-value>

            </init-param>

            <init-param>

                  <param-name>activeReverseAjaxEnabled</param-name>

                  <param-value>true</param-value>

            </init-param>

            <init-param>

                  <param-name>crossDomainSessionSecurity</param-name>

                  <param-value>false</param-value>

            </init-param>

      </servlet>

      <servlet>

            <servlet-name>Sender</servlet-name>

            <servlet-class>com.dwr.servlet.Sender</servlet-class>

      </servlet>

 

      <servlet-mapping>

            <servlet-name>dwr</servlet-name>

            <url-pattern>/dwr/*</url-pattern>

      </servlet-mapping>

      <servlet-mapping>

            <servlet-name>Sender</servlet-name>

            <url-pattern>/notify.do</url-pattern>

      </servlet-mapping>

 

      <welcome-file-list>

            <welcome-file>index.jsp</welcome-file>

      </welcome-file-list>

</web-app>

 

创建一个请求页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

      <head>

            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

            <title>index页面</title>

      </head>

      <body>

            <form action="notify.do">

                  <input type="text" name="message" />

                  <input type="submit" value="提交">

            </form>

      </body>

</html>

创建一个发送广播的Servlet

packagecom.dwr.servlet;

 

importjava.io.IOException;

importjava.util.Collection;

 

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

importorg.directwebremoting.ScriptBuffer;

importorg.directwebremoting.ScriptSession;

importorg.directwebremoting.ServerContext;

importorg.directwebremoting.ServerContextFactory;

 

public class Sender extends HttpServlet {

 

      private static final long serialVersionUID = 221673813297384002L;

 

      protected voiddoGet(HttpServletRequest request,

                  HttpServletResponse response) throws ServletException, IOException {

            ServerContext serverContext = ServerContextFactory

                        .get(getServletContext());

            Collection<ScriptSession> scriptSessions = serverContext

                        .getAllScriptSessions();

 

            // Collection<ScriptSession> sessions =

            // wctx.getScriptSessionsByPage("/poject/test.html");

 

            String message = request.getParameter("message");

            if (message == null) {

                  message = "empty";

            }

            ScriptBuffer scriptBuffer = new ScriptBuffer();

            scriptBuffer.appendScript("showMessage(").appendData(message)

                        .appendScript(");");

            for (ScriptSession scriptSession : scriptSessions) {

                  scriptSession.addScript(scriptBuffer);

            }

      }

 

}

添加一个html页面,用来接收服务端广播

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

      <head>

            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

            <title>Client1的页面</title>

            <script type='text/javascript' src='dwr/engine.js'></script>

            <script type="text/javascript">

                  dwr.engine.setActiveReverseAjax(true);

                  functionshowMessage(msg) {

                        alert(msg);

                  }

            </script>

      </head>

      <body>

      </body>

</html>

打开添加的html页面,然后让Sender发送一个广播, html页面就会自动弹出一个javascript对话框,到此,dwr的推送就完成了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值