2009-12-03 2 views
1

소켓 용으로이 코드를 사용하고 있습니다. 콘솔은 페이지가 매우 적은 시간 동안 처리된다고 말하지만 Chrome은 페이지가 ~ 1 초 동안로드 중임을 나타냅니다.PHP에서 소켓이 느린가요?

$this->serv_sock = socket_create(AF_INET, SOCK_STREAM, 0); 

    socket_bind($this->serv_sock, $this->serv, $this->port) or die("Could not bind to address\n"); 

    socket_listen($this->serv_sock); 

    while (1) { 
     echo "Waiting...\n";   
     $client = socket_accept($this->serv_sock); 

     $start_mtime = microtime(true); 

     echo "Accepted at ".$start_mtime.".\n"; 

     $input = ''; 

     $len = 0; 

     do { 
      //echo "Reading.\n"; 
      $inp = socket_read($client, 1024); 
      $input .= $inp; 

      if (strpos($input, "\n\n") === false && strpos($input, "\r\n\r\n") === false) 
       continue; 

      if (!$len) { 
       if (!preg_match("/Content-Length: (\d+)/", $input, $matches)) { 
        break; 
       } 
       $len = $matches[1]; 
       if (!$len) 
        break; 
       echo "We want $len bytes.\n"; 
      } 

      if (strpos($input, "\n\n") !== false) 
       list($headers, $content) = explode("\n\n", $input); 
      else 
       list($headers, $content) = explode("\r\n\r\n", $input); 

      if (strlen($content) >= $len) 
       break; 
     } while ($inp); 

     echo "Calling callback as ".microtime(true).".\n"; 

     if (strpos($input, "\n\n") !== false) 
      list($headers, $content) = explode("\n\n", $input); 
     else 
      list($headers, $content) = explode("\r\n\r\n", $input); 

     $output = $this->translate($callback, $headers, $content); // nothing slow here 

     $time_end = microtime(true); 
     echo "Sending output at ".$time_end." (total ".($time_end - $start_mtime).")\n\n"; 

     $output = "HTTP/1.0 Ok\n". 
     "Content-Type: text/html; charset=utf-8\n". 
     "Content-Length: ".strlen($output)."\n". 
     "Connection: close\n\n". 
     $output; 

     socket_write($client, $output); 

     socket_close($client); 
    } 

답변

0

나는 당신이 말하는 것을 이해합니다. 내가 아는 한 서버 처리 시간과 클라이언트 처리 시간에는 차이가 있습니다.

실제로 서버에서 정보를 처리하는 데 걸리는 시간은 항상 적습니다. 서버가 정보 처리를 끝내면 데이터는 여전히 브라우저로 보내 져야하며 렌더링되어야합니다. 데이터가 도착하고 브라우저가 렌더링되는 시간은 내가 크롬이 ~ 1 초가 걸리는 것을 말하는 이유가 무엇인지 의심 스럽습니다.