How to show ping output dynamically in a web page?

前端 未结 2 649
有刺的猬
有刺的猬 2021-02-06 03:50

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 res

2条回答
  •  一个人的身影
    2021-02-06 04:15

    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 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.

    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.

提交回复
热议问题