java 網站用戶在線和客服聊天


注:本文來源於《java 網站用戶在線和客服聊天java



這是應用到項目中的一個例子。sql

實現原理是將信息存儲到Application域裏面。而後使用Struts2 Action 用json格式的數據進行先後臺交互。apache

截圖:json

前臺用戶界面:session

後臺客服界面:網站

編輯框採用的Ueditor 能夠發送圖片表情文字格式、塗鴉。。。。等等!功能十分的強大!ui

代碼部分:this

用到了兩個Action,分別爲用戶和管理員服務。spa

Struts.xml.net

<!-- 聊天功能實現  -->
	<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<Admin> list=new ArrayList<Admin>();
	public AdminList(){

	}

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

	/**
	 * 獲得全部Admin
	 * @return
	 */
	public List<Admin> 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<Admin> 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<AdminSimplify> list;

	public List<AdminSimplify> getList() {
		return list;
	}
	/**
	 * 獲得當前登錄的管理員
	 * @return
	 */
	public String execute(){

		list=new ArrayList<AdminSimplify>();
		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<Message> list;

	public List<Message> 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<Message> 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<Message> 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<Message> list=new ArrayList<Message>();

	public MessageList(){

	}

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

	/**
	 * 尋找信息 經過接受的用戶
	 * @param uid
	 * @return
	 */
	public List<Message> getByTo(String uid){
		List<Message> tmp=new ArrayList<Message>();
		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<Message> 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<Admin> list=new ArrayList<Admin>();
	public AdminList(){

	}

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

	/**
	 * 獲得全部Admin
	 * @return
	 */
	public List<Admin> 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<Admin> 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;
	}

}
相關文章
相關標籤/搜索