介面上有一個按鈕,點擊向後台發送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看看?