欢迎在php严程序 - php教程学习AJAX教程, 本节课讲解:php开发客服系统(持久连接+轮询+反向ajax)

php开发客服系统(下载源码)

用户端(可直接给客户发送消息)
客服端(点击用户名.即可给该用户回复消息)

讲两种实现方式:
一:iframe + 服务器推技术comet(反向ajax,即服务器向浏览器推送数据)
二:ajax持久连接 + 长轮询

客服端采用第一种方式:iframe + 服务器推技术
思路:
1:新建comentbyiframe.php 该用文件使用while(true)一直连接到服务器不断开.
如果在while的过程中查到了新数据.使用ob_flush推给apache服务器.apache再用flush推给浏览器.
2:新建html页面,插入一个iframe. 该iframe的src为comentbyiframe.php。
并隐藏iframe。comentbyiframe.php获取的数据用js输出到父窗口中的某个函数.该函数把信息追加到指定的聊天窗口中
3:只要客户端收到用户发来的数据. 就显示为"xx对你说..". 客服端只要点击用户名。即可给该用户发送数据.

用户端采用第二种方式:ajax持久连接 + 长轮询
ajax持久连接:文档加载完毕后(或其他时机),使用ajax请求一个php文件
被请求的php文件通过while(true)循环.迟迟不给apache返回数据的目的.
轮询指:请求服务器的时候.如果服务器没有数据.则一直等.当服务器有数据后.就返回给客户端.
这样请求、响应过后就完成了一次HTTP请求. 还没完.客户端收到数据后又到服务器要数据.这就是轮询
就好像一个乞丐一样. 不给他钱,他就一直跟着你要. 你给他钱以后.他还不满足,又跑来找你要.
实现思路:
进入用户端后.如果没有用户名.使用setcookie设置一个用户名.然后通过ajax持久连接. 不停向服务器索要数据(即客服发送给该用户的记录)

数据表设计
create table liao(
id int auto_increment primary key,
rec varchar(10) not null default '' comment '接收者',
pos varchar(10) not null default '' comment '发送者',
content varchar(30) not null default '' comment '发送内容',
isread tinyint not null default 1 comment '0已读1未读'
)engine myisam charset utf8;

客服端首页:index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.2/jquery.min.js"></script>
  <script>
    // 将用户发来的数据写到聊天对话框中
    function write_msg(msg){
        msg = eval('('+msg+')');
        $('<p><a href="javascript:;" onclick="setRec(\''+msg.pos+'\');">'+(msg.pos)+'</a>对你说:<br />&nbsp;&nbsp;'+(msg.content)+'</p>').appendTo($('#msgzone'));
    }
    // 添加回复人
    function setRec(pos){
        $('#rec').val(pos);
        $('#comment_btn').val('回复给:'+pos);
    }
    // 回复
    function comment(){
        var rec = $('#rec').val();      // 回复给谁
        var cont = $('#cont').val();    // 回复内容
        if (rec ==''){
            alert('请先选择回复对象');
            return;
        }
        if (cont==''){
            alert('说点什么吧');
            return;        
        }
         
        $.post('comment.php',{rec:rec,cont:cont},function(msg){
            if (msg == 'ok'){
                $('<p>你对'+rec+'说:<br />'+(cont)+'</p>').appendTo($('#msgzone'));
                $('#cont').val('');
            }
        });
    }
 
  </script>
  <style type="text/css">
      #msgzone {width:500px; height:300px; border:1px solid #ccc; padding:10px; overflow:scroll;}
  </style>
 </head>
 
 <body>
  <h1>客服系统 - 客服端</h1>
  <div id="msgzone"></div>
  <iframe src="byiframe.php" width="0" height="0" frameborder="0"></iframe>
  <textarea cols="50" rows="6" id="cont"></textarea>
  <input type="button" value="回复给:" id="comment_btn" onclick="comment();" />
  <input type="hidden" id="rec" value="" />
 </body>
</html>

服务器不断推送未读记录 byiframe.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/*
客服端
iframe + 服务器推技术 comet、反向ajax
*/
require 'conn.php';
set_time_limit(0);
ob_start();
echo str_repeat(' ',4000);
//ob_flush();
ob_end_flush();
flush();
 
while (true){
    // 查询用户给客服端发送的数据
    $sql 'select * from liao where isread="1" and rec="admin" order by id desc limit 1';
    $rs = mysql_query($sql,$conn);
    $row = mysql_fetch_assoc($rs);
    if ($row){
        // 将消息设置为已读
        $setRead 'update liao set isread=0 where id='.$row['id'];
        mysql_query($setRead,$conn);
        $json = json_encode($row);
        echo '<script>parent.write_msg(\''.$json.'\');</script>';
        ob_flush();     // 推给apache
        flush();        // 推给浏览器
    }
 
    sleep(1);
}
?>

客服给用户回复消息comment.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
/*
给用户回复消息
*/
require 'conn.php';
 
$rec $_POST['rec'];
$pos 'admin';
$cont $_POST['cont'];
 
$sql "insert into liao (rec,pos,content) values ('$rec','$pos','$cont')";
$rs = mysql_query($sql,$conn);
if ($rs){
    echo 'ok';
}
?>

持久连接,如果有信息才把信息返回给客户端.之后连接断开 getuser.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/*
ajax调用本文件不断向服务器索要数据.如果成功后.break退出循环.
还没完,服务器返回以后.ajax再次调用继续索取
*/
 
if (!isset($_COOKIE['pos'])){
    exit();
}
 
set_time_limit(0);
 
require 'conn.php';
$pos = htmlspecialchars($_COOKIE['pos']);
 
while (true){
    $sql 'select id,content from liao where isread=1 and rec="'.$pos.'" order by id desc limit 1';
    $rs = mysql_query($sql,$conn);
    $find = mysql_fetch_assoc($rs);
    if ($find){
        $setRead 'update liao set isread=0 where id='.$find['id'];
        mysql_query($setRead,$conn);
        echo json_encode($find);
        break;
    }
     
    sleep(1);
}
 
?>

不让断开,用户端不断调用getuser.php索要记录 byajax.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
客户端
*/
// 给该用户随机分配一个用户名
if (!isset($_COOKIE['pos'])){
    setcookie('pos','user'.mt_rand(1,100));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.2/jquery.min.js"></script>
  <script>
     
    // 轮循索要数据
    $(function(){
        var setting = {
            url:'getuser.php',
            dataType:'json',
            success:function(msg){
                $('<p>客服对你说:<br />&nbsp;&nbsp;'+msg.content+'</p>').appendTo($('#msgzone'));
                // 关键:服务器返回信息以后.再去服务器索要.即:轮询
                $.ajax(setting);
            }
        }
        $.ajax(setting);
    })
 
    // 给客服发送数据
    function say(){
        var cont = $('#cont');
        if (cont.val() == ''){
            alert('说点什么吧');
            return;
        }
         
        $.post('toadmin.php',{content:cont.val()},function(msg){
            if (msg!=''){
                $('<p>你对客服说:<br />'+msg+'</p>').appendTo($('#msgzone'));
                cont.val('');
            }
        });
 
    }
  </script>
  <style type="text/css">
      #msgzone {width:500px; height:300px; border:1px solid #ccc; padding:10px; overflow:scroll;}
  </style>
 </head>
 
 <body>
  <h1>客服系统 - 用户端</h1>
  <div id="msgzone"></div>
  <textarea cols="50" rows="6" id="cont"></textarea>
  <input type="button" value="发送" onclick="say();" />
 </body>
</html>

用户向客服发送消息 toadmin.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/*
向客服发送消息
*/
 
require 'conn.php';
 
if (!isset($_COOKIE['pos'])){
    exit();
}
 
$pos = htmlspecialchars($_COOKIE['pos']);   // 发送者
$rec 'admin';         // 接收者
$cont = htmlspecialchars($_POST['content']); // 发送内容
 
$sql "insert into liao (pos,rec,content) value ('$pos','$rec','$cont')";
mysql_query($sql,$conn);
echo $cont;
?>

php开发客服系统(持久连接+轮询+反向ajax)的更多相关文章

  1. php开发客服系统(持久连接+轮询+反向ajax 转载 http://www.tuicool.com/articles/2mU7v2R)

    php开发客服系统( 下载源码 ) 用户端(可直接给客户发送消息) 客服端(点击用户名.即可给该用户回复消息) 讲两种实现方式: 一:iframe + 服务器推技术comet(反向ajax,即服务器向 ...

  2. 用SignalR 2.0开发客服系统[系列2:实现聊天室]

    前言 交流群:195866844 上周发表了 用SignalR 2.0开发客服系统[系列1:实现群发通讯] 这篇文章,得到了很多帮助和鼓励,小弟在此真心的感谢大家的支持.. 这周继续系列2,实现聊天室 ...

  3. 用SignalR 2.0开发客服系统[系列3:实现点对点通讯]

    前言 交流群:195866844 目录: 用SignalR 2.0开发客服系统[系列1:实现群发通讯] 用SignalR 2.0开发客服系统[系列2:实现聊天室] 真的很感谢大家的支持,今天发表系列3 ...

  4. 用SignalR 2.0开发客服系统[系列5:使用SignalR的中文简体语言包和其他技术点]

    前言 交流群:195866844 目录: 用SignalR 2.0开发客服系统[系列1:实现群发通讯] 用SignalR 2.0开发客服系统[系列2:实现聊天室] 用SignalR 2.0开发客服系统 ...

  5. 用SignalR 2.0开发客服系统[系列4:负载均衡的情况下使用SignalR]

    前言 交流群:195866844 目录: 用SignalR 2.0开发客服系统[系列1:实现群发通讯] 用SignalR 2.0开发客服系统[系列2:实现聊天室] 用SignalR 2.0开发客服系统 ...

  6. 用SignalR 2.0开发客服系统[系列1:实现群发通讯]

    前言 交流群:195866844 先说一下我为什么会写这个博客吧,(首先说一下,我是一个小菜鸟,讲的不好请指导 - -,)  前段时间公司的项目涉及到在B/S上使用即时通讯,(其实就是做一个B/S的客 ...

  7. 使用 WPF+ ASP.NET MVC 开发 在线客服系统 (一)

    近段时间利用业余时间开发了一套在线客服系统,期间遇到过大大小小不少问题,好在都一一解决,最终效果也还可以,打算写一个系列的文章把开发过程详细的记录下来. 希望能够和更多的开发人员互相交流学习,也希望有 ...

  8. Linux + .net core 开发升讯威在线客服系统:同时支持 SQL Server 和 MySQL 的实现方法

    前段时间我发表了一系列文章,开始介绍基于 .net core 的在线客服系统开发过程. 有很多朋友一直提出希望能够支持 MySQL 数据库,考虑到已经有朋友在用 SQL Server,我在升级的过程中 ...

  9. .net core 和 WPF 开发升讯威在线客服系统【私有化部署免费版】发布

    希望 .net 和 WPF 技术时至今日,还能有一些存在感. 这个项目源于2015年前后,当时开发的初版,我使用了 ASP.NET MVC 做为后端,数据库使用原生 ADO.NET 进行操作.WPF ...

随机推荐

  1. Jasper_filter data_pass field data from main to sub to filter some data

    main report: 1 add variable <variable name="Variable_rule" class="java.lang.String ...

  2. 用QtWebKit开发简单的浏览器

    用QtWebKit开发简单的浏览器 1.代码实现 工程目录结构如下: AddressBar类包含了地址栏和按钮两个控件,将地址栏回车和按钮点击信号与goToSite()槽连接. 当回车和点击事件发生时 ...

  3. head命令

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  4. HDU 2187 A sequence of numbers

    题目连接  http://acm.hdu.edu.cn/showproblem.php?pid=2817 题意: 给定三个数,判断是等差数列还是等比数列,然后输出第k项. 做法:直接判断即可 #inc ...

  5. 基于网络的服装定制MTM系统研究 - 硕士论文 - 道客巴巴

    国内的mtm系统_百度搜索 基于网络的服装定制MTM系统研究 - 硕士论文 - 道客巴巴 PDF文档(共76页) - 下载需1800积分 天津工业大学 硕士学位论文基于网络的服装定制MTM系统研究 姓 ...

  6. 【KMP】Period

    KMP算法 Next[]函数深入理解,Next[]当前字符前匹配字符数,串长n-Next[i]=串内循环子串的长度p. 本题求子循环串内循环节数. Problem Description For ea ...

  7. C#调用PowerShell脚本

    今天通过一个小例子,学习了C#如何调用PowerShell脚本文件的Function以及传参. private bool CallPowershell(string outputFile) { str ...

  8. 企业版IDP的申请及“In House”发布

    企业版IDP,即iOS Development Enterprise Program.注意是$299/Year那种,并不是$99/Year的那种. 这种方式的IDP其最大的好处在于:可以发布“In H ...

  9. nexus5 root教程

    转载自: http://www.inexus.co/article-1280-1.html http://www.pc6.com/edu/71016.html https://download.cha ...

  10. [RxJS] Observables can throw errors

    Whenever we are writing code, we need to remember that things may go wrong. If an error happens in a ...