【问题标题】:Piping stdout to a browser将标准输出管道传输到浏览器
【发布时间】:2015-05-02 21:08:02
【问题描述】:

我有一个C++ 程序,它读取一个文件并一次打印一行(每秒一行)。

我需要在PHP 脚本中执行代码,并将输出通过管道传回浏览器。到目前为止,我已经尝试过execpassthru,但在这两种情况下,程序执行后整个输出都被“转储”在浏览器上。

如何让PHP 将输出流式传输回浏览器。

这是我目前编写的代码:
sender.php:发送请求执行。

<?php 

/*
 * Purpose of this program:
 * To display a stream of text from another C++-based program.
 * Steps:
 * 1. Button click starts execution of the C++ program.
 * 2. C++ program reads a file line-by-line and prints the output.
 */

?>

<html>

<head>
    <script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#startDisplaying").click(function() {
                console.log("Starting to display.");
                /*
                //initialize event source.
                if(!!window.EventSource) {
                    console.log("Event source is available.");
                    var evtSource = new EventSource("receiver.php");

                    evtSource.addEventListener('message', function(e) {
                        console.log(e.data);
                    }, false);

                    evtSource.addEventListener('open', function(e) {
                        console.log("Connection opened.");
                    }, false);

                    evtSource.addEventListener('error', function(e) {
                        console.log("Error seen.");
                        if(e.readyState == EventSource.CLOSED) {
                            console.log("Connection closed.");
                        }
                    }, false);
                } else {
                    console.error("Event source not available.");
                }
                */
                $.ajax({
                    type: 'POST',
                    url: 'receiver.php',
                    success: function(data) {
                        console.log("Data obtained on success: " + data);
                        $("#displayText").text(data);
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        console.error("Error seen: " + textStatus + " and error = " + errorThrown);
                    }
                });
            });
        });
    </script>
</head>

<body>
<textarea id="displayText"></textarea><br/>
<button id="startDisplaying">Start Displaying</button>
</body>

</html>  

执行程序的receiver.php:

<?php 

/* This file receives the request sent by sender.php and processes it.
 * Steps:
 * 1. Start executing the timedFileReader executable.
 * 2. Print the output to the textDisplay textarea.
 */

header('Content-Type: text/event-stream');
header('Cache-control: no-cache');

function sendMsg($id, $msg) {
    echo "id: $id".PHP_EOL;
    echo "data: $msg".PHP_EOL;
    echo PHP_EOL;
    ob_flush();
    flush();
}

$serverTime = time();

$cmd = "\"timedFileReader.exe\"";
//sendMsg($serverTime, 'server time: '.exec($cmd, time()));
passthru($cmd);

/*
$cmd = "\"timedFileReader.exe\"";
exec($cmd. " 2>&1 ", $output);
print_r($output);
*/
?>

C++ 程序:

#include<fstream>
#include<string>
#include<unistd.h>
#include<cstdlib>
#include<iostream>
using namespace std;

int main(int argc, char *argv[]) {

  ifstream ifile;
  string line;

  ifile.open("file.txt");
  if(!ifile) {
    cout << "Could not open file for reading." << endl;
    exit(0);
  }

  while(getline(ifile, line)) {
    cout << line << endl;
    //usleep(5000000);  //sleep for 1000 microsecs.
    sleep(1);
  }

  return 0;
}

这种执行模型甚至可以在 PHP 中实现吗?
欢迎任何帮助。

【问题讨论】:

  • 除非您打算动态编译程序然后运行它,否则语言并不重要——您只是在运行一个二进制文件。

标签: php c++


【解决方案1】:

请参阅http://php.net/manual/en/function.popen.php

它也可能依赖于 HTTP 服务器,因为它可以在您读取 C++ 应用程序输出之前关闭浏览器会话。

不确定您使用 PHP 到 C++ 桥的原因,但根据您的代码 - 您可以在 PHP 中完成全部工作。或者使用 C++ CGI 接口...

(类似问题:how get the output from process opend by popen in php?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多