dwr 推送技术

dwr3 推送到指定登录用户。

下载一个dwr.jar包,放到lib下:

首先是web.xml配置文件,在此文件中加入

<servlet>

<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<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>
        <init-param>
    <param-name>classes</param-name>
    <param-value>java.lang.Object</param-value>
        </init-param>
<init-param>
<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>3000</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>WARN</param-value>
        </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

接着是dwr.xml文件,和web.xml在同一目录下

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE dwr PUBLIC
          "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
          "http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<create creator="new" javascript="MessagePush">
<param name="class">com.aurora.dwr.MessagePush</param>
</create>
</allow>
</dwr>   

接着就是实现类:MessagePush.java

package com.aurora.dwr;




import java.util.Collection;


import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;


import org.directwebremoting.Browser;
import org.directwebremoting.Container;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ScriptSessionFilter;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.extend.RealScriptSession;
import org.directwebremoting.extend.ScriptSessionManager;


import com.aurora.mschool.entity.User;




public class MessagePush {
public void onPageLoad(String userId) {
HttpSession session = WebContextFactory.get().getSession();
Container container = ServerContextFactory.get().getContainer();
ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
Collection<RealScriptSession> sss = manager.getScriptSessionsByHttpSessionId(session.getId());
for (RealScriptSession realScriptSession : sss) {
ScriptSession scriptSession = realScriptSession;
scriptSession.setAttribute("userId", userId);
}

}


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 (session.getAttribute("userId") == null)
return false;
else
return (session.getAttribute("userId")).equals(userId);
}
}, 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);
}
}

});
}


}

页面index.jsp 为:

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript" src="<%=basePath%>dwr/engine.js"></script>
 <script type="text/javascript" src="<%=basePath%>dwr/util.js"></script>
  <script type="text/javascript" src="<%=basePath%>dwr/interface/MessagePush.js"></script>

<script src="<%=basePath%>public/javascript/jquery.js"></script>

<SCRIPT type="text/javascript">
$(function(){
  dwr.engine.setActiveReverseAjax(true);
  //dwr.engine.setNotifyServerOnPageUnload(true);
       MessagePush.onPageLoad(${session.user.id });
       dwr.engine._errorHandler = function(message, ex) {
       dwr.engine._debug("Error: " + ex.name + ", " + ex.message, true);
       }; 
      
  });

function showMessage(content){
  if(content != null){
  alert(content);
  }
  }

</SCRIPT>

而我们的推送要经过一个action,比如说:http://localhost:8080/demo/push.action?id=8

public void push()throws Exception{
MessagePush push = new MessagePush();
User user = new User();
user.setId(id);
int newpush = this.messageService.findUnreadMessagesCounts(user);
System.out.println(newpush+"------------"+user+"----================="+user.getId());
push.sendMessageAuto(String.valueOf(id), String.valueOf(newpush));
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值