DWRプッシュ--HelloWorld
13928 ワード
<dependency>
<groupId>org.directwebremotinggroupId>
<artifactId>dwrartifactId>
<version>3.0.1-RELEASEversion>
dependency>
<servlet>
<servlet-name>dwr-invokeservlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServletservlet-class>
<init-param>
<param-name>debugparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>activeReverseAjaxEnabledparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartupparam-name>
<param-value>trueparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>dwr-invokeservlet-name>
<url-pattern>/dwr/*url-pattern>
servlet-mapping>
<dwr>
<allow>
<create creator="new" javascript="CheckUserStatus">
<param name="class" value="com.lb.mil.service.CheckUserStatusService"/>
create>
allow>
dwr>
package com.lb.mil.service;
import com.alibaba.fastjson.JSON;
import com.lb.common.data.json.StatusResult;
import com.lb.common.utils.ObjectUtils;
import com.lb.mil.cache.EHCacheConfig;
import com.lb.mil.cache.EHCacheUtil;
import com.lb.mil.cache.data.UserState;
import com.lb.mil.entity.MilInformation;
import com.lb.mil.entity.MilInformationAttach;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Created by Administrator on 2016/7/18.
*/
public class CheckUserStatusService
{
protected Logger logger = LoggerFactory.getLogger(CheckUserStatusService.class);
public void check()
{
//
//
ScriptBuffer scriptBuffer = new ScriptBuffer(); // js
WebContext webContext= WebContextFactory.get();
String currentPage = webContext.getCurrentPage();
ScriptSession myScSession = webContext.getScriptSession();
if(ObjectUtils.isNotNull(offLineIds)||ObjectUtils.isNotNull(onlineIds))
{
// show
scriptBuffer.appendScript("show(")
.appendData(JSON.toJSONString(statusResult))
.appendScript(");");
// session
Collection sessions = webContext.getScriptSessionsByPage(currentPage);
// session
//ScriptSession sessions = webContext.getScriptSession();
Util util = new Util(sessions);
util.addScript(scriptBuffer);
}
}
}
<script type='text/javascript' src='${pageContext.request.contextPath }/dwr/engine.js'>script>
<script type='text/javascript' src='${pageContext.request.contextPath }/dwr/util.js'>script>
<script type='text/javascript' src='${pageContext.request.contextPath }/dwr/interface/CheckUserStatus.js'>script>
<body onload="dwr.engine.setActiveReverseAjax(true);dwr.engine.setNotifyServerOnPageUnload(true,true);dwr.engine.setErrorHandler(function(){});">
function show(msg){
//
}
//
setInterval(getUserStatus,5000);//1000 1
/**
*
*/
function getUserStatus()
{
//check CheckUserStatusService ,
CheckUserStatus.check();
}