DWR长连接

web.xml

  <servlet>
        <servlet-name>dwr-invoke</servlet-name>
        <servlet-class> uk.ltd.getahead.dwr.DWRServlet </servlet-class>
        <init-param> 
           <param-name> debug </param-name>
           <param-value> true </param-value>
        </init-param>
        <init-param> <!-- 这个是DWR2.0必须的,不然会报java.lang.IllegalArgumentException -->
           <param-name> classes </param-name>
           <param-value> java.lang.Object </param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dwr-invoke</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>

在Web-INF下创建dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
<dwr>
	<allow>
		<convert match="com.relonger.ccljent.entity.Message" converter="bean">  <!--javaBean-->
			<param name="include" value="msg,time" />
		</convert>
		<create creator="spring" javascript="ChatService">
			<param name="beanName" value="chatService" />
		</create>
	</allow> 
</dwr> 

  ChatService  服务端

public class ChatService implements ApplicationContextAware {
	private ApplicationContext ctx;
	@Override
	public void setApplicationContext(ApplicationContext ctx)
			throws BeansException {
		this.ctx = ctx;
	}
	/**
	 * <b>function:</b> 向服务器发送信息,服务器端监听ChatMessageEvent事件,当有事件触发就向所有客户端发送信息
	 * * @author hoojo
	 * * @createDate 2011-6-8 下午12:37:24
	 * * @param msg
	 * */
	public void sendMessage(Message msg) {
		//发布事件
		ctx.publishEvent(new ChatMessageEvent(msg));
	}
}

  ChatMessageClient  客户端

public class ChatMessageClient implements ApplicationListener,
		ServletConfigAware {
	private ServletContext ctx;
	public ServletContext getCtx() {
		return ctx;
	}
	public void setCtx(ServletContext ctx) {
		this.ctx = ctx;
	}
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		//如果事件类型是ChatMessageEvent就执行下面操作
		if (event instanceof ChatMessageEvent) {
			Message msg = (Message) event.getSource();
			ServerContext context = ServerContextFactory.get();
			//获得客户端所有chat页面script session连接数
			if(ctx != null){
				System.out.print("==========");
			}
			 WebContext contex = WebContextFactory.get();  
			 if(contex != null){
				 System.out.print("====>>>>>>>");
			 }
			 
			Collection<ScriptSession> sessions = context.getScriptSessionsByPage("/DWR/chat.jsp");
//			Collection<ScriptSession> sessions = context.getScriptSessionsByPage(ctx.getContextPath() + "/chat.jsp");
			for (ScriptSession session : sessions) {
				ScriptBuffer sb = new ScriptBuffer();
				Date time = msg.getTime();
				String s = time.getYear() + "-" + (time.getMonth() + 1) + "-" +  time.getDate() + " "
				+  time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
				//执行setMessage方法
				sb.appendScript("showMessage({msg: '")
				.appendScript(msg.getMsg()).appendScript("', time: '")
				.appendScript(s)
				.appendScript("'})");
				System.out.println(sb.toString());
				//执行客户端script session方法,相当于浏览器执行JavaScript代码
				//上面就会执行客户端浏览器中的showMessage方法,并且传递一个对象过去
				session.addScript(sb);
				}
			}  
	}
	@Override
	public void setServletConfig(ServletConfig arg0) {
		// TODO Auto-generated method stub

	}
}

  

 

 

转载于:https://www.cnblogs.com/fengyu9/p/3630551.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值