界面上有一个按钮,点击向后台发送Ajax请求,请求的文件内容:
redis.php:
<code class="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; }</code>
然后在服务器模拟向频道XGD发布消息,发布操作代码:
Publish.php:
<code class="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.'<br>'; } }else{ echo '本地Redis无法连接!'; exit; }</code>
服务器使用Centos6.5, 在服务端用php命令运行publish.php,
同时进入本地redis-cli命令行,执行命令: subscribe XGD,
界面就循环出现订阅频道发布的内容,重新开启一个命令终端,用php命令执行redis.php,在命令行也出现了订阅频道发布的信息,问题是:
在Web前端的Ajax请求总是运行超时,没有返回结果。不知道为什么,还请各位大神赐教。
Ajax代码是:
<code class="javascript">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(); } }) }</code>
(项目使用了Thinkphp+DWZ)
================================================================
界面上有一个按钮,点击向后台发送Ajax请求,请求的文件内容:
redis.php:
<code class="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; }</code>
然后在服务器模拟向频道XGD发布消息,发布操作代码:
Publish.php:
<code class="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.'<br>'; } }else{ echo '本地Redis无法连接!'; exit; }</code>
服务器使用Centos6.5, 在服务端用php命令运行publish.php,
同时进入本地redis-cli命令行,执行命令: subscribe XGD,
界面就循环出现订阅频道发布的内容,重新开启一个命令终端,用php命令执行redis.php,在命令行也出现了订阅频道发布的信息,问题是:
在Web前端的Ajax请求总是运行超时,没有返回结果。不知道为什么,还请各位大神赐教。
Ajax代码是:
<code class="javascript">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(); } }) }</code>
(项目使用了Thinkphp+DWZ)
================================================================
阻塞模式,不会结束,所以就超时,你在callback里面exit看看?