使用DWR实现消息推送功能
2018-02-28 18:10
627 查看
参考:http://blog.csdn.net/carefree31441/article/details/17225851
添加Maven依赖:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.directwebremoting/dwr --> <dependency> <groupId>org.directwebremoting</groupId> <artifactId>dwr</artifactId> <version>3.0.0-RELEASE</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> </dependencies>
新建一个ScriptSessionListener:监听ScriptSession的创建和销毁
public class DWRScriptSessionListener implements ScriptSessionListener { // 维护一个Map key为session的Id, value为ScriptSession对象 public static final Map<String, ScriptSession> scriptSessionMap = new HashMap<String, ScriptSession>(); public void sessionCreated(ScriptSessionEvent event) { WebContext webContext = WebContextFactory.get(); HttpSession session = webContext.getSession(); ScriptSession scriptSession = event.getSession(); scriptSession.setAttribute("userid", session.getId());//这里使用sesssion,使用时可存储为用户id后使用 scriptSessionMap.put(session.getId(), scriptSession); // 添加scriptSession System.out.println("session-created"+session.getId()); } public void sessionDestroyed(ScriptSessionEvent event) { WebContext webContext = WebContextFactory.get(); HttpSession session = webContext.getSession(); scriptSessionMap.remove(session.getId()); // 移除scriptSession } }
继承DefaultScriptSessionManager,添加ScriptSession监听器
public class DWRScriptSessionManager extends DefaultScriptSessionManager { public DWRScriptSessionManager(){ //绑定一个ScriptSession增加销毁事件的监听器 this.addScriptSessionListener( new DWRScriptSessionListener()); } }
Web全局配置:web.xml
<web-app> <display-name>DWR-START</display-name> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>org.directwebremoting.extend.ScriptSessionManager </param-name> <param-value>gdou.laiminghai.dwr.DWRScriptSessionManager </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app>
消息推送功能类
public class MessagePush { //改善给特定用户 public void sendToOne(final String userId, final String 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", message); Collection<ScriptSession> sessions = Browser.getTargetSessions(); for (ScriptSession scriptSession : sessions) { scriptSession.addScript(script); } } }); } //发送给所有用户 public void sendToAll(final String message) { Browser.withAllSessions(new Runnable() { public void run() { ScriptSessions.addFunctionCall("showMessage", message); } }); } }
DWR配置:在web.xml同一目录下新建dwr.xml
<dwr> <allow><!--定义了DWR能够创建和转换的类,以供 javascript 访问--> <create creator="new" javascript="MessagePush"> <param name="class" value="gdou.laiminghai.dwr.MessagePush" /> </create> </allow> </dwr>
页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <div>sessionid:<input id="sessionid" type="text">message:<input id="msg" type="text"> <input type="button" onclick="sendToOne();" value="发送"></div> <div>message:<input id="msg2" type="text"> <input type="button" onclick="sendToAll();" value="发送给所有人"></div> <script type='text/javascript' src='dwr/engine.js'></script> <script type='text/javascript' src='dwr/util.js'></script> <script type="text/javascript" src="dwr/interface/MessagePush.js"></script> <script type="text/javascript"> dwr.engine.setActiveReverseAjax(true); dwr.engine.setNotifyServerOnPageUnload(true); //回调函数 function showMessage(message) { alert(message); } function sendToOne(){ var sessionid = document.getElementById("sessionid").value; var msg = document.getElementById("msg").value; MessagePush.sendToOne(sessionid,msg); } function sendToAll(){ var msg2 = document.getElementById("msg2").value; MessagePush.sendToAll(msg2); } </script> </body> </html>
相关文章推荐
- 使用dwr实现后台消息推送功能
- Vue 使用 mqttws31.js 实现消息实时推送功能(WebSocket)
- 使用dwr3.0实现服务端向浏览器做消息推送,做滚动评论或弹幕效果,而且根据视频id做推送消息拦截功能
- DWR服务器推技术(实现消息实时推送功能)
- Android中使用WebSocket实现群聊和消息推送功能(不使用WebView)
- .Net使用SignalR实现消息推送功能预研及Demo 推荐
- 使用tomcat8.0.36实现的websocket技术,实现向单个以及全部用户实时推送消息的功能
- Android平台上使用MQTT协议实现消息推送功能
- 使用PopupWin控件实现消息推送功能
- 手机服务器Android消息推送(二)--基于MQTT协议实现的推送功能
- 如何使用androidpn实现android手机消息推送(简单的源码分析)
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 使用GCM服务(Google Cloud Messaging)实现Android消息推送
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 如何使用androidpn实现android手机消息推送(简单的源码分析)
- Android消息推送(二)--基于MQTT协议实现的推送功能
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- 如何使用androidpn实现android手机消息推送(简单的源码分析)