php执行neverending binary / script print输出

[英]php execute neverending binary/script print output live


I'm running into a problem where the php script never terminates due to its work, so it never gets a chance to pass data back to ajax to display back to the user in html format.

我遇到了一个问题,PHP脚本由于其工作而永远不会终止,因此它永远不会有机会将数据传回ajax以html格式显示回用户。

To be more clear, what I'm trying to do is have a user click on a button on an html page, the button executes a javascript function which uses ajax to execute the php script. The php script executes a never ending command (ie: ping localhost) using popen and then I echo the output from popen. The problem is that because the php script never finishes, the echos are never reported back unless I kill the ping process, which causes the php script to end and the data echoed returned back to the javascript function to display in html.

更清楚的是,我要做的是让用户点击html页面上的按钮,该按钮执行一个javascript函数,该函数使用ajax来执行php脚本。 php脚本使用popen执行一个永不停止的命令(即:ping localhost),然后我回显popen的输出。问题是因为php脚本永远不会完成,所以回不会报告回声,除非我终止ping进程,这导致php脚本结束并且数据回显返回到javascript函数以在html中显示。

I want to be able to see the live output when the php script is running. If I click on that button in the html website, I want the button to call the javascript function, then execute the php script and I want to see immediately every echo from the php script as it is running live. Is that possible?

我希望能够在php脚本运行时看到实时输出。如果我点击html网站上的那个按钮,我希望按钮调用javascript函数,然后执行php脚本,我想立即看到php脚本中的所有回显,因为它正在运行。那可能吗?

Here is my code so far:

这是我到目前为止的代码:

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>

        function execfunc() {
            // Clear any previous output.
            clearOutput();

            $.ajax({
                type: "GET",
                url: "phpscript.php",
                data: "",
                success: function(msg){
                    $('#divOutput').html(msg);
                }
            }); // Ajax Call
        }

        function stopReadout() {
            // to do
        }

        function clearOutput() {
            $("#divOutput").html("");
        }
    </script>
</head>
<body>
    <h3>Executing Linux Command</h3>
    <p>Push the button to execute the command "ping localhost":</p>
    <button type="button" onclick="execfunc();">Execute</button>
    <button type="button" onclick="stopReadout();">Stop</button>
    <button type="button" onclick="clearOutput();">Clear</button>
    <br/>
    <p id="label">Terminal output:</p>
    <br/>
    <div id="divOutput" style="width:800px;height:512px;border:1px solid black;overflow:scroll"></div>
</body>

phpscript.php

<?php
    while (@ ob_end_flush()); // end all output buffers if any
    $cmd = 'ping localhost';
    $proc = popen($cmd, 'r');

    echo '<pre>';
    while (!feof($proc))
    {
        echo fread($proc, 4096);
        flush();
    }
    echo '</pre>';
?>

If you run the phpscript.php by itself, it works fine and echos back to the browser window. But if I execute the script from the index.html javascript using ajax, I don't get any live output back because the php script never finishes.

如果您自己运行phpscript.php,它可以正常工作并回显到浏览器窗口。但是如果我使用ajax从index.html javascript执行脚本,我不会得到任何实时输出,因为php脚本永远不会完成。

I even tried having phpscript.ph make a fifo pipe and dump the ping output into the fifo pipe, then I had another php script that echoed back 'cat fifofile', but the same behavior happens.

我甚至尝试让phpscript.ph制作一个fifo管道并将ping输出转储到fifo管道中,然后我有另一个PHP脚本回显'cat fifofile',但是同样的行为发生了。

Any suggestions or help would truly be appreciated it! D

任何建议或帮助将真正受到赞赏! d

1 个解决方案

#1


0  

You need to use web sockets for that.

您需要使用Web套接字。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/04/08/305add3039236ae540b6385337715913.html



 
  © 2014-2022 ITdaan.com 联系我们: