用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java“服务推送”实现网页即时聊天,增强用户体验

2013-08-01 作者: 蓝色理想举报

[java]代码库

package wyf;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dojox.cometd.Bayeux;
import dojox.cometd.Channel;


/**
 * Project:AjaxCometChat <br>
 * Module: <br>
 * Description: <br>
 * Version:1.0 <br>
 * Date:2013-7-31 <br>
 * Copyright (C) 2012  <br>
 *
 * @author:beijing_qi
 */
public class AjaxReceiveServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		ServletContext application=this.getServletContext();
		Bayeux b=(Bayeux)application.getAttribute(Bayeux.DOJOX_COMETD_BAYEUX);
		List<String> zxlb=(List<String>)application.getAttribute("zxlb");
		if(null==zxlb){
			zxlb=new ArrayList<String>();
		}
		
		String action=request.getParameter("action");
		if(action!=null){
			if(action.equals("nc")){
				String nc=request.getParameter("nc");
				if(zxlb.contains(nc)){
					out.print("<#nccf#>");
				}else{
					zxlb.add(nc);
					application.setAttribute("zxlb", zxlb);
					out.print("<#ncok#>");
					Channel c=b.getChannel("/Manage", true);
					Map<String,Object> message=new HashMap<String,Object>();
					String lbStr="";
					for(String tempNc:zxlb){
						lbStr=lbStr+tempNc+"<#>";
					}
					message.put("lbStr", lbStr);
					message.put("pubStr", nc+"%u8fdb%u5165%u804a%u5929%u5ba4%uff01\n");
					c.publish(b.newClient("server_user"), message, "new_server_message");
					
					
				}
			}else if(action.equals("tc")){
				String nc=request.getParameter("nc");
				zxlb.remove(nc);
				out.print("<#tcok#>");
				Channel c=b.getChannel("/Manage", true);
				Map<String,Object> message=new HashMap<String,Object>();
				String lbStr="";
				for(String tempNc:zxlb){
					lbStr=lbStr+tempNc+"<#>";
					
				}
				message.put("lbStr", lbStr);
				message.put("pubStr", nc+"%u9000%u51fa%u804a%u5929%u5ba4%uff01\n");
				c.publish(b.newClient("server_user"), message, "new_server_message");
			}
		}
		
	}

}

[代码运行效果截图]


java“服务推送”实现网页即时聊天,增强用户体验


网友评论    (发表评论)

共5 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...