dwr 推送消息

dwr + spring  实时推送消息,通过网络技术学习,做了个小功能,总结如下:


1、下载dwr.jar

官网地址:http://directwebremoting.org/dwr/downloads/

下载最新版本v3.0


2、导入jar

   

3、配置web.xml

<!-- DWR配置 START -->
<servlet>
	<description>This is the description of my J2EE component</description>
	<display-name>This is the display name of my J2EE component</display-name>
	
	<servlet-name>DWRServlet</servlet-name>
	<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
	<!--commet 方式-->
	<init-param>
		<param-name>activeReverseAjaxEnabled</param-name>
		<param-value>true</param-value>
	</init-param>
	<!--测试环境下,需要开启debug模式,线上环境需要关闭-->  
	<init-param>
		<param-name>debug</param-name>
		<param-value>false</param-value>
	</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>
</servlet>

<servlet-mapping>
	<servlet-name>DWRServlet</servlet-name>
	<url-pattern>/dwr/*</url-pattern>
</servlet-mapping> 

<!-- DWR配置 END -->

4、由Spring管理dwr

配置applicationContext-common.xml 

<!--由spring管理dwr   -->
<bean id="deviceInfo" class="com.dc.rcms.DWRDeviceInfoStatus"></bean>


5、页面引用以下js

<!--dwr消息推送 -->
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>

并且页面 初始化,加

$(function(){    
    dwr.engine.setActiveReverseAjax(true);  
    dwr.engine.setNotifyServerOnPageUnload(true);
});

6、后台直接要推送的内容

public DWRDeviceInfoStatus(){
	//定时向前台推送设备信息
	Timer timer = new Timer();
	timer.schedule(new TimerTask() {
		@Override
		public void run() {
		<span style="white-space:pre">	</span>try{
				ServerContext serverContext = ServerContextFactory.get();
				Container container = serverContext.getContainer();
				ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
				Collection sessions = manager.getAllScriptSessions();
				if(sessions.size() == 0 ){
					return;
				}
				//得到设备部位信息状态数量
				Util util = new Util(sessions);
				List<SEntity> list=baseDao.search("select count(id) as allnum,sum(case when status=1 then 1 end) as gznum,sum(case when status=0 then 1 end) as zcnum from rcms_device ",null);
				if(list!=null && list.size()>0){
					SEntity entity=list.get(0);
					String jsonStr="{\"allnum\":"+repaceNull(entity.getValueAsString("allnum"))
							+",\"gznum\":"+repaceNull(entity.getValueAsString("gznum"))
							+",\"zcnum\":"+repaceNull(entity.getValueAsString("zcnum"))+"}";
					util.addFunctionCall("showDeviceInfo", jsonStr);
				}
				//得到所有故障点信息
				Util utilgzd = new Util(sessions);
				List<SEntity> gzlist=baseDao.search("select device_code,count(id) n from rcms_device where status =1 group by device_code order by device_code", null);
				if(gzlist!=null && gzlist.size()>0){
					String gzdjsonStr="[";
					for(int i=0;i<gzlist.size();i++){
						SEntity gzdentity=gzlist.get(i);
						gzdjsonStr += "{";
						gzdjsonStr +="\"device_code\":\""+repaceNull(gzdentity.getValueAsString("DEVICE_CODE"))+"\",";
						gzdjsonStr +="\"gzdnum\":\""+repaceNull(gzdentity.getValueAsString("n"))+"\"";
						gzdjsonStr +="},";
					}
					gzdjsonStr = gzdjsonStr.substring(0,gzdjsonStr.length()-1);
					gzdjsonStr = gzdjsonStr+"]" ;
					utilgzd.addFunctionCall("showDeviceGzdInfo", gzdjsonStr);
				}
				
			}catch(Exception e){
				
			}
		}
	}, 10*1000, 1000*3);
	
}	


7、页面接收后台推送的消息

脚本处理

/* 后台自动推送设备信息 */
//显示设备正常与否信息
function showDeviceInfo(data){
	var jsonstr = eval( "(" + data + ")" );
	if(jsonstr!=null){
		$("#bottomcount").html("<p style=\"margin-left: 20px;margin-top: 20px; font-size: 16px;\">共<span id=\"allnum\" style=\"margin-left: 10px;margin-right: 10px;\">"+jsonstr.allnum+"</span>个   正常<span id=\"zcnum\" style=\"margin-left: 10px;margin-right: 10px;\"><a class=\"icon\" target=\"dialog\" rel=\"main\" title=\"设备部位的正常数量\" width=\"850\" height=\"500\" οnclick=\"getsitezc_gznum(0);\"><font color=\"green\">"+jsonstr.zcnum+"</font></a></span>个   故障<span id=\"gznum\" style=\"margin-left: 10px;margin-right: 10px;\"><a class=\"icon\" target=\"dialog\" rel=\"main\" title=\"设备部位码的故障数量\" width=\"850\" height=\"500\"οnclick=\"getsitezc_gznum(1);\"><font color=\"red\">"+jsonstr.gznum+"</font></a></span>个</p>");
	}
}
//显示所有故障点信息
function showDeviceGzdInfo(data){
	var gzdjsonstr = eval( "(" + data + ")" );
	if(gzdjsonstr!=null){
		$("#gzdcount").html("<ul>");
		for(var i=0; i<gzdjsonstr.length; i++) { 
			$("#gzdcount").append("<a target=\"dialog\" rel=\"main\" title=\"设备部位的故障点数量\" width=\"850\" height=\"500\" οnclick=\"getsitegzdnum('"+gzdjsonstr[i].device_code+"');\"><li style=\"list-style-type:none;\"><img alt=\"警示\" src=\"img/warning.png\"/><span id=\"gzsnum\" style=\"margin-left: 5px;margin-top: 20px; font-size: 14px;\">"+gzdjsonstr[i].device_code+"  (  故障  <span style=\"margin-left: 5px;margin-right: 5px;margin-top: 10px;\"><font color=\"red\" >"+gzdjsonstr[i].gzdnum + "</font></span>个)</span></li></a></ul>");
		}
	}
	isfirst=2;
}

页面如下




至此,大功告成。。。




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值