package com.socket.servlet;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;
import org.apache.tomcat.util.codec.binary.Base64;
import com.socket.util.CutomerListVO;
import com.socket.util.MessageUtil;
import com.socket.util.MessageVo;
import com.socket.util.PublicMessageInbound;
@SuppressWarnings("deprecation")
public class MyWebSocketServlet extends WebSocketServlet {
/**
*
*/
private static final long serialVersionUID = -6488889268352650321L;
@Override
protected StreamInbound createWebSocketInbound(String subProtocol,HttpServletRequest request) {
final String gbType = request.getParameter("gbType"); //default 0 staff ,1 customer
final String isQuery = request.getParameter("isQuery"); //default 0 find 1send
final String fromId = request.getParameter("fromId");
return new MessageInbound() {
@Override
protected void onBinaryMessage(ByteBuffer byteBuffer) throws IOException {
int len = byteBuffer.limit() - byteBuffer.position();
byte[] bytes = new byte[len];
byteBuffer.get(bytes);
new Base64().encodeToString(bytes);
}
@SuppressWarnings("unchecked")
@Override
protected void onTextMessage(CharBuffer msg) throws IOException{
Boolean isStaff = Boolean.valueOf(gbType.equals("0"));
Boolean isFind = Boolean.valueOf(isQuery.equals("0"));
MessageVo messageVo = MessageUtil.getMessage(msg); //处理消息类
Integer fromId = messageVo.getFromId();
//如果是客服网页地址
if("0".equals(gbType)){
LinkedHashMap<Integer, PublicMessageInbound> socketStaffMap = MyHttpServlet.getSocketStaffMap();
PublicMessageInbound staffMessageInbound = socketStaffMap.get(fromId);
//如果是查询
if(isFind){
Integer waitCount =-2;
Object content =messageVo.getContent();
//如果有客服已经登录且在会话中 返回会话的地址
if(staffMessageInbound!=null&&staffMessageInbound.getIsLasy()){
waitCount =-1;
content = content.toString().split("toId=")[0]+"toId="+staffMessageInbound.getLasyToId();
WsOutbound findListWsOutBound = socketStaffMap.get(999999).getMessageInbound().getWsOutbound();
MessageVo data = MessageUtil.createMessage(0,true,content,waitCount);
String jsonMsg = MessageUtil.createResp(10, "open talk", data).toString();
CharBuffer fromMsg = CharBuffer.wrap(jsonMsg .toCharArray());
findListWsOutBound.writeTextMessage(fromMsg);
findListWsOutBound.flush();
}else{
//如果内容是空 返回在线客户列表
if(content==null){
waitCount =0;
Map<String,Object> rst = MessageUtil.findWaitCustomer();
content = rst.get("vosWait");
List<CutomerListVO> vosStaffClose = (List<CutomerListVO>) rst.get("vosStaffClose");
for(CutomerListVO vo :vosStaffClose){
PublicMessageInbound staffCloseCustomerMessageInbound = MyHttpServlet.getSocketCustomerMap().get(vo.getId());
if(staffCloseCustomerMessageInbound!=null){
WsOutbound findListWsOutBound = staffCloseCustomerMessageInbound.getMessageInbound().getWsOutbound();
MessageVo data= MessageUtil.createMessage(0,false,content,-3);
String jsonMsg = MessageUtil.createResp(4, "staff closed", data).toString();
CharBuffer fromMsg = CharBuffer.wrap(jsonMsg .toCharArray());
findListWsOutBound.writeTextMessage(fromMsg);
findListWsOutBound.flush();
}
}
WsOutbound findListWsOutBound = socketStaffMap.get(999999).getMessageInbound().getWsOutbound();
MessageVo data = MessageUtil.createMessage(0,true,content,waitCount);
String jsonMsg = MessageUtil.createResp(6, "customer list", data).toString();
CharBuffer fromMsg = CharBuffer.wrap(jsonMsg .toCharArray());
findListWsOutBound.writeTextMessage(fromMsg);
findListWsOutBound.flush();
}else{
WsOutbound findListWsOutBound = socketStaffMap.get(999999).getMessageInbound().getWsOutbound();
MessageVo data = MessageUtil.createMessage(0,true,content,waitCount);
String jsonMsg = MessageUtil.createResp(5, "open talk", data).toString();
CharBuffer fromMsg = CharBuffer.wrap(jsonMsg .toCharArray());
findListWsOutBound.writeTextMessage(fromMsg);
findListWsOutBound.flush();
}
}
}else{
//如果是客服会话
if(staffMessageInbound!=null){
WsOutbound fromWsOutBound = staffMessageInbound.getMessageInbound().getWsOutbound();
//如果客服空闲或者客服交谈的客户是当前需要产生会话的人
Integer toId = messageVo.getToId();
//如果客服空闲或者 忙碌的人是当前客户
if(!staffMessageInbound.getIsLasy()||(staffMessageInbound.getIsLasy()&&staffMessageInbound.getLasyToId().intValue()==toId.intValue())){
LinkedHashMap<Integer, PublicMessageInbound> socketCustomerMap = MyHttpServlet.getSocketCustomerMap();
PublicMessageInbound customerMessageInbound = MyHttpServlet.getSocketCustomerMap().get(toId);
//客户是否断开
if(customerMessageInbound!=null){
//如果客户空闲或者忙碌的人是当前客服 可以进行消息传输
if(!customerMessageInbound.getIsLasy()||(customerMessageInbound.getIsLasy()&&customerMessageInbound.getLasyToId().intValue()==fromId.intValue())){
customerMessageInbound.setIsLasy(true);
customerMessageInbound.setLasyToId(fromId);
MyHttpServlet.getSocketCustomerMap().put(toId, customerMessageInbound);
staffMessageInbound.setIsLasy(true);
staffMessageInbound.setLasyToId(toId);
MyHttpServlet.getSocketStaffMap().put(fromId, staffMessageInbound);
WsOutbound toWsOutBound = customerMessageInbound.getMessageInbound().getWsOutbound();
if(staffMessageInbound!=null&&socketCustomerMap!=null){
int waitCount = 0;
Object content = messageVo.getContent();
MessageVo fromData= MessageUtil.createMessage(toId,content==null?0:1, true, content, waitCount);
MessageVo toData= MessageUtil.createMessage(fromId,content==null?0:1, true, content, waitCount);
String jsonFromMsg = MessageUtil.createResp(0, "staff", fromData).toString();
String jsonToMsg = MessageUtil.createResp(0, "customer", toData).toString();
CharBuffer fromMsg = CharBuffer.wrap(jsonFromMsg.toCharArray());
CharBuffer toMsg = CharBuffer.wrap(jsonToMsg .toCharArray());
// 消息发送到前端
fromWsOutBound.writeTextMessage(fromMsg);
fromWsOutBound.flush();
toWsOutBound.writeTextMessage(toMsg); //
toWsOutBound.flush();
}
}else{
MessageVo data= MessageUtil.createMessage(isStaff, "用户会话已经被其他客服接入");
String jsonMsg = MessageUtil.createResp(3, "socket in other staff", data).toString();
CharBuffer fromMsg = CharBuffer.wrap(jsonMsg.toCharArray());
fromWsOutBound.writeTextMessage(fromMsg);
fromWsOutBound.flush();
}
}else{
MessageVo data= MessageUtil.createMessage(isStaff, "用户已经断开");
String jsonMsg = MessageUtil.createResp(2, "socket in other staff", data).toString();
CharBuffer fromMsg = CharBuffer.w
没有合适的资源?快使用搜索试试~ 我知道了~
java在线客服聊天
共49个文件
class:9个
java:8个
xml:5个
1星 需积分: 33 84 下载量 72 浏览量
2019-03-08
10:30:34
上传
评论 7
收藏 660KB RAR 举报
温馨提示
客服和客户之间的交流工具 实时聊天 ,公司做了伪在线客服系统。自己为了技术,提前研究了使用websocket进行长连接通信。写了个简单的在线聊天demo,算是对自己的交代,后期会抽时间优化,完善流程等。
资源推荐
资源详情
资源评论
收起资源包目录
WebSocketUser.rar (49个子文件)
WebSocketUser
src
com
socket
util
MessageVo.java 1KB
MessageUtil.java 5KB
PublicMessageInbound.java 873B
WaitCountUtil.java 2KB
CutomerListVO.java 456B
RespVO.java 674B
servlet
MyWebSocketServlet.java 12KB
MyHttpServlet.java 2KB
.classpath 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.prefs.xml 161B
org.eclipse.wst.common.project.facet.core.xml 450B
org.eclipse.m2e.core.prefs 90B
org.eclipse.core.resources.prefs 77B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 582B
.jsdtscope 522B
org.eclipse.jdt.core.prefs 430B
org.eclipse.wst.validation.prefs 50B
pom.xml 1KB
target
classes
com
socket
util
MessageVo.class 2KB
MessageUtil.class 7KB
RespVO.class 1KB
CutomerListVO.class 1KB
PublicMessageInbound.class 2KB
WaitCountUtil.class 3KB
servlet
MyWebSocketServlet.class 1KB
MyWebSocketServlet$1.class 9KB
MyHttpServlet.class 1KB
test-classes
m2e-jee
web-resources
META-INF
MANIFEST.MF 106B
maven
webSocket
webSocket
pom.properties 261B
pom.xml 1KB
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
js
global-socket.js 4KB
staffTalk.js 1KB
jquery-1.7.2.min.js 93KB
staff.js 2KB
user.js 1KB
css
onlineConsultation.css 10KB
staffTalk.jsp 1KB
user.jsp 2KB
staff.jsp 2KB
img
service.jpeg 21KB
bg_face_64.png 13KB
bg.jpg 390KB
guest.jpeg 16KB
WEB-INF
classes
lib
gson-2.2.2.jar 185KB
web.xml 1KB
共 49 条
- 1
资源评论
- javaee12342019-06-25这个是tomcat什么版本的
kevin0212
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功