为什么每次都会立即回复,而不是等待?

时间:2015-04-07 16:44:36

标签: php mysql ajax

我有一个名为' stream.php'的php文件,基本上就是这样:

<?php

    $MyServer = "host";
    $MyUser   = "user";
    $MyPassword = "pass";
    $MyDatabase = "db";
    $Con  = mysqli_connect("$MyServer","$MyUser","$MyPassword",$MyDatabase);

    $time_wasted = 0;
    $lastid= $_POST['id'];

    $stmt   =   "SELECT * FROM mail WHERE ID > " . $lastid . " ORDER BY ID ";

    $newMail = 0;
    if ($result=mysqli_query($Con,$stmt))
    {
      // Return the number of rows in result set
      $rowcount=mysqli_num_rows($result);
      if($rowcount <= 0){
        while( $rowcount <= 0 ){
              if( $rowcount <= 0 ){
                 if( $time_wasted >= 20 ){
                    echo "timeout" . " " . $lastid;
                    die();
                 }

                 sleep( 1 );
                 //todo
                 $result=mysqli_query($Con,$stmt);
                 $rowcount=mysqli_num_rows($result);
                 //mysqli_free_result($result);
                 $time_wasted += 1;
              }
        }
      }
        $flag = 0;   
        $lastid = 0; 
        $new_messages = array();
        if( $rowcount >= 1):
            $newMail++;
            $row = mysqli_fetch_array($result);
            $timest = $row['time'];
            $oldtime = strtotime($timest);
            $lastid = $row['ID'];
            while ( $row = mysqli_fetch_array($result)):
                $flag = 1;
                $timest = $row['time'];
                $newtime = strtotime($timest);
                if(($newtime-$oldtime) > 5):
                    $newMail ++;
                    $oldtime = $newtime;
                endif;

              $new_messages[] = array( 'id' => $row['ID'], 'message' => $row['msg_txt'] );
            endwhile;
        endif;
        if($flag>0):
            $last_msg = end( $new_messages );
            $lastid = $last_msg['id'];
        endif;
      $row = mysqli_fetch_array($result);
      //echo $row['msg_txt'] . " " . $row['ID'];
      echo $newMail . " " . $lastid;
      // Free result set
      mysqli_free_result($result);
    }
?>

这就是负责将AJAX请求发送到上述文件的内容:

function shortPollMod(id) {
                // 1. Create XHR instance - Start
                if(id === 0){
                    //alert("beginning")
                    jQuery('.cont').prepend( '<div id="odd"><span></span><i class="flaticon-close33" onclick="closeNotif(this)"></i></div>' );
                    jQuery('.cont #odd span').text('No new notifications');
                    totalMail = 0;
                }
                var xhr;
                if (window.XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                }
                else if (window.ActiveXObject) {
                    xhr = new ActiveXObject("Msxml2.XMLHTTP");
                }
                else {
                    throw new Error("Ajax is not supported by this browser");
                }
                // 1. Create XHR instance - End

                // 2. Define what to do when XHR feed you the response from the server - Start
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        if (xhr.status == 200 && xhr.status < 300) {
                            var result = xhr.responseText;
                            var lastID = result.split(' ').pop();
                            var first = result.split(' ')[0];
                            alert(result);

                            if(id === lastID || parseInt(lastID) === 0){
                                alert("hurrah");
                                shortPollMod(lastID);   
                            }
                            else{
                                notif(first);
                                shortPollMod(lastID);
                            }
                        }
                    }
                }
                // 2. Define what to do when XHR feed you the response from the server - Start


                // 3. Specify your action, location and Send to the server - Start 
                xhr.open('POST', 'stream.php');
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send("id=" + id);
                // 3. Specify your action, location and Send to the server - End
            }

现在,我试着解释发生了什么:

  • MAIL数据库获取一些行更新(来自其他来源)
  • 客户端应向stream.php发送请求以询问&#39;新邮件
  • 虽然没有邮件,但服务器应该等待&#39;对于新行。

但是当我尝试上述长期调查时,请注意(首先);正在执行的次数与行数相同,并且不会发送新行。如果我的表最初有5行,然后客户端启动了请求,我基本上得到了添加的所有新行的总数(我想计算所有那些时间差为5秒的行)

0 个答案:

没有答案