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

2023-09-10 16:08:07 作者:幸福还没到

作为诊断页的一部分,我希望让用户能够运行一个平,即一个普通的shell命令发送ICMP_ECHO_REQUSTs到某个IP,并在浏览器中一个div动态显示resuls。

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命令返回的数据的新行 (可选)可以破的平安。当然,与平安,我可以只设置只发送x坪,然后退出,因此无需停止进程的选项。但我也有另一种工具,这将是下一个,日志查看器,并且该工具本身并不一定行数后停止,而是一直持续下去,如果不被打断,即与控制-C。

我设置一个memcache的到相约的过程中运行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 correct, 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推 - 需要保持活动连接和服务器保持在分块的方式发送的信息,宣布下一大块传入每一个时间,而不是将其发送,直到准备。通常这个卡住流要么是在XMLHtt prequest对象或隐藏的iframe接受,尽管它可能只是显示给用户的是,如果它是可取的(如你的情况)。 在轮询 - 客户端只是要求服务器,如果它的东西进入的定期。令人生畏,具有巨大的消息延迟,这是一个交通的生猪,但工程几乎总是。 在长轮询 - 轮询和HTTP推送的组合 - 即民意调查后,第一个答案被推迟到这将是一些回答 服务器发送的事件(SSE) - 一个几乎公认的标准,实现在Opera的年龄,现在很多浏览器支持它,它的目标是成为一个W3C标准。 的WebSockets也是一个新提出的标准由谷歌,允许与发送更复杂的TCP连接/从Javascript接收功能。它也可用于HTTP推。 使用非HTML方法(即闪存,Java和Silverlight的),以获得进入的内容。有几个库/现成的模块化主权财富基金/小应用程序(例如, 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推和彗星。有大量的文档,教程和现有的解决方案到处乱飞。例如,对于回报率,你可以尝试剑圣或的 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.

最后,我想推荐的优秀文章由格雷戈尔·罗斯上的 SSE(第1部分)和 WebSockets的(部分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.