欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

推荐实用的在线客服系统PHP开源代码,包含安装指南及永久使用权

最编程 2024-07-21 13:28:15
...
<span style="color:#314659"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#4078f2 !important">package</span> com.qianlong.controller;

<span style="color:#4078f2 !important">import</span> com.qianlong.service.SelectService;
<span style="color:#4078f2 !important">import</span> org.springframework.beans.factory.annotation.Autowired;
<span style="color:#4078f2 !important">import</span> org.springframework.stereotype.Component;

<span style="color:#4078f2 !important">import</span> java.io.IOException;
<span style="color:#4078f2 !important">import</span> java.text.SimpleDateFormat;
<span style="color:#4078f2 !important">import</span> java.util.Date;
<span style="color:#4078f2 !important">import</span> java.util.HashMap;
<span style="color:#4078f2 !important">import</span> java.util.Map;
<span style="color:#4078f2 !important">import</span> javax.websocket.OnClose;
<span style="color:#4078f2 !important">import</span> javax.websocket.OnMessage;
<span style="color:#4078f2 !important">import</span> javax.websocket.OnOpen;
<span style="color:#4078f2 !important">import</span> javax.websocket.Session;
<span style="color:#4078f2 !important">import</span> javax.websocket.server.PathParam;
<span style="color:#4078f2 !important">import</span> javax.websocket.server.ServerEndpoint;
<span style="color:#999999 !important"><em>/**
* 聊天室的服务端程序
* <span style="color:#c678dd">@author</span> Administrator
*
*/</em></span>
<span style="color:#999999 !important"><em>//声明websocket某个服务端的地址</em></span>
<span style="color:#fd971f !important">@ServerEndpoint(value = "/charRoomServer/{param}")</span>
<span style="color:#fd971f !important">@Component</span>
<span style="color:#4078f2 !important">public</span> <span style="color:#4078f2 !important">class</span> <span style="color:#50a14f !important">ChatRoomServer</span> {
<span style="color:#fd971f !important">@Autowired</span>
<span style="color:#4078f2 !important">public</span> <span style="color:#4078f2 !important">static</span> SelectService selectService;
<span style="color:#4078f2 !important">private</span> <span style="color:#c18401 !important">boolean</span> firstFlag=<span style="color:#ae81ff !important">true</span>;
<span style="color:#4078f2 !important">private</span> Session session;
<span style="color:#4078f2 !important">private</span> String userName;

<span style="color:#999999 !important"><em>//发送人id</em></span>
<span style="color:#4078f2 !important">private</span> String userId;
<span style="color:#999999 !important"><em>//key代表此次客户端的userId,value代表此次连接对象</em></span>
<span style="color:#4078f2 !important">private</span> <span style="color:#4078f2 !important">static</span> <span style="color:#4078f2 !important">final</span> HashMap<String, Object> connectMap=<span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">HashMap</span><String, Object>();
<span style="color:#999999 !important"><em>//保存所有用户昵称信息</em></span>
<span style="color:#999999 !important"><em>//key是session的id,value是用户昵称</em></span>
<span style="color:#4078f2 !important">private</span> <span style="color:#4078f2 !important">static</span> <span style="color:#4078f2 !important">final</span> HashMap<String, String> userMap=<span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">HashMap</span><String, String>();
<span style="color:#999999 !important"><em>//服务端收到客户端的连接请求,连接成功后会执行此方法</em></span>
<span style="color:#fd971f !important">@OnOpen</span>
<span style="color:#4078f2 !important">public</span> <span style="color:#4078f2 !important">void</span> <span style="color:#50a14f !important">start</span>(<span style="color:#fd971f !important">@PathParam(value = "param")</span> String param, Session session) {
<span style="color:#50a14f !important">this</span>.session=session;
<span style="color:#50a14f !important">this</span>.userId=param; <span style="color:#999999 !important"><em>//接收参数</em></span>
connectMap.put(param,<span style="color:#50a14f !important">this</span>);
}

<span style="color:#999999 !important"><em>//客户端发来的信息,服务端接收</em></span>
<span style="color:#fd971f !important">@OnMessage</span> <span style="color:#999999 !important"><em>//接收人userId</em></span>
<span style="color:#4078f2 !important">public</span> <span style="color:#4078f2 !important">void</span> <span style="color:#50a14f !important">chat</span>(String clientMessage,Session session) {
<span style="color:#999999 !important"><em>//firstFlag为true是第一次进入,第二次进入之后设为false</em></span>
ChatRoomServer client=<span style="color:#ae81ff !important">null</span>;
<span style="color:#4078f2 !important">if</span>(firstFlag) {
<span style="color:#50a14f !important">this</span>.userName=clientMessage;
<span style="color:#999999 !important"><em>//将新进来的用户保存到用户map</em></span>
userMap.put(session.getId(), userName);

<span style="color:#4078f2 !important">try</span> {
<span style="color:#4078f2 !important">if</span>(<span style="color:#c18401 !important">"超级管理员"</span>.equals(userId)){

}<span style="color:#4078f2 !important">else</span>{
<span style="color:#999999 !important"><em>//构造发送给客户端的提示信息</em></span>
String message=htmlMessage(<span style="color:#c18401 !important">"大白机器人:"</span>,<span style="color:#c18401 !important">"亲爱的"</span>+userId+<span style="color:#c18401 !important">",您想了解点儿啥?"</span>);
client=(ChatRoomServer) connectMap.get(userId);
<span style="color:#999999 !important"><em>//给对应的web端发送一个文本信息</em></span>
client.session.getBasicRemote().sendText(message);
}
} <span style="color:#4078f2 !important">catch</span> (IOException e) {
e.printStackTrace();
}
firstFlag=<span style="color:#ae81ff !important">false</span>;
}<span style="color:#4078f2 !important">else</span> {
System.err.println(<span style="color:#c18401 !important">"clientMessage:"</span>+userName);
<span style="color:#999999 !important"><em>//给对方发消息</em></span>
String message1=htmlMessage(userId,clientMessage);
client = (ChatRoomServer) connectMap.get(userName);
<span style="color:#4078f2 !important">if</span>(client!=<span style="color:#ae81ff !important">null</span>){
<span style="color:#4078f2 !important">try</span> {
client.session.getBasicRemote().sendText(message1);
} <span style="color:#4078f2 !important">catch</span> (IOException e) {
e.printStackTrace();
}
}
<span style="color:#999999 !important"><em>//给自己窗口发消息</em></span>
String message2=htmlMessage(userId,clientMessage);
client = (ChatRoomServer) connectMap.get(userId);
<span style="color:#4078f2 !important">try</span> {
client.session.getBasicRemote().sendText(message2);
} <span style="color:#4078f2 !important">catch</span> (IOException e) {
e.printStackTrace();
}

<span style="color:#999999 !important"><em>//这是将前台用户发送的消息存数据库并标记为未读,和上面通信没关系</em></span>
<span style="color:#4078f2 !important">if</span>(<span style="color:#c18401 !important">"超级管理员"</span>.equals(userId)){

}<span style="color:#4078f2 !important">else</span>{
Map map=<span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">HashMap</span>();
map.put(<span style="color:#c18401 !important">"account"</span>,userId);
map.put(<span style="color:#c18401 !important">"message"</span>,clientMessage);
map.put(<span style="color:#c18401 !important">"addtime"</span>,<span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">Date</span>());
<span style="color:#c18401 !important">int</span> <span style="color:#c18401 !important">i</span> <span style="color:#ab5656">=</span> selectService.chatInsert(map);
System.out.println(i);
}
}
}

<span style="color:#999999 !important"><em>/**
* 前台js的ws.close事件,会触发后台的标注onClose的方法
*/</em></span>
<span style="color:#fd971f !important">@OnClose</span>
<span style="color:#4078f2 !important">public</span> <span style="color:#4078f2 !important">void</span> <span style="color:#50a14f !important">close</span>() {
userMap.remove(session.getId());
connectMap.remove(userId);
}
<span style="color:#999999 !important"><em>/**
* 渲染页面,把信息构造好标签再发送
*/</em></span>
<span style="color:#4078f2 !important">public</span> String <span style="color:#50a14f !important">htmlMessage</span>(String userName,String message) {
StringBuffer stringBuffer=<span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">StringBuffer</span>();
SimpleDateFormat sf=<span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">SimpleDateFormat</span>(<span style="color:#c18401 !important">"yyyy-MM-dd HH:mm:ss"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"<article>"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"<span>"</span>+sf.format(<span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">Date</span>())+<span style="color:#c18401 !important">"</span>"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"<div class='avatar'>"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"<h3>"</span>+userName+<span style="color:#c18401 !important">"</h3>"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"</div>"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"<div class='msg'>"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"<div class='tri'></div>"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"<div class='msg_inner'>"</span>+message+<span style="color:#c18401 !important">"</div>"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"</div>"</span>);
stringBuffer.append(<span style="color:#c18401 !important">"</article>"</span>);
<span style="color:#999999 !important"><em>//这里拼接了消息发送人的userId,在前台进行截取字符串接收发送人的userId</em></span>
stringBuffer.append(<span style="color:#c18401 !important">"|"</span>+userName);
<span style="color:#4078f2 !important">return</span> stringBuffer.toString();
}
}
</code></span></span>