dwr 推送技术

1、web .xml 配置

<!-- DWR Configuration -->
	<servlet>
		<servlet-name>DwrServlet</servlet-name>
		<servlet-class>
			org.directwebremoting.servlet.DwrServlet
		</servlet-class>
		<init-param>
			<description>调试DWR,发布系统时应将其设为false</description>  
			<param-name>debug</param-name>
			<param-value>false</param-value>
		</init-param>
		<init-param>  
            <description>使用服务器推技术(反转AJAX)</description>  
            <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>  
        <init-param>  
            <param-name>maxWaitAfterWrite</param-name>  
            <param-value>100</param-value>  
        </init-param>  
        <load-on-startup>4</load-on-startup>  
	</servlet>
	<servlet-mapping>
		<servlet-name>DwrServlet</servlet-name>
		<url-pattern>/static/dwr/*</url-pattern>
	</servlet-mapping>


2、新建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>  
        <convert converter="bean" match="com.ziteng.qyh.util.DwrRevice"/>  
        <create creator="new" javascript="PushAjaxMsg">  
             <param name="class" value="com.ziteng.qyh.util.PushAjaxMsg"/>  
        </create>  
    </allow>  
</dwr>  

3、 下载 dwr.jar包

4、新建类DwrRevice.java


package com.ziteng.qyh.util;

public class DwrRevice {
	private String msg;
	private Object obj;
	private String ret;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public String getRet() {
		return ret;
	}

	public void setRet(String ret) {
		this.ret = ret;
	}
}
5、 推送类 PushAjaxMsg.java 

package com.ziteng.qyh.util;

import java.util.Collection;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;

public class PushAjaxMsg {
	
	public void sendMessageAuto(String userid, String message) {

		final String userId = userid;
		final String autoMessage = message;
		Browser.withAllSessionsFiltered(new ScriptSessionFilter() {
			public boolean match(ScriptSession session) {
				if(userId != null ){
					return true;
				} else{
					return false;
				}
			}
		}, new Runnable() {

			private ScriptBuffer script = new ScriptBuffer();

			public void run() {

				script.appendCall("showMessage", autoMessage);

				Collection<ScriptSession> sessions = Browser.getTargetSessions();

				for (ScriptSession scriptSession : sessions) {
					scriptSession.addScript(script);
				}
			}
		});
	}

}

6、触发推送信息


PushAjaxMsg pAjaxMsg = new PushAjaxMsg();
		String autoMsg = "{\"ret\":1,\"exam_id\":\"" + exam.getId()
		+ "\",\"state\":\"upd_exam\",\"contacts_id\":" + contacts_id + 
		",\"end_time\":\"" + endTime+":00" + "\"}";
        pAjaxMsg.sendMessageAuto(String.valueOf(contacts_id), autoMsg);		


7、js 接收推送消息 pushMsg.js


window.onload = init;//页面加载完毕后执行初始化方法init 
var incomplete = "Incomplete reply from server";
/**
 * 页面 初始化信息
 */
function init() {  
//	console.debug(JSON.parse("{'ret':'0','exam_id':'fd88bc2c-319e-4189-9518-7be3d499fccc','state':'sub_exam','contacts_id':135}"))
	dwr.engine.setActiveReverseAjax(true); // 激活反转 重要  
}  

//推送信息
function showMessage(autoMessage){
	if( incomplete != autoMessage ){
		console.debug('msg',autoMessage)
		var msg = JSON.parse(autoMessage);
		var localObj = window.location;
		var contextPath = localObj.pathname.split("/")[1];
		var path = localObj.protocol+"//"+localObj.host;
		if(msg.ret == 0){  //  考生交卷信息
			$('.student li').each(function(index){
				if( $(this).attr('id')==msg.contacts_id && $(this).find('.up').length > 0) {
					// $(this).append('<img class="up" src='+path+'/images/exam/up_papers.png alt="">');
					$(this).find('.up').css("display","block");
				}
			});
		} else if(msg.ret == 1){  // 考生交卷时间
			if(msg.end_time){
				var startDate= new Date();
				if(typeof(msg.end_time)=="string") {
					endDate=new Date(msg.end_time.replace(/-/g,'/'));
		        }
				var interval=(endDate-startDate)/(1000*60);
				var min=parseInt(interval);
				var sec=parseInt((interval-min)*60);
				if(parseInt(interval) > 0){
					$('#second').html(sec);
					$('#minute').html(min);
					if(time){
						clearInterval(time);
						stopWatch();
					}
				}
			}
		}
		
	}
	
}
/** 
 * 发送消息 
 */  
function send() {  
	var examId = $('#examId').val();
	if(examId){
		var msgObj = {};
		msgObj.ret = "0";
		msgObj.exam_id = examId;
		msgObj.contacts_id = 234;
		msgObj.state = "sub_exam";
		var msg = JSON.stringify(msgObj);
	    PushAjaxMsg.sendMessageAuto('11',msg); // 发送消息  
	}
} 





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值