一个关于dwr实现服务器推技术的问题 求解
我想实现一个客户端登陆后 然后服务器主动给客户端发消息的功能 但是消息死活推不过去
代码如下
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 把Web应用根目录以属性名添加到系统参数中 --> <listener> <listener-class> org.directwebremoting.servlet.DwrListener </listener-class> </listener> <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> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <!-- dwr配置文件路径 --> <param-name>config</param-name> <param-value>/WEB-INF/dwr.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.msg.web.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> </web-app>
dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd"> <dwr> <allow> <!-- chat --> <create creator="new" javascript="MsgService"> <param name="class" value="com.msg.service.MsgService" /> </create> </allow> </dwr>
MsgService.java
package com.msg.service; import org.directwebremoting.Browser; import org.directwebremoting.ServerContextFactory; import org.directwebremoting.ui.dwr.Util; public class MsgService { public void sendMsg(final String msg) { // 向指定页面推送消息 Browser.withPage(ServerContextFactory.get().getContextPath()+"/client.jsp",new Runnable() { public void run() { Util.setValue("msgBox", msg); } }); } }
client.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type='text/javascript' src='dwr/engine.js'></script> <script type='text/javascript' src='dwr/interface/MsgService.js'></script> <script type='text/javascript' src='dwr/util.js'></script> </head> <body onload="dwr.engine.setActiveReverseAjax(true);"> <br> <br/> <div align="center"> <span>当前登录用户名:</span>${userName} <p> Server Message: </p> <hr /> </div> <div id="msgBox"></div> </body> </html>
server.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type='text/javascript' src='dwr/engine.js'></script> <script type='text/javascript' src='dwr/interface/MsgService.js'></script> <script type='text/javascript' src='dwr/util.js'></script> <script type="text/javascript"> dwr.engine.setActiveReverseAjax(true); function sendMessage(){ var message = dwr.util.getValue("msg"); MsgService.sendMsg(message); } </script> </head> <body> <br> <br/> <div align="center"> <p> Your Message: <input id="msg" /> <input type="button" value="Send" onclick="sendMessage()" /> </p> </div> </body> </html>
这是大部分实现代码 实在不明白为什么推不出去了 希望做过这方面的大神提示一下下
感谢
另外我用 Browser.withCurrentPage() 实现了客户端直接的信息传递 即聊天室功能
但是服务器主动给客户端发送信息死活不行。。。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Goeasy
也挺不错的,专业做
web
实时推送,服务器稳定,代码简洁易懂,还有中英文,网址是
https://goeasy.io/
普通的浏览器没有websocket,http协议又是无状态的,dwr的那种推送本质来说,也是轮询吧。只不过服务器端是阻塞式的输出,没消息的时候一直是卡住的,有消息了再马上输出,变相的一种推送了。客户端没对应请求过去,怎么也出不了东西了。
官方应该有demo吧。
这玩意我没整过,以上只是理论。
感觉用轮询的方式可能会成功