dwr 推送技术

来源:互联网 发布:淘宝上高夫假货多吗 编辑:程序博客网 时间:2024/05/17 17:39

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
原创粉丝点击