最近项目有需要用到‘推’,就是服务器端自动把消息推给客户端,就做了个不像样的dwr实现的服务器推送demo,玩玩...
首先少不了dwr的jar包。
然后是在项目中建个dwr.xml的配置文件,当然,同时需要在web.xml中配置。
贴上代码:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>DwrServlet</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>pollAndCometEnabled</param-name> <param-value>true</param-value> </init-param> <load-on-startup>12</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DwrServlet</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
配置也没什么好解释的,
然后在工程中创建了两个类:
package dwr.reverseAjax; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpSession; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; import org.directwebremoting.proxy.dwr.Util; import org.directwebremoting.util.Logger; import dwr.reverse.StocksPusher; public class DWRAjaxReverse implements Runnable { public static List<DWRAjaxReverse> store = new ArrayList<DWRAjaxReverse>(); WebContext wctx = null; public String currentPage = ""; private static final Logger log = Logger.getLogger(StocksPusher.class); @SuppressWarnings("unchecked") public DWRAjaxReverse() { ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor( 1); executor.scheduleAtFixedRate(this, 1, 7000, TimeUnit.MILLISECONDS); wctx = WebContextFactory.get(); HttpSession session = wctx.getSession(); List<DWRAjaxReverse> list = (List<DWRAjaxReverse>) session .getAttribute("DWRHelper"); if (list == null) list = new ArrayList<DWRAjaxReverse>(); list.add(this); store.add(this); } @SuppressWarnings("unchecked") public void addMessage(String text) { LinkedList messages = new LinkedList(); if (text != null && text.trim().length() > 0) { messages.addFirst(new Message(text)); while (messages.size() > 10) { messages.removeLast(); } } wctx = WebContextFactory.get(); currentPage = wctx.getCurrentPage(); Util utilThis = new Util(wctx.getScriptSession()); utilThis.setValue("text", ""); Collection sessions = wctx.getScriptSessionsByPage(currentPage); Util utilAll = new Util(sessions); utilAll.removeAllOptions("chatlog"); // utilAll.addOptions("chatlog", messages, "text"); utilAll.addFunctionCall("reverseAjax", text); } @SuppressWarnings("unchecked") @Override public void run() { if (true) { log.info("run..."); Collection sessions = wctx.getScriptSessionsByPage(currentPage); Util utilAll = new Util(sessions); Date date = new Date(); LinkedList lst = new LinkedList(); lst.add(new Message(date.toString())); // utilAll.setValue("text", date.toString()); //utilAll.removeAllOptions("chatlog"); //utilAll.addOptions("chatlog", lst, "text"); utilAll.addFunctionCall("reverseAjax", date.toString()); log.info("current time is : "+date.toString()); } } }
package dwr.reverseAjax; import org.directwebremoting.Security; public class Message { private String text; private long id = System.currentTimeMillis(); public Message(String newText){ text = newText; if(text.length()>256){ text = text.substring(0,256); } text = Security.replaceXmlCharacters(text); } public String getText() { return text; } public void setText(String text) { this.text = text; } public void setId(long id) { this.id = id; } public long getId() { return id; } }
主要操作都在DWRAjaxReverse类中。
然后就是在dwr.xml中配置了,
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create creator="new" javascript="DWRAjaxReverse"> <param name="class" value="dwr.reverseAjax.DWRAjaxReverse" /> <include method="addMessage" /> </create> <convert converter="bean" match="dwr.reverseAjax.Message"> <param name="include" value="id,text" /> </convert> </allow> </dwr>
用过dwr的人这个配置你懂的!
现在写个简单的jsp,
<h1> DWR AjaxReverse </h1> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script type='text/javascript' src='/stock/dwr/engine.js'></script> <script type='text/javascript' src='/stock/dwr/util.js'></script> <script type='text/javascript' src='/stock/dwr/interface/DWRAjaxReverse.js'></script> <body onload="dwr.engine.setActiveReverseAjax(true);init()"> <script type="text/javascript"> function reverseAjax(messages){ document.getElementById('time_text').innerHTML= messages; } function init() { DWRAjaxReverse.addMessage("you are welcome!"); } </script> <hr /> <ul id="chatlog" style="list-style-type: none;" > </ul> <span id='time_text' ></span> </body>
不要问我/stock/dwr/engine.js 这样在工程中看不到的js在哪,玩dwr这个是要懂的(其实我也不懂,只知道dwr会自己虚拟的创建)
dwr.engine.setActiveReverseAjax(true) 这句在你的前台页面是不能少的
其中,reverseAjax这个function就是给后台调用的,后台调用前台
到此,这么个demo也算像个样子了