|
这个问题源于一个贴子<<使用反向Ajax技术做在线客服系统>>,地址如下
使用反向Ajax技术做在线客服系统
http://blog.csdn.net/baochao95/article/details/52822098
原PHP的方式已经实现,确实可用。之后就想着用CI改写一下。碰到如下问题。
控制器中kefu_iframe部分,无论如何调整都无法实现iframe长连接功能,只能加了exit('55555')后,会实现客服和系统对话功能,但只能执行一次。查了挺多资料,都没有办法实现来回对话的交互功能。
分析了几点:
①CI的VIEW缓存问题,感觉是控制器中的内容如果不执行完,VIEW部分是无法加载的。
②sleep在中间似乎不起作用。
控制器文件 Ajaxke.php
PHP复制代码
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Ajaxke extends Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function __construct (){
parent ::__construct ();
// $this->db->close();
$this->load->model('m_ajax');
}
public function index ()
{
$this->load->view('ajaxkefu_admin', TRUE);
}
public function kefu_iframe (){
//<meta http-equiv="Content-Type" content="text/html"; charset=utf-8">
//$this->output->set_content_type('text/plain', 'UTF-8');
// $this->output->set_content_type('text/html', 'UTF-8');
// echo $this->output->get_header('content-type');
echo '************';
// echo $this->output->get_header('content-type');
// exit();
header('Content-type:text/html; charset=utf-8');
set_time_limit(0);
// ob_start();
ob_end_clean();
echo str_pad(' ', 4096);
// ob_flush();
// flush();
while (true) {
$sql = "select * from msg where rec = 'admin' and isread = 0 limit 0,1";
$msg = $this->m_ajax->getOnemsg($sql)->row_array();
// echo $msg; echo '<br />';
if (!empty($msg)) {
ob_end_clean();
$sql = 'update msg set isread = 1 where mid = '.$msg['mid'];
// echo $sql; exit('333333333');
// parent.window.comet({"mid":"28","rec":"admin","pos":"asdf","isread":"0","content":"asdfasdf"})
$this->m_ajax->getOnemsg($sql);
$json = json_encode($msg);
// sleep(5);
echo "<script>parent.window.comet($json); </script>";
// sleep(5);
$this->output->_display ();
exit('55555555555555');
ob_flush();
flush();
sleep(1);
}
sleep(1);
}
}
public function kefu_ajax (){
set_time_limit(0);
$rec = $_COOKIE['username'];
$sql = "select * from msg where rec = '$rec' and isread =0 limit 0,1";
// exit($rec);
while (true) {
$msg = $this->m_ajax->getOnemsg($sql)->row_array();
if (!empty($msg)) {
$sql = "update msg set isread = 1 where mid = ".$msg['mid'];
$this->m_ajax->getOnemsg($sql);
echo json_encode($msg);
exit();
}
sleep(1);
}
}
public function kefu_sendmsg (){
header('Content-type:text/html;charset=utf-8');
// $db = $this->load->database('ajax',true);
$rec = $_POST['rec'];
$pos = $_COOKIE['username'];
$respContent = $_POST['content'];
// $rec = 'admin';
// $pos = 'still';
// $respContent = 'come on ';
$sql = "insert into msg (pos,rec,content) values ('$pos', '$rec', '$respContent')";
$res = $this->m_ajax->insertOnemsg($sql);
echo $res? 'ok':'fail';
}
public function keuser ()
{
setcookie('username', 'user'.rand(10000,99999));
$this->load->view('kefu_user');
}
}
复制代码
视图文件:ajaxkefu_admin.php
HTML复制代码
<?php
setcookie('username','admin')
?>
<html>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Closest — 100% Free Fully Responsive HTML5 Template by FREEHTML5.co </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Free HTML5 Template by FREEHTML5" />
<meta name="keywords" content="free html5, free template, free bootstrap, html5, css3, mobile first, responsive" />
<!--ajaxke.css -->
<link rel="stylesheet" href="<?php echo _CSSPATH_ ; ?>style.css">
<script src="<?php echo _JSPATH_ ; ?>kefuadmin.js"> </script>
<!-- Facebook and Twitter integration -->
<meta property="og:title" content=""/>
<meta property="og:image" content=""/>
<meta property="og:url" content=""/>
<meta property="og:site_name" content=""/>
<meta property="og:description" content=""/>
<meta name="twitter:title" content="" />
<meta name="twitter:image" content="" />
<meta name="twitter:url" content="" />
<meta name="twitter:card" content="" />
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<link rel="shortcut icon" href="favicon.ico">
<link rel='stylesheet' type='text/css'>
</head>
<body>
<h1>客服功能--客服人员端 </h1>
<h2>原理:iframe+长连接 </h2>
<div id="chatArea">
</div>
<hr>
<iframe name="frame" frameborder="0" src="<?php echo __ADMIN__DO_MAIN; ?>/ajaxke/kefu_iframe"> </iframe>
<p>咨询人: <span id="postman"></span></p>
<p><textarea id="respContent"></textarea></p>
<p><input type="button" value="回复" /></p>
</body>
</html>
复制代码
|
|