好用的在线客服系统PHP源码(开源代码+终身使用+安装教程)

好用的在线客服系统PHP源码(开源代码+终身使用+安装教程)​在线客服系统是一套交互式沟通工具,采用PHP+MYSQL开发。高性能,不卡顿。使用它可以迅速缩小你的选择范围,联系多个供应商、客户等,也可以给你的企业一个关于用户体验的重大影响。

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

​在线客服系统是一套交互式沟通工具,采用PHP+MYSQL开发。高性能,不卡顿。使用它可以迅速缩小你的选择范围,联系多个供应商、客户等,也可以给你的企业一个关于用户体验的重大影响。(尾部下载完整版PHP客服系统源码)
在这里插入图片描述
在这里插入图片描述

一,思路梳理

1,首先思考群聊的实现方式。
​2,再来举一反三,思考单聊的实现方式。
有一个地方需要提一下,比如要给A用户发送消息,就要根据A的userId得到连接对象,然后就会把消息发送给A用户。

二,代码分析

1,首先是用户聊天框的js代码(html忽略)
此为用户点击在线客服按钮进入聊天框页面就执行的js代码。

说明:下面接收人id写死了为超级管理员,我这里省事情了,因为超级管理员的账号(也就是唯一标识)就是超级管理员这五个字,当真正开发时就要动态获取唯一标识了!

<span style="color:#314659"><span style="background-color:#ffffff"><code class="language-javascript language-js"><script type=<span style="color:#c18401 !important">"text/javascript"</span>>
	<span style="color:#999999 !important"><em>//1,获取连接,new WebSocket()</em></span>
	<span style="color:#999999 !important"><em>//获取到路径url的参数(用户的手机号,这里将手机号作为了用户的唯一标识)</em></span>
    <span style="color:#4078f2 !important">var</span> accounta=<span style="color:#c18401 !important">window</span>.location.search;
    <span style="color:#4078f2 !important">var</span> account=accounta.<span style="color:#50a14f !important">slice</span>(<span style="color:#ae81ff !important">9</span>);
    <span style="color:#999999 !important"><em>//发送人id</em></span>
    <span style="color:#4078f2 !important">var</span> sb=account;
	<span style="color:#999999 !important"><em>//将发送人id参数传给服务端</em></span>
	<span style="color:#4078f2 !important">var</span> wsUrl=<span style="color:#c18401 !important">"ws://127.0.0.1:8082/charRoomServer"</span>;
	<span style="color:#4078f2 !important">var</span> allUrl=wsUrl+<span style="color:#c18401 !important">"/"</span>+sb;
	<span style="color:#999999 !important"><em>//接收人id</em></span>
	<span style="color:#4078f2 !important">var</span> jsrid=<span style="color:#c18401 !important">'超级管理员'</span>;
	<span style="color:#999999 !important"><em>//客户端与服务端建立连接,建立连接以后,它会发出一个ws.open事件</em></span>
	<span style="color:#4078f2 !important">var</span> ws=<span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">WebSocket</span>(allUrl);
	<span style="color:#999999 !important"><em>//连接成功后就将接收人id发送给服务端</em></span>
	ws.onopen=<span style="color:#4078f2 !important">function</span>(){
		ws.<span style="color:#50a14f !important">send</span>(jsrid);
	}
	<span style="color:#999999 !important"><em>//客户端收到服务端发送的消息</em></span>
	ws.onmessage=<span style="color:#4078f2 !important">function</span>(message){
	    <span style="color:#4078f2 !important">var</span> shenfen=<span style="color:#50a14f !important">JSON</span>.<span style="color:#50a14f !important">stringify</span>(message.data).<span style="color:#50a14f !important">toString</span>();
	    <span style="color:#4078f2 !important">var</span> shenfens=shenfen.<span style="color:#50a14f !important">slice</span>(shenfen.<span style="color:#50a14f !important">lastIndexOf</span>(<span style="color:#c18401 !important">"|"</span>)+<span style="color:#ae81ff !important">1</span>,-<span style="color:#ae81ff !important">1</span>);
     <span style="color:#999999 !important"><em>//这里通过很low的方式拿到了发送人id  [shenfens](后台拼接字符串并前台截取得到)   </em></span>
     <span style="color:#999999 !important"><em>//判断发送人id是否和页面初始化时的接收人id(jsrid)是否一致,相同则说明是对方回复的消息,并将该消息添加到自己的聊天框;如果收到的发送人id与“大白机器人”或者是和自己的id一致,也要将消息添加到自己的聊天框(自己发的消息嘛)</em></span>
        <span style="color:#4078f2 !important">if</span>(jsrid==shenfens||shenfens==<span style="color:#c18401 !important">"大白机器人:"</span>||shenfens==sb){
            <span style="color:#999999 !important"><em>//获取以后,在客户端显示</em></span>
            messages.innerHTML+=message.data;
        }<span style="color:#4078f2 !important">else</span>{
            <span style="color:#999999 !important"><em>//不做任何操作</em></span>
        }
	}
	<span style="color:#999999 !important"><em>//获取某个用户输入的聊天内容,并发送到服务端</em></span>
	 <span style="color:#4078f2 !important">function</span> <span style="color:#50a14f !important">getMessage</span>(){
		<span style="color:#4078f2 !important">var</span> inputMessage=<span style="color:#c18401 !important">document</span>.<span style="color:#50a14f !important">getElementById</span>(<span style="color:#c18401 !important">"inputMessage"</span>).value;
		<span style="color:#999999 !important"><em>//alert(inputMessage);</em></span>
		<span style="color:#999999 !important"><em>//获取消息以后,要发送给服务端,然后广播给所有用户</em></span>
		<span style="color:#4078f2 !important">if</span>(<span style="color:#50a14f !important">typeof</span>(inputMessage)==<span style="color:#c18401 !important">'undefined'</span>){
			<span style="color:#50a14f !important">alert</span>(<span style="color:#c18401 !important">"请输入您要发送的消息!"</span>);
		}<span style="color:#4078f2 !important">else</span>{
			ws.<span style="color:#50a14f !important">send</span>(inputMessage);
			<span style="color:#999999 !important"><em>//输入框消息清空</em></span>
			inputMessage.value=<span style="color:#c18401 !important">""</span>;
		}	
	} 
	<span style="color:#999999 !important"><em>//当关闭页面或者用户退出时,会执行一个ws.close()方法</em></span>
	<span style="color:#c18401 !important">window</span>.onbeforeunload=<span style="color:#4078f2 !important">function</span>(){
		ws.<span style="color:#50a14f !important">close</span>();
	}
	<span style="color:#999999 !important"><em>//按回车发送信息</em></span>
	<span style="color:#c18401 !important">document</span>.onkeyup=<span style="color:#4078f2 !important">function</span>(e){
		<span style="color:#4078f2 !important">if</span>(e.keyCode==<span style="color:#ae81ff !important">13</span>){
			<span style="color:#50a14f !important">getMessage</span>();
			inputMessage.value=<span style="color:#c18401 !important">""</span>;
		}
	}
</script>
</code></span></span>

Jetbrains全家桶1年46,售后保障稳定

2,其次为后台系统客服聊天框的js代码

<span style="color:#314659"><span style="background-color:#ffffff"><code class="language-javascript language-js"><script>
        <span style="color:#4078f2 !important">var</span> name=<span style="color:#c18401 !important">""</span>;
        $(<span style="color:#4078f2 !important">function</span> () {
            $.<span style="color:#50a14f !important">post</span>(
                <span style="color:#c18401 !important">"/demo/getName"</span>,
                <span style="color:#4078f2 !important">function</span> (data) {
                    name=data.name;
                },
                <span style="color:#c18401 !important">"json"</span>
            );
 <span style="color:#999999 !important"><em>//得到未读消息并展示到列表中(当点击其中一个未读消息时会得到该消息的账号(前台用户的userId唯一标识))</em></span>
            $.<span style="color:#50a14f !important">post</span>(
                <span style="color:#c18401 !important">"/demo/getweidu"</span>,
                <span style="color:#4078f2 !important">function</span> (data) {
                    <span style="color:#4078f2 !important">var</span> temp=<span style="color:#c18401 !important">''</span>;
                    <span style="color:#4078f2 !important">var</span> count=<span style="color:#ae81ff !important">0</span>;
                    <span style="color:#4078f2 !important">var</span> account=<span style="color:#c18401 !important">''</span>;
                    <span style="color:#4078f2 !important">for</span>(<span style="color:#4078f2 !important">var</span> i=<span style="color:#ae81ff !important">0</span>;i<data.length;i++){
                        temp+=<span style="color:#c18401 !important">'<div class="yonghu" onclick="liaotian('</span>+data[i].account+<span style="color:#c18401 !important">')">\n'</span> +
                            <span style="color:#c18401 !important">'            <span id="lalala">'</span>+data[i].account+<span style="color:#c18401 !important">'</span>\n'</span> +
                            <span style="color:#c18401 !important">'            <div id="'</span>+data[i].account+<span style="color:#c18401 !important">'" class="weidu">'</span>+data[i].count+<span style="color:#c18401 !important">'</div>\n'</span> +
                            <span style="color:#c18401 !important">'        </div>'</span>;
                    }
                    $(<span style="color:#c18401 !important">"#nav"</span>).<span style="color:#50a14f !important">append</span>(temp);
                }
            );
        });

<span style="color:#999999 !important"><em>//这是点击对应的未读消息之后将未读消息展示到客服的聊天框</em></span>
        <span style="color:#4078f2 !important">var</span> sb=<span style="color:#ae81ff !important">null</span>;
        <span style="color:#4078f2 !important">function</span> <span style="color:#50a14f !important">liaotian</span>(v) {  <span style="color:#999999 !important"><em>//下面的websocket内容在这个方法里面,执行这个点击事件之后触发websocket连接</em></span>
            sb=v;
            $(<span style="color:#c18401 !important">"#content"</span>).<span style="color:#50a14f !important">empty</span>();
            $(<span style="color:#c18401 !important">"#"</span>+v).<span style="color:#50a14f !important">hide</span>();
            $.<span style="color:#50a14f !important">post</span>(
                <span style="color:#c18401 !important">"/demo/getxiaoxi"</span>,
                {<span style="color:#f74449 !important">account</span>:v},
                <span style="color:#4078f2 !important">function</span> (data) {
                    <span style="color:#999999 !important"><em>// alert(JSON.stringify(data));</em></span>
                    <span style="color:#4078f2 !important">var</span> str=<span style="color:#c18401 !important">''</span>;
                    <span style="color:#4078f2 !important">for</span>(<span style="color:#4078f2 !important">var</span> i=<span style="color:#ae81ff !important">0</span>;i<data.length;i++){
                        <span style="color:#4078f2 !important">var</span> time=data[i].addtime.<span style="color:#50a14f !important">slice</span>(<span style="color:#ae81ff !important">0</span>,<span style="color:#ae81ff !important">10</span>);
                        str+=<span style="color:#c18401 !important">' <div class="message"><span>'</span>+data[i].account+<span style="color:#c18401 !important">'</span>用户:<span>'</span>+time+<span style="color:#c18401 !important">'</span>'</span>+data[i].message+<span style="color:#c18401 !important">'</div>'</span>;
                    }
                    $(<span style="color:#c18401 !important">"#content"</span>).<span style="color:#50a14f !important">append</span>(str);
                },
                <span style="color:#c18401 !important">"json"</span>
            );
            <span style="color:#999999 !important"><em>//将点击后的未读消息改为已读消息</em></span>
            $.<span style="color:#50a14f !important">post</span>(
                <span style="color:#c18401 !important">"/demo/changeYD"</span>,
                {<span style="color:#f74449 !important">account</span>:v},
                <span style="color:#4078f2 !important">function</span> (data) {
                    <span style="color:#c18401 !important">console</span>.<span style="color:#50a14f !important">log</span>(data);
                }
            );
            <span style="color:#999999 !important"><em>//发送人id(超级管理员写死的,上面说过了)</em></span>
            <span style="color:#4078f2 !important">var</span> wsUrl=<span style="color:#c18401 !important">"ws://127.0.0.1:8082/charRoomServer"</span>;
            <span style="color:#4078f2 !important">var</span> allUrl=wsUrl+<span style="color:#c18401 !important">"/"</span>+<span style="color:#c18401 !important">"超级管理员"</span>;
            <span style="color:#999999 !important"><em>//客户端与服务端建立连接,建立连接以后,它会发出一个ws.open事件</em></span>
            <span style="color:#4078f2 !important">var</span> ws=<span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">WebSocket</span>(allUrl);
            <span style="color:#999999 !important"><em>//接收人userId</em></span>
            <span style="color:#4078f2 !important">var</span> jsrid=sb;
            <span style="color:#999999 !important"><em>//连接成功后,提示浏览器客户端输入名称</em></span>
            ws.onopen=<span style="color:#4078f2 !important">function</span>(){
                ws.<span style="color:#50a14f !important">send</span>(jsrid);
            }
            <span style="color:#999999 !important"><em>//客户端收到服务端发送的消息</em></span>
            ws.onmessage=<span style="color:#4078f2 !important">function</span>(message){
                <span style="color:#999999 !important"><em>//截取到普通用户的手机号</em></span>
                <span style="color:#4078f2 !important">var</span> shenfen=<span style="color:#50a14f !important">JSON</span>.<span style="color:#50a14f !important">stringify</span>(message.data).<span style="color:#50a14f !important">toString</span>();
                <span style="color:#4078f2 !important">var</span> shenfens=shenfen.<span style="color:#50a14f !important">slice</span>(shenfen.<span style="color:#50a14f !important">lastIndexOf</span>(<span style="color:#c18401 !important">"|"</span>)+<span style="color:#ae81ff !important">1</span>,-<span style="color:#ae81ff !important">1</span>);
                 <span style="color:#c18401 !important">console</span>.<span style="color:#50a14f !important">log</span>(<span style="color:#c18401 !important">"身份:"</span>+shenfens);
                <span style="color:#999999 !important"><em>// //判断发送人id是否和页面初始化时的接收人id(jsrid)是否一致,相同则说明是对方回复的消息</em></span>
                <span style="color:#4078f2 !important">if</span>(jsrid==shenfens||<span style="color:#c18401 !important">"超级管理员"</span>==shenfens){
                    <span style="color:#999999 !important"><em>//获取以后,在客户端显示</em></span>
                    content.innerHTML+=message.data;
                }<span style="color:#4078f2 !important">else</span>{
                    <span style="color:#999999 !important"><em>//不做任何操作</em></span>
                }
            }
            <span style="color:#999999 !important"><em>//获取某个用户输入的聊天内容,并发送到服务端</em></span>
            <span style="color:#4078f2 !important">function</span> <span style="color:#50a14f !important">getMessage</span>(){
                <span style="color:#4078f2 !important">var</span> inputMessage=<span style="color:#c18401 !important">document</span>.<span style="color:#50a14f !important">getElementById</span>(<span style="color:#c18401 !important">"inputMessage"</span>).value;
                <span style="color:#999999 !important"><em>//alert(inputMessage);</em></span>
                <span style="color:#999999 !important"><em>//获取消息以后,要发送给服务端,然后广播给所有用户</em></span>
                <span style="color:#4078f2 !important">if</span>(<span style="color:#50a14f !important">typeof</span>(inputMessage)==<span style="color:#c18401 !important">'undefined'</span>){
                    <span style="color:#50a14f !important">alert</span>(<span style="color:#c18401 !important">"请输入您要发送的消息!"</span>);
                }<span style="color:#4078f2 !important">else</span>{
                    ws.<span style="color:#50a14f !important">send</span>(inputMessage);
                    <span style="color:#999999 !important"><em>//输入框消息清空</em></span>
                    inputMessage.value=<span style="color:#c18401 !important">""</span>;
                }
            }
            <span style="color:#999999 !important"><em>//当关闭页面或者用户退出时,会执行一个ws.close()方法</em></span>
             <span style="color:#c18401 !important">window</span>.onbeforeunload=<span style="color:#4078f2 !important">function</span>(){
                 ws.<span style="color:#50a14f !important">close</span>();
             }
            <span style="color:#999999 !important"><em>//按回车发送信息</em></span>
            <span style="color:#c18401 !important">document</span>.onkeyup=<span style="color:#4078f2 !important">function</span>(e){
                <span style="color:#4078f2 !important">if</span>(e.keyCode==<span style="color:#ae81ff !important">13</span>){
                    <span style="color:#50a14f !important">getMessage</span>();
                    inputMessage.value=<span style="color:#c18401 !important">""</span>;
                }
            }
        }
    </script>
</code></span></span>

3,最后为服务端的websocket实例

<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>

三,依赖注入

在websocket中进行依赖注入service并调用service方法进行数据库存储,如果按常规的方式是走不通的。

解决方式:

在该springboot项目中添加一个WebsocketConfig配置类,对service进行配置。

<span style="color:#314659"><span style="background-color:#ffffff"><code class="language-java"><span style="color:#fd971f !important">@Configuration</span>
<span style="color:#4078f2 !important">public</span> <span style="color:#4078f2 !important">class</span> <span style="color:#50a14f !important">WebSocketConfig</span> {
    <span style="color:#999999 !important"><em>/**
     * ServerEndpointExporter 用于扫描和注册所有携带 ServerEndPoint 注解的实例,
     * 若部署到外部容器 则无需提供此类。
     */</em></span>
    <span style="color:#fd971f !important">@Bean</span>
    <span style="color:#4078f2 !important">public</span> ServerEndpointExporter <span style="color:#50a14f !important">serverEndpointExporter</span>() {
        <span style="color:#4078f2 !important">return</span> <span style="color:#4078f2 !important">new</span> <span style="color:#50a14f !important">ServerEndpointExporter</span>();
    }

    <span style="color:#999999 !important"><em>

    
     * 因 SpringBoot WebSocket 对每个客户端连接都会创建一个 WebSocketServer(<span style="color:#c678dd">@ServerEndpoint</span> 注解对应的对象,Bean 注入操作会被直接略过,因而手动注入一个全局变量
     */</em></span>
    <span style="color:#fd971f !important">@Autowired</span>
    <span style="color:#4078f2 !important">public</span> <span style="color:#4078f2 !important">void</span> <span style="color:#50a14f !important">setSelectService</span>(SelectService selectService) {
        ChatRoomServer.selectService = selectService;
    }
}
</code></span></span>

然后在websocket中注入service。

四,效果展示

在这里插入图片描述
在这里插入图片描述

五,源码下载

php客服系统源码包下载地址:
https://yfi.lanzouj.com/iHN4A061ok5c

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/206572.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Linux查看端口占用情况的几种方式「建议收藏」

    Linux查看端口占用情况的几种方式「建议收藏」概述测试环境为Centos7.9,本文简单给出了几种检测端口的例子。一、查看本机端口占用1、netstat#安装netstatyum-yinstallnet-tools#检测端口占用netstat-npl|grep”端口”2、lsof#安装lsofyum-yinstalllsof#检测端口占用lsof-i:端口号二、查看对方端口是否开放1、nc(netcat)#安装ncyum-yinstallnc#查看对方端口是否开放nc-vz对

  • virsh命令行_怎么进入命令行窗口

    virsh命令行_怎么进入命令行窗口[@TOC]定义存储池与其目录创建已定义的存储池激活并自动启动已定义的存储池在存储池中创建虚拟机存储卷kvm存储池主要体现一种管理方式,可以通过挂载存储目录,lvm逻辑卷的方式创建存储池,虚拟机存储卷创建完成后,剩下的操作与无存储卷的方式无任何区别KVM存储池也要用于虚拟机迁移任务存储池相关管理命令二、生产环境存储池使用添加lvm和远程存储即可rawqcowqocw2qed什么是写时拷贝四、挂载磁盘使用libguestfsLinux工具可以在虚拟机无

  • 二叉树性质及习题

    二叉树性质及习题二叉树性质:1.在二叉树的第k层至多有2^(k-1)个结点。(k>=1)2.深度为k的二叉树至多有2^(k-1)个结点(k>=1)。3.对任何一棵二叉树T,如果其叶结点数为n0,度为2的结点数为n2,则n0=n2+1。证明:若度为1的结点有n1个,总结点个数为n,总边数为e,则根据二叉树的定义,n=n0+n1+n2…

  • 面试题的基本总结回顾(以以往面试过的问题做基本总结)[通俗易懂]

    Java基础问题整理:1.HashMap1.7与HashMap1.8的区别,从数据结构上、Hash值的计算上、链表数据的插入方法、内部Entry类的实现上分析?2.Hash1.7是基于数组和链表实现的,为什么不用双链表?HashMap1.8中引入红黑树的原因是?为什么要用红黑树而不是平衡二叉树?3.HashMap、HashTable、ConcurrentHashMap的原理与区别?…

  • eclipse导入maven工程

    eclipse导入maven工程eclipse导入maven工程

  • JavaScript高级程序设计(读书笔记)(七)[通俗易懂]

    JavaScript高级程序设计(读书笔记)(七)[通俗易懂]本笔记汇总了作者认为“JavaScript高级程序设计”这本书的前七章知识重点,仅供参考。第七章函数表达式小结:在JavaScript编程中,函数表达式是一种非常有用的技术。使用函数表达式可以无须对函数命名,从而实现动态编程。匿名函数,也称为拉姆达函数,是一种使用JavaScript函数的强大方式。以下总结了函数表达式的特点。函数表达式不同于函数声明。

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号