本文主要是介绍使用DWR反转实现信息推送(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
除了利用Pushlet实现信息推送外,DWR反转同样可以实现推送。
DWR的简单配置方法已经在以前的博客中写过。所以这里直接贴代码:
新建web工程,这里命名为DwrEg,包结构如下:
web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <servlet> <servlet-name>dwr-invoke</servlet-name> <servlet-class>uk.ltd.getahead.dwr.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>classes</param-name> <param-value>java.lang.Object</param-value> </init-param> --> </servlet> <servlet-mapping> <servlet-name>dwr-invoke</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>client.html</welcome-file> </welcome-file-list> </web-app>
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 javascript="myrevsrse" creator="new"> <param name="class" value="com.src.MyReverse"/> </create> </allow> </dwr>
MyReverse.java代码如下:
package com.src;
import java.util.Collection;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContext;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
public class MyReverse {
public void sendMes(String mes){
System.out.println("kao shi si bestllll...");
send("系统消息:"+mes);
}
public void send(final String output) {
org.directwebremoting.WebContext web = WebContextFactory.get();
String page = web.getServletContext().getContextPath()+"/client.html";
Collection sessions = web.getScriptSessionsByPage(page);
System.out.println("size=="+sessions.size());
Util utilAll = new Util(sessions);
utilAll.addFunctionCall("callBack", output); //callBack是client.jsp里面的javascript函数,output是传递过去的参数
//utilAll.setValue("news_id", output, false);
//这种方法也可以,直接将client.html里id为news_id的textarea的值设置为output的值
}
public void noticeNewOrder(int id) {
WebContext wctx = WebContextFactory.get();
ScriptBuffer script = new ScriptBuffer();
script.appendScript("receiveMessages(")
.appendData(id)
.appendScript(");");
ServerContext sctx = ServerContextFactory.get(wctx.getServletContext());
Collection<ScriptSession> pages = sctx.getScriptSessionsByPage("/DwrEg/client.html");
for (ScriptSession session : pages) {
session.addScript(script);
}
}
}
client.html代码如下:
<html>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<head>
<script type="text/javascript">
function callBack(data){
alert(data);
}
function receiveMessages(id) {
$('orderNotice').innerHTML = "收到id为" + id + "的新订单!";
$('orderNotice').show();
}
</script>
</head>
<!-- dwr.engine.setActiveReserseAjax(true);这句必须加上,不然的话,不能控制反转 -->
<body οnlοad="dwr.engine.setActiveReverseAjax(true);">
<textarea rows="20" cols="20" id="news_id"></textarea>
<br />
<div id="orderNotice"></div>
</body>
</html>
推送界面dopush.html的代码如下:
<html>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/myrevsrse.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript">
function sendnews(){
var new_content = dwr.util.getValue("newcontent");
myrevsrse.sendMes(new_content);
}
function sendinfo(){
myrevsrse.noticeNewOrder(12);
}
</script>
</head>
<body>
<input type="text" name="newcontent">
<input type = "button" value="发消息" οnclick="sendnews()"/>
<br />
<input type = "button" value="send 2" οnclick="sendinfo()"/>
<br />
</body>
同样,这里的问题也是怎么实现点对点的信息推送,这儿只是实现了向所有的clent.html页面推送信息,以后接着总结。。。。
转自:http://tonl.iteye.com/blog/1399027
这篇关于使用DWR反转实现信息推送(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!