如何在网页中动态显示 ping 输出?
作为诊断页面的一部分,我希望用户能够运行“ping”,即一个普通的 shell 命令,将 ICMP_ECHO_REQUST 发送到某个 IP,并在浏览器的 div 中动态显示结果。
后端是 Ruby/Rails。
我已经在服务器端运行该命令并读取 ping 命令的输出。
我一直在构建定期回调服务器以动态更新页面某些部分的网页。
但在这种情况下,存在三个挑战:
- 能够让 ajax 回调到 uri/url 以找到运行 ping 命令的进程
- 理想情况下 能够在 ping 命令返回新的数据行时更新页面
- 可选地能够“打破”ping。通过 ping,我当然可以将选项设置为仅发送 x ping,然后退出,从而无需停止该进程。但我还有另一个工具,即日志查看器,并且该工具不会在一定行数后自行停止,而是在不中断的情况下永远继续,即使用 Control-C。
我是否设置一个内存缓存来与运行 ping 的进程会合,或者是否有更简单的方法?
我搜索了很多,认为这应该是一个足够常见的问题,有一个 Rails 插件可以神奇地实现所需的功能,但我根本没有找到太多。
有什么建议或指示吗?
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.
The backend is Ruby/Rails.
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:
- be able to have the ajax call back to an uri/url to find the process that runs the ping command
- ideally be able to update the page when the ping command returns a new line of data
- Optionally be able to "break" the ping. With ping I can of course just set the option to only send x pings and then exit and thus eliminating the need to stop the process. But I also have another tool that would be next, a log viewer, and that tool does not stop by itself after a certain number of lines but continue forever if not interrupted, ie with Control-C.
Do I set up a memcache to rendez-vous with the process running ping or is there a simpler way?
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.
Any suggestions or pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您想要的通常是在客户端中从 Web 服务器接收一些信息,而客户端不知道该信息何时传入,即将信息从 Web 服务器推送到客户端。有几种方法可以做到这一点,但它们都有一些缺点:
所有这些方法确实都包含在总括术语“HTTP 推送”和“comet”中。有大量文档、教程和现有解决方案。例如,对于 RoR,您可以尝试 Juggernaut 或 shooting_star,或者直接选择 简约解决方案。
最后,我想推荐 Gregor Roth 撰写的一篇关于 SSE(第 1 部分) 和 WebSockets(第 2 部分) 提供了详细的解释、示例和使用前景。
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:
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.
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.
听起来,您只需要在页面加载并且人们正在观看时执行 ping 操作。如果是这样的话,我认为你可以避免后端进程。
我认为对控制器操作的 ajax 调用会执行 ping 操作,然后输出响应。您可以通过页面上的 javascript 控制频率、启动、停止,并使用响应更新特定的 div 或其他页面对象。
这个例子使用 ruby ping 库,它只返回 true。如果您需要更多功能,可以使用其他库(例如 net-ping )。
在你的控制器中
然后在你的javascript中(我使用的是jQuery,但你可以使用prototype/scriptaculous或你最喜欢的JS风格):
从那里你可以使用setTimeout命令每5秒运行一次,或者你想要生成的频率平。
如果您需要始终进行 ping,您可能需要查看一些后端作业处理器,例如 resqueue 这将使用 ping 结果更新数据库表,或者使用与上面页面类似的方法轮询 memcached 存储。
From what it sounds, you only need the ping when the page is loaded and people are watching it. If that is the case, I think you can avoid a backend process.
I would think that an ajax call to a controller action that pings and then outputs the response. You could control the frequency, start, stop through javascript on the page and update a specific div or other page object with the response.
this example uses ruby ping library which only returns true. If you need more functionality there are other libraries available (e.g. net-ping).
In your controller
And then in your javascript (I am using jQuery, but you could use prototype/scriptaculous or you favorite JS flavor):
From there you can use a setTimeout command to run it every 5 seconds or however often you would like to generate the ping.
If you need the ping going on all the time, you might want to look at some backend job processors like resqueue that would update a database table with the ping results, or a memcached store that you then poll using a similar method as above from the page.