界面上有一个按钮,点击向后台发送ajax请求,请求的文件内容:
redis.php:
ini_set('default_socket_timeout',25);$redis = new redis();$conn = $redis->connect('127.0.0.1','6379');if($conn){ $redis->subscribe(array('xgd'),'ckr');}else{ echo '本地redis无法连接!'; exit;}function ckr($instance,$channel,$msg){ echo $msg;}
然后在服务器模拟向频道xgd发布消息,发布操作代码:
publish.php:
$redis = new redis();$conn = $redis->connect('127.0.0.1','6379');if($conn){ $i=0; while(true){ sleep(1); $i++; $respose['id'] = uniqid(); $respose['v_ltl_status'] = uniqid(); $respose['ltl_time'] = uniqid(); $respose['ltl_reckon'] = uniqid(); $respose['ltl_cardno'] = uniqid(); $respose['card_type'] = uniqid(); $respose['card_name'] = uniqid(); $respose['ltl_cardflag'] = uniqid(); $respose['rsd_bicc'] = uniqid(); $respose['v_ltl_type'] = uniqid(); $res_json = json_encode(array('result'=>$respose)); $redis->publish('xgd',$res_json); //echo '数据已经发布'.$i.'
'; }}else{ echo '本地redis无法连接!'; exit;}
服务器使用centos6.5, 在服务端用php命令运行publish.php,
同时进入本地redis-cli命令行,执行命令: subscribe xgd,
界面就循环出现订阅频道发布的内容,重新开启一个命令终端,用php命令执行redis.php,在命令行也出现了订阅频道发布的信息,问题是:
在web前端的ajax请求总是运行超时,没有返回结果。不知道为什么,还请各位大神赐教。
ajax代码是:
function loadresult(){ //$.pdialog.open(__url__/getredisinfo,'operate','操作结果',{'width':'700','height':'400','mask':true}); var dt = new date(); var ts = dt.gettime(); $.ajax({ cache : false, type : 'post', url : '/redis.php', global: false, datatype : 'json', async : true, data:{time:ts}, timeout:1000, success:function(data){ $(.redis).html($(.redis).html()+data.result); }, complete:function(){ loadresult(); } }) }
(项目使用了thinkphp+dwz)
================================================================
回复内容: 界面上有一个按钮,点击向后台发送ajax请求,请求的文件内容:
redis.php:
ini_set('default_socket_timeout',25);$redis = new redis();$conn = $redis->connect('127.0.0.1','6379');if($conn){ $redis->subscribe(array('xgd'),'ckr');}else{ echo '本地redis无法连接!'; exit;}function ckr($instance,$channel,$msg){ echo $msg;}
然后在服务器模拟向频道xgd发布消息,发布操作代码:
publish.php:
$redis = new redis();$conn = $redis->connect('127.0.0.1','6379');if($conn){ $i=0; while(true){ sleep(1); $i++; $respose['id'] = uniqid(); $respose['v_ltl_status'] = uniqid(); $respose['ltl_time'] = uniqid(); $respose['ltl_reckon'] = uniqid(); $respose['ltl_cardno'] = uniqid(); $respose['card_type'] = uniqid(); $respose['card_name'] = uniqid(); $respose['ltl_cardflag'] = uniqid(); $respose['rsd_bicc'] = uniqid(); $respose['v_ltl_type'] = uniqid(); $res_json = json_encode(array('result'=>$respose)); $redis->publish('xgd',$res_json); //echo '数据已经发布'.$i.'
'; }}else{ echo '本地redis无法连接!'; exit;}
服务器使用centos6.5, 在服务端用php命令运行publish.php,
同时进入本地redis-cli命令行,执行命令: subscribe xgd,
界面就循环出现订阅频道发布的内容,重新开启一个命令终端,用php命令执行redis.php,在命令行也出现了订阅频道发布的信息,问题是:
在web前端的ajax请求总是运行超时,没有返回结果。不知道为什么,还请各位大神赐教。
ajax代码是:
function loadresult(){ //$.pdialog.open(__url__/getredisinfo,'operate','操作结果',{'width':'700','height':'400','mask':true}); var dt = new date(); var ts = dt.gettime(); $.ajax({ cache : false, type : 'post', url : '/redis.php', global: false, datatype : 'json', async : true, data:{time:ts}, timeout:1000, success:function(data){ $(.redis).html($(.redis).html()+data.result); }, complete:function(){ loadresult(); } }) }
(项目使用了thinkphp+dwz)
================================================================
阻塞模式,不会结束,所以就超时,你在callback里面exit看看?