DWR实现后台推送消息到web页面

DWR简介

DWR(Direct Web Remoting)可用于实现javascript直接调用java函数和后台直接调用页面javascript代码,后者可用作服务端推送消息到Web前端。

(服务器推送技术又称为Comet技术,是继AJAX后又一热门技术)

 

以下链接可作简单了解

官网

http://directwebremoting.org/dwr/

JavaDoc

http://www.directwebremoting.org/dwr/javadoc/

入门讲解(前端)

http://directwebremoting.org/dwr/introduction/getting-started.html

入门讲解(后台)

http://directwebremoting.org/dwr/documentation/server/javaapi.html

下载地址

http://directwebremoting.org/dwr/downloads/index.html

 


 

需求场景

移动端用户向后台上传实时坐标后,后台需通知WEB页面在地图上实时更新用户位置。

 

实现过程

1、导入jar包(若用maven则添加依赖关系)

  地址: http://directwebremoting.org/dwr/downloads/index.html

 

2、web.xml中添加servlet

<servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>
        org.directwebremoting.servlet.DwrServlet
    </servlet-class>
    <init-param>
        <param-name>pollAndCometEnabled</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

 

3、编写前端

1)页面引入dwr相关js(无需导入js文件)

<script type='text/javascript' src='dwr/engine.js'></script> 
<script type='text/javascript' src='dwr/util.js'></script>

2)页面初始化时初始dwr参数

$(function(){
    initDwr(); 
});
//初始化dwr
function initDwr(){ 
    dwr.engine.setActiveReverseAjax(true);  //在web.xml配置无效只好在此配置
}

3)编写供后台调用的函数

//更新用户位置
function updateUserLocation(userid, longitude, latitude){
    mapMarkerList[userid].setPosition(new AMap.LngLat(longitude,latitude)); //更新地图Marker的位置
}

 

5、后台

1)编写DwrUtil工具类

public class DwrUtil {
    /**
     * 调用页面javascript函数
     * @param functionName
     * @param args
     */
    public void invokeJavascriptFunction (String _funcName, List _args){
        final String funcName = _funcName;
        final List args = _args;
        Browser.withAllSessions(new Runnable(){ 
            private ScriptBuffer script = new ScriptBuffer(); 
            public void run(){ 
                //拼接javascript
                script = script.appendScript(funcName+"(");
                for(int i=0; i<args.size(); i++){
                    if(i != 0){
                        script = script.appendScript(",");
                    }
                    script = script.appendData(args.get(i));
                }
                script.appendScript(")"); 
                //System.out.println(script.toString());   
                 
                Collection<ScriptSession> sessions = Browser.getTargetSessions(); 
                for (ScriptSession scriptSession : sessions){ 
                    scriptSession.addScript(script); 
                } 
            } 
        });
    }
}

2)业务逻辑中调用javascript代码实现推送功能

@RequestMapping(value = "api/apiBaseController/testUploadLocation.do")
@ResponseBody
public AjaxJson testUploadLocation(HttpServletRequest request) {
    // 参数获取
    String userid = request.getParameter("userid");
    String longitude = request.getParameter("longitude");
    String latitude = request.getParameter("latitude");
    //推送到web端
    DwrUtil t =  new DwrUtil();
    List args = new ArrayList();
    args.add(userid);
    args.add(longitude);
    args.add(latitude);
    t.invokeJavascriptFunction("updateUserLocation",args);
         
    AjaxJson ajaxjson = new AjaxJson();
    ajaxjson.setStatusmsg("上传成功");
    ajaxjson.setUsermsg("上传成功");
    return ajaxjson;
}

 

posted @ 2016-12-22 14:36 leapMie 阅读(...) 评论(...) 编辑 收藏