java 网站用户在线和客服聊天


注:本文来源于《java 网站用户在线和客服聊天》



这是应用到项目中的一个例子。

实现原理是将信息存储到Application域里面。然后使用Struts2 Action 用json格式的数据进行前后台交互。

截图:

前台用户界面:

20130521081322386

后台客服界面:

20130521081348963

编辑框采用的Ueditor 可以发送图片表情文字格式、涂鸦。。。。等等!功能十分的强大!

代码部分:

20130521081505375

用到了两个Action,分别为用户和管理员服务。

Struts.xml


	<package name="chat" extends="struts-default,json-default">
		
		<action name="chatMessageSend" class="com.shop.chat.MessageAction" method="send">
			<result name="success" type="json">result>
		action>

		
		<action name="chatMessageGet" class="com.shop.chat.MessageAction">
			<result type="json">result>
		action>

		
		<action name="adminMessageGet" class="com.shop.chat.AdminMessageAction">
			<result name="success" type="json">result>
		action>

		
		<action name="adminMessageSend" class="com.shop.chat.AdminMessageAction" method="send">
			<result type="json">result>
		action>

		
		<action name="adminList" class="com.shop.chat.AdminListAction">
			<result type="json">result>
		action>

	package>


Java 类:

package com.shop.chat.admin;

import java.util.ArrayList;
import java.util.List;

import com.shop.bean.Admin;

/**
 * 存放当前已经登陆的管理员列表
 * @author Pan
 *
 */
public class AdminList {

	private List list=new ArrayList();
	public AdminList(){

	}

	/**
	 * 增加Admin对象
	 * @param admin
	 */
	public void save(Admin admin){
		if(list.indexOf(admin)==-1){
			list.add(admin);
		}
	}

	/**
	 * 得到所有Admin
	 * @return
	 */
	public List getList(){
		return list;
	}
}



package com.shop.chat.admin;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import com.shop.bean.Admin;

/**
 * admin列表服务
 * @author Pan
 *
 */
public class AdminListServler {

	private AdminList list;

	public AdminListServler(HttpSession session){
		ServletContext context=session.getServletContext();
		list=(AdminList)context.getAttribute("adminlist");
		if(list==null){
			list=new AdminList();
			context.setAttribute("adminlist", list);
		}
	}
	/**
	 * add an admin to the list
	 * @param admin
	 */
	public void save(Admin admin){
		list.save(admin);
	}

	/**
	 * get all admin object
	 * @return arraylist
	 */
	public List getList(){
		return list.getList();
	}
}
package com.shop.chat.admin;

import java.sql.Timestamp;

public class AdminSimplify {

	private Integer id;
	private String adminName;
	private String adminPass;
	private String nickName;
	private String head;
	private Timestamp addTime;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getAdminName() {
		return adminName;
	}
	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}
	public String getAdminPass() {
		return adminPass;
	}
	public void setAdminPass(String adminPass) {
		this.adminPass = adminPass;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getHead() {
		return head;
	}
	public void setHead(String head) {
		this.head = head;
	}
	public Timestamp getAddTime() {
		return addTime;
	}
	public void setAddTime(Timestamp addTime) {
		this.addTime = addTime;
	}

}
package com.shop.chat;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shop.bean.Admin;
import com.shop.chat.admin.AdminListServler;
import com.shop.chat.admin.AdminSimplify;

public class AdminListAction extends ActionSupport {

	private List list;

	public List getList() {
		return list;
	}
	/**
	 * 得到当前登陆的管理员
	 * @return
	 */
	public String execute(){

		list=new ArrayList();
		AdminListServler aServler=new AdminListServler(ServletActionContext.getRequest().getSession());
		for (Admin admin : aServler.getList()) {

			AdminSimplify as=new AdminSimplify();
			as.setAddTime(admin.getAddTime());
			as.setAdminName(admin.getAdminName());
			as.setAdminPass(admin.getAdminPass());
			as.setHead(admin.getHead());
			as.setId(admin.getId());
			as.setNickName(admin.getNickName());
			list.add(as);
		}

		return SUCCESS;
	}
}
package com.shop.chat;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shop.bean.Admin;

public class AdminMessageAction extends ActionSupport {

	private String to;		//接受用户
	private String result;	//返回消息
	private String content;	//消息内容
	private String nickName;	//昵称
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getNickName() {
		return nickName;
	}
	public void setContent(String content) {
		this.content = content;
	}
	private List list;

	public List getList() {
		return list;
	}

	public void setResult(String result) {
		this.result = result;
	}
	public String getResult() {
		return result;
	}
	public void setTo(String to) {
		this.to = to;
	}


	public String execute(){
		Admin admin=(Admin)ServletActionContext.getRequest().getSession().getAttribute("admin");
		if(admin==null){
			setResult("你还没有登录!");
			return SUCCESS;
		}
		MessageServer msServer=new MessageServer(ServletActionContext.getRequest().getSession(),admin.getId()+"",admin.getNickName());
		list=msServer.getMessage();
		return SUCCESS;
	}

	/**
	 * 发送消息
	 * @return
	 */
	public String send(){
		Admin admin=(Admin)ServletActionContext.getRequest().getSession().getAttribute("admin");
		if(admin==null){
			setResult("没有登录");
			return SUCCESS;
		}

		MessageServer msServer=new MessageServer(ServletActionContext.getRequest().getSession(), admin.getId()+"",admin.getNickName());
		try {
			msServer.send(to, content);
			setResult("发送成功!");
			setNickName(admin.getNickName());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result=e.getMessage();
		}
		return SUCCESS;
	}


}
package com.shop.chat;

/**
 * 信息容器
 * @author Pan
 *
 */

public class Message {

	private String form; // 来自用户
	private String to; // 接受用户
	private String message; // 信息
	private String nickName;	//昵称

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getNickName() {
		return nickName;
	}
	public String getForm() {
		return form;
	}

	public void setForm(String form) {
		this.form = form;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
package com.shop.chat;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shop.bean.User;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class MessageAction extends ActionSupport {

	private String to;			//传送到指定的用户
	private String result;		//返回信息
	private String content;		//内容
	private String nickName;	//昵称
	private List list;	//消息集合

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getNickName() {
		return nickName;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public List getList() {
		return list;
	}
	public String getResult() {
		return result;
	}
	public void setTo(String to) {
		this.to = to;
	}
	/**
	 * 获取消息
	 */
	public String execute(){

		HttpServletRequest request=ServletActionContext.getRequest();
		User user=(User)request.getSession().getAttribute("user");
		MessageServer msServer=new MessageServer(request.getSession(), user.getId()+"",user.getNickName());
		this.list=msServer.getMessage();

		return SUCCESS;
	}

	/**
	 * 发送消息
	 * @return
	 */
	public String send(){

		HttpServletRequest request=ServletActionContext.getRequest();
		User user=(User)request.getSession().getAttribute("user");
		if(user==null){
			result="你还没有进行登录!";
			return SUCCESS;
		}
		MessageServer mServer=new MessageServer(request.getSession(),user.getId()+"",user.getNickName());
		mServer.getMessage();

		try {
			mServer.send(to, content);

			System.out.println("to:"+to);
			setNickName(user.getNickName());
			setResult("发送成功");
		} catch (Exception e) {
			setResult("发送失败!");
			result=e.getMessage();
		}
		return SUCCESS;
	}
}
package com.shop.chat;

import java.util.ArrayList;
import java.util.List;

/**
 * 信息集合 这个对象储存到Application中
 * @author Pan
 *
 */
public class MessageList {

	private List list=new ArrayList();

	public MessageList(){

	}

	/**
	 * 添加信息
	 * @param message
	 */
	public void add(Message message){
		list.add(message);
	}

	/**
	 * 寻找信息 通过接受的用户
	 * @param uid
	 * @return
	 */
	public List getByTo(String uid){
		List tmp=new ArrayList();
		System.out.println("list的大小:"+list.size());
		for(Message message:list){
			if(message.getTo().equals(uid)){
				//添加到返回对象
				tmp.add(message);
			}
		}
		//然后移除信息 以免信息重复
		list.removeAll(tmp);
		return tmp;
	}

}
package com.shop.chat;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

public class MessageServer {

	private MessageList mList;
	private ServletContext context;
	private String uid;
	private String nickname;
	public MessageServer(HttpSession session,String uid,String nickname){

		this.context=session.getServletContext();

		Object object=context.getAttribute("mlist");
		if(object==null){
			mList=new MessageList();
			context.setAttribute("mlist", mList);
		}else {
			this.mList=(MessageList)object;
		}
		this.uid=uid;
		this.nickname=nickname;
	}
	/**
	 * 发送数据
	 * @throws Exception
	 */
	public void send(String to,String content) throws Exception{

		if(uid==null){
			throw new Exception("用户名id无效");
		}

		Message message=new Message();
		message.setForm(uid);
		message.setTo(to);
		message.setMessage(content);
		message.setNickName(this.nickname);
		mList.add(message);

	}

	/**
	 * 接受信息
	 * @return
	 */
	public List getMessage(){
		//返回当前用户的信息
		System.out.println("uid是:"+uid);
		return mList.getByTo(uid);
	}

}
package com.shop.chat.admin;

import java.util.ArrayList;
import java.util.List;

import com.shop.bean.Admin;

/**
 * 存放当前已经登陆的管理员列表
 * @author Pan
 *
 */
public class AdminList {

	private List list=new ArrayList();
	public AdminList(){

	}

	/**
	 * 增加Admin对象
	 * @param admin
	 */
	public void save(Admin admin){
		if(list.indexOf(admin)==-1){
			list.add(admin);
		}
	}

	/**
	 * 得到所有Admin
	 * @return
	 */
	public List getList(){
		return list;
	}
}
package com.shop.chat.admin;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import com.shop.bean.Admin;

/**
 * admin列表服务
 * @author Pan
 *
 */
public class AdminListServler {

	private AdminList list;

	public AdminListServler(HttpSession session){
		ServletContext context=session.getServletContext();
		list=(AdminList)context.getAttribute("adminlist");
		if(list==null){
			list=new AdminList();
			context.setAttribute("adminlist", list);
		}
	}
	/**
	 * add an admin to the list
	 * @param admin
	 */
	public void save(Admin admin){
		list.save(admin);
	}

	/**
	 * get all admin object
	 * @return arraylist
	 */
	public List getList(){
		return list.getList();
	}
}
package com.shop.chat.admin;

import java.sql.Timestamp;

public class AdminSimplify {

	private Integer id;
	private String adminName;
	private String adminPass;
	private String nickName;
	private String head;
	private Timestamp addTime;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getAdminName() {
		return adminName;
	}
	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}
	public String getAdminPass() {
		return adminPass;
	}
	public void setAdminPass(String adminPass) {
		this.adminPass = adminPass;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getHead() {
		return head;
	}
	public void setHead(String head) {
		this.head = head;
	}
	public Timestamp getAddTime() {
		return addTime;
	}
	public void setAddTime(Timestamp addTime) {
		this.addTime = addTime;
	}

}

你可能感兴趣的:(java 网站用户在线和客服聊天)