简体   繁体   English

如何实现服务器发送事件和 bash 脚本的有效解决方案

[英]how to achive a working solution for Server Sent Event and bash script

I would like to implement a web page that show in real time the output of a bash script.我想实现一个实时显示 bash 脚本输出的网页。

I tried this simple example:我试过这个简单的例子:

html page: html页面:

<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
</head>
<body>
     <div id="result">put data out here</div>
     <script type="text/javascript">
        function eventsourcetest(){
           var source = new EventSource("http://localhost/prova_ping.php");
           source.addEventListener("message", function(e) {
           if (e.data !== "") {
              data=JSON.parse(e.data);                 
              document.getElementById("result").innerHTML +=  e.data+ "<br>";
           }
           }, false);
           source.addEventListener("error", function(e) {
              alert("errore!");
              source.close();
           }, false);
        }    
     </script>
<p><button type="button" onclick="eventsourcetest();">ping google.com</button>
</body>
</html>

prova_ping.php: prova_ping.php:

<?php

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

  $proc = popen("ping -c4 www.google.com", 'r');
  while (!feof($proc))
  {
     echo "risultato: " . fread($proc, 4096) . "\n\n";flush();ob_flush();
  }

?>

checking the php page with the command:使用以下命令检查 php 页面:

wget -O - -q "http://localhost/prova_ping.php"

I get the output in real time, but when I load the html page in firefox and push the button, nothing is happening and I don't get any error from browser on from apache looking into the error.log.我实时获得输出,但是当我在 firefox 中加载 html 页面并按下按钮时,什么也没有发生,我没有从浏览器中看到任何错误,从 apache 查看 error.log。

Googling I have found many similar example, usualy on internal php command as time and all working fine but nothing about bash script exept this about ping that for me is not working!谷歌搜索我发现了许多类似的例子,通常在内部 php 命令上随着时间的推移一切正常,但没有任何关于 bash 脚本的内容,关于 ping 对我来说不起作用!

If there are some one that can help me, any suggestions are appreciated.如果有人可以帮助我,任何建议将不胜感激。

Thank in advance,预先感谢,

Emilio埃米利奥

You can modify your HTML page like so:您可以像这样修改 HTML 页面:

<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
</head>
<body>
    <div id="result">put data out here</div>

    <button onclick="window.location.href='http://localhost/prova_ping.php';">Ping Google.com</button>
</body>
</html>

First change this line:首先改变这一行:

  echo "risultato: " . fread($proc, 4096) . "\n\n";flush();ob_flush();

into:进入:

  echo "data " . fread($proc, 4096) . "\n\n";@ob_flush();@flush();

The key thing is the message has to be 'data'.关键是消息必须是“数据”。 (The standard does allow other messages, but is a feature that is never of practical use.) (该标准确实允许其他消息,但该功能从来没有实际用途。)

I've put ob_flush() before flush() .我已经把ob_flush()之前flush() ( ob_flush is for the PHP buffers, so you need to flush them, before flush() which flushes the apache buffers.) The @ just suppresses error messages - if you had the PHP buffers switched off, it would fill your output with noise. ob_flush用于 PHP 缓冲区,因此您需要在flush() apache 缓冲区的flush()之前刷新它们。) @只是抑制错误消息 - 如果您关闭了 PHP 缓冲区,它会用噪音填充您的输出。

The other fix you need to do is that you are not sending JSON data, so JSON.parse() causes an error.您需要做的另一个修复是您没有发送 JSON 数据,因此JSON.parse()会导致错误。 I just changed data=JSON.parse(e.data);我刚刚更改了data=JSON.parse(e.data); to data=e.data; data=e.data;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM