現在チャットを作成中なのですが、cometを使い
1.ajaxでphpにリクエスト
2.phpでデータベースをチェック、変化があるまでループで待機
変化があれば値を返す
といった感じで書き込みを表示させているのですが
2.でのwhileでループ中に 504 Gateway Timeout とでてしまいます。
どうすればいいでしょうか
php
1if (isset($_GET["state"])&&$_GET["state"]==="monitoring") { 2 $sql=sprintf('SELECT p.* FROM chat_posts p 3 ORDER BY p.res_created DESC LIMIT 1 '); 4 $posts=mysqli_query($db,$sql) or die(mysqli_error($db)); 5 $post=mysqli_fetch_assoc($posts); 6 $lastres=$post["res_created"]; 7 $newres=$lastres; 8 while ($newres===$lastres) {//書き込みがあるまでループ 9 $sql=sprintf('SELECT p.* FROM chat_posts p 10 ORDER BY p.res_created DESC LIMIT 1 '); 11 $posts=mysqli_query($db,$sql) or die(mysqli_error($db)); 12 $post=mysqli_fetch_assoc($posts); 13 $newres=$post["res_created"]; 14 session_write_close(); 15 sleep(1); 16 if($newres!==$lastres){//書き込みがあった場合全書き込みを渡す 17 $sql=sprintf('SELECT p.* FROM chat_posts p 18 ORDER BY p.res_created DESC '); 19 $posts=mysqli_query($db,$sql) or die(mysqli_error($db)); 20 while ($post=mysqli_fetch_assoc($posts)) { 21 $res[]=array( 22 'postnum'=>3, 23 'num'=>$post['res_id'], 24 'name'=>$post['res_name'], 25 'res'=>$post['res'], 26 'time'=>$post['res_created'] 27 ); 28 } 29 header(' Content-type:application/json; charset=utf-8 ' ); 30 header(' X-Content-Type-Option:nosniff ' ); 31 echo json_encode( 32 $res,JSON_HEX_TAG | JSON_UNESCAPED_UNICODE | JSON_HEX_APOS | 33 JSON_HEX_QUOT | JSON_HEX_AMP 34 );# code...# code...# code... 35 break; 36 } 37 } 38 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/03/06 04:41