如何在网页中动态显示 ping 输出?

2022-01-23 00:00:00 shell command dynamic ruby-on-rails ajax

作为诊断页面的一部分,我希望用户能够运行ping",即普通的 shell 命令将 ICMP_ECHO_REQUST 发送到某个 IP 并在浏览器的 div 中动态显示结果.

As part of a diagnostics page I would like to have the user be able to run a "ping", ie an ordinary shell command to send ICMP_ECHO_REQUSTs to a certain IP and display the resuls dynamically in a div in the browser.

后端是 Ruby/Rails.

The backend is Ruby/Rails.

我已经在服务器端运行命令并读取 ping 命令的输出.

I'm past running the command on the server side and reading the output from the ping command.

我一直在构建网页,这些网页会定期回调服务器以动态更新页面的某些部分.

And I've been building web pages that periodically calls back to the server to update som parts of the page dynamically.

但在这种情况下存在三个挑战:

But in this case there are three challenges:

  • 能够让 ajax 回调到 uri/url 以找到运行 ping 命令的进程
  • 最好能在 ping 命令返回新行数据时更新页面
  • 可以选择中断" ping.使用 ping,我当然可以将选项设置为仅发送 x ping,然后退出,从而无需停止进程.但我还有另一个工具,即日志查看器,该工具不会在一定数量的行之后自行停止,而是会在不中断的情况下永远持续下去,即使用 Control-C.

我是否设置了一个内存缓存来与运行 ping 的进程会合,还是有更简单的方法?

Do I set up a memcache to rendez-vous with the process running ping or is there a simpler way?

我搜索了很多,认为这应该是一个常见的问题,以至于有一个 rails 插件可以神奇地实现所需的东西,但我根本没有找到太多.

I searched a lot thinking this should be a problem common enough to have a rails plugin just magically implementing whats needed but I didn't find much at all.

有什么建议或指点吗?

推荐答案

如果我理解正确的话,你想要的通常是在客户端从 web 服务器接收一些信息,而客户端不知道这些信息是什么时候传入的,即推送信息从网络服务器到客户端.有几种方法可以做到这一点,它们都有一些缺点:

If I understand you correctly, what you want is generally receiving some information from web server in client while client doesn't know when exactly that information is incoming, i.e. pushing information from web server to client. There are a few ways to do that, all of them have some downsides:

  • HTTP 推送 - 需要保持活动连接,并且服务器以分块方式保持发送信息,每次都宣布下一个块传入,并且在准备好之前不发送.通常,这个chucked 流"要么在 XMLHttpRequest 对象中接收,要么在隐藏的 iframe 中接收,尽管如果需要(如您的情况),也可以按原样将其显示给用户.
  • 轮询 - 客户端只是询问服务器它是否定期传入.令人望而生畏,具有巨大的消息传递延迟和流量消耗,但几乎总是有效.
  • 长轮询 - 轮询和 HTTP 推送的组合 - 即轮询后的第一个回答被延迟,直到它可以回答.
  • 服务器发送事件 (SSE) - 一个几乎被接受的标准,在Opera 由来已久,现在许多浏览器都支持它,它的目标是成为 W3C 标准.
  • WebSockets 也是 Google 新提出的标准,允许更复杂的 TCP 连接以及来自 Javascript 的发送/接收功能.它也可以用于 HTTP 推送.
  • 使用非 HTML 方法(即 Flash、Java、Silverlight)获取传入内容.有一些库/现成的模块化 SWF/小程序(例如,BlazeDS) 可用于此目的,例如,它可以将来自给定连接的信息作为 JSON 在目标页面上执行.
  • HTTP push - requires a keep-alive connection and server keeping sending information in chunked manner, announcing next chunk incoming every time and not sending it until ready. Usually this "chucked stream" is either received in XMLHttpRequest object or a hidden iframe, although it's possible to just display it to user as is if it's desirable (as in your case).
  • Polling - client just asks server if it's something incoming regularly. Daunting, has huge messaging latencies and it's a traffic hog, but works almost always.
  • Long polling - a combination of polling and HTTP push - i.e. first answer after poll gets delayed till it will be something to answer.
  • Server-sent events (SSE) - a nearly-accepted standard, implemented in Opera for ages, now many browsers support it and it's aiming to become a W3C standard.
  • WebSockets is also a newly proposed standard by Google, allowing more complex TCP connections with send/receive functionality from Javascript. It also can be used for HTTP push.
  • Using non-HTML methods (i.e. Flash, Java, Silverlight) to get incoming content. There are a few libraries / readymade modular SWFs/applets (for example, BlazeDS) available for this purpose, which can tunnel information from a given connection to execute as JSON on target page, for example.

所有这些方法都包含在HTTP push"和comet"的概括性术语中.有大量的文档、教程和现有的解决方案随处可见.例如,对于 RoR,您可以尝试 Juggernaut 或 shooting_star,或者选择 简约解决方案.

All these methods are indeed covered by umbrella terms "HTTP push" and "comet". There's plenty of documentation, tutorials and existing solutions flying around. For example, for RoR, you can try Juggernaut or shooting_star, or just opt for minimalistic solutions.

最后,我想推荐一篇 Gregor Roth 在 SSE(第 1 部分) 和 WebSockets (part 2) 给出了详细的解释、示例和使用前景.

Finally, I'd like to recommend an excellent article by Gregor Roth on SSE (part 1) and WebSockets (part 2) that gives detailed explanations, examples and prospects for usage.

相关文章