php开发客服系统(持久连接+轮询+反向ajax)
歡迎在php嚴程序 - php教程學(xué)習(xí)AJAX教程, 本節(jié)課講解:php開發(fā)客服系統(tǒng)(持久連接+輪詢+反向ajax)
php開發(fā)客服系統(tǒng)(下載源碼)
用戶端(可直接給客戶發(fā)送消息)
客服端(點擊用戶名.即可給該用戶回復(fù)消息)
講兩種實現(xiàn)方式:
一:iframe + 服務(wù)器推技術(shù)comet(反向ajax,即服務(wù)器向瀏覽器推送數(shù)據(jù))
二:ajax持久連接 + 長輪詢
客服端采用第一種方式:iframe + 服務(wù)器推技術(shù)
思路:
1:新建comentbyiframe.php 該用文件使用while(true)一直連接到服務(wù)器不斷開.
如果在while的過程中查到了新數(shù)據(jù).使用ob_flush推給apache服務(wù)器.apache再用flush推給瀏覽器.
2:新建html頁面,插入一個iframe. 該iframe的src為comentbyiframe.php。
并隱藏iframe。comentbyiframe.php獲取的數(shù)據(jù)用js輸出到父窗口中的某個函數(shù).該函數(shù)把信息追加到指定的聊天窗口中
3:只要客戶端收到用戶發(fā)來的數(shù)據(jù). 就顯示為"xx對你說..". 客服端只要點擊用戶名。即可給該用戶發(fā)送數(shù)據(jù).
用戶端采用第二種方式:ajax持久連接 + 長輪詢
ajax持久連接:文檔加載完畢后(或其他時機),使用ajax請求一個php文件
被請求的php文件通過while(true)循環(huán).遲遲不給apache返回數(shù)據(jù)的目的.
輪詢指:請求服務(wù)器的時候.如果服務(wù)器沒有數(shù)據(jù).則一直等.當(dāng)服務(wù)器有數(shù)據(jù)后.就返回給客戶端.
這樣請求、響應(yīng)過后就完成了一次HTTP請求. 還沒完.客戶端收到數(shù)據(jù)后又到服務(wù)器要數(shù)據(jù).這就是輪詢
就好像一個乞丐一樣. 不給他錢,他就一直跟著你要. 你給他錢以后.他還不滿足,又跑來找你要.
實現(xiàn)思路:
進入用戶端后.如果沒有用戶名.使用setcookie設(shè)置一個用戶名.然后通過ajax持久連接. 不停向服務(wù)器索要數(shù)據(jù)(即客服發(fā)送給該用戶的記錄)
數(shù)據(jù)表設(shè)計:
create table liao(
id int auto_increment primary key,
rec varchar(10) not null default '' comment '接收者',
pos varchar(10) not null default '' comment '發(fā)送者',
content varchar(30) not null default '' comment '發(fā)送內(nèi)容',
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> ????// 將用戶發(fā)來的數(shù)據(jù)寫到聊天對話框中 ????function write_msg(msg){ ????????msg = eval('('+msg+')'); ????????$('<p><a?href="javascript:;"?onclick="setRec(\''+msg.pos+'\');">'+(msg.pos)+'</a>對你說:<br?/> '+(msg.content)+'</p>').appendTo($('#msgzone')); ????} ????// 添加回復(fù)人 ????function setRec(pos){ ????????$('#rec').val(pos); ????????$('#comment_btn').val('回復(fù)給:'+pos); ????} ????// 回復(fù) ????function comment(){ ????????var rec = $('#rec').val();????? // 回復(fù)給誰 ????????var cont = $('#cont').val();??? // 回復(fù)內(nèi)容 ????????if (rec ==''){ ????????????alert('請先選擇回復(fù)對象'); ????????????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>客服系統(tǒng) - 客服端</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="回復(fù)給:"?id="comment_btn"?onclick="comment();"?/> ??<input?type="hidden"?id="rec"?value=""?/> ?</body> </html> |
服務(wù)器不斷推送未讀記錄 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 + 服務(wù)器推技術(shù) comet、反向ajax */ require?'conn.php'; set_time_limit(0); ob_start(); echo?str_repeat(' ',4000); //ob_flush(); ob_end_flush(); flush(); while?(true){ ????// 查詢用戶給客服端發(fā)送的數(shù)據(jù) ????$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){ ????????// 將消息設(shè)置為已讀 ????????$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); } ?> |
客服給用戶回復(fù)消息comment.php
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php /* 給用戶回復(fù)消息 */ 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調(diào)用本文件不斷向服務(wù)器索要數(shù)據(jù).如果成功后.break退出循環(huán). 還沒完,服務(wù)器返回以后.ajax再次調(diào)用繼續(xù)索取 */ 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); } ?> |
不讓斷開,用戶端不斷調(diào)用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> ????? ????// 輪循索要數(shù)據(jù) ????$(function(){ ????????var setting = { ????????????url:'getuser.php', ????????????dataType:'json', ????????????success:function(msg){ ????????????????$('<p>客服對你說:<br?/> '+msg.content+'</p>').appendTo($('#msgzone')); ????????????????// 關(guān)鍵:服務(wù)器返回信息以后.再去服務(wù)器索要.即:輪詢 ????????????????$.ajax(setting); ????????????} ????????} ????????$.ajax(setting); ????}) ????// 給客服發(fā)送數(shù)據(jù) ????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>客服系統(tǒng) - 用戶端</h1> ??<div?id="msgzone"></div> ??<textarea?cols="50"?rows="6"?id="cont"></textarea> ??<input?type="button"?value="發(fā)送"?onclick="say();"?/> ?</body> </html> |
用戶向客服發(fā)送消息 toadmin.php
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php /* 向客服發(fā)送消息 */ require?'conn.php'; if?(!isset($_COOKIE['pos'])){ ????exit(); } $pos?= htmlspecialchars($_COOKIE['pos']);???// 發(fā)送者 $rec?=?'admin';?????????// 接收者 $cont?= htmlspecialchars($_POST['content']);?// 發(fā)送內(nèi)容 $sql?=?"insert into liao (pos,rec,content) value ('$pos','$rec','$cont')"; mysql_query($sql,$conn); echo?$cont; ?> |
轉(zhuǎn)載于:https://www.cnblogs.com/killers888/p/5045955.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的php开发客服系统(持久连接+轮询+反向ajax)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Firebug Tutorial (Se
- 下一篇: php写的squid验证辅助器