JQuery: no periodic output handler in output stream?

Let's say I have a web interface for ping: it takes the host name / ip and the number of packets, starts the shell for ping with the given parameters and returns the original output from ping, washing the output buffer after each new line. Therefore I visit:

https://example.com/ping.php?target=127.0.0.1&num=10 

and the browser displays the resulting rows gradually as they come back from ping.

Is it possible to put these results in turn, when they become available, in an arbitrary <pre> or <div> element?

My first attempt fills only the #results element after receiving the full server response:

 $.get("/ping.php", {target: $('#ping_target').val(), num: $('#ping_num').val() }, function(responseText) { $("#results").html(responseText); } ); 

I read the following questions:

and I know that I could send x requests from 1 ping each instead of one request for x pings or so that I could write ping results to a temp file and release periodic requests to get the last state of the file. All of them are rather inelegant when the essence of the matter is one ping team.

In short, the question is: β€œCan I run one request, accept the resulting response stream and update the contents of the element with the new response data several times before it is completed?” It can be updated every time when any data is received, updated every milliseconds, updated after each new line, etc.

From my reading, the answer seems to be β€œno,” but I would like to confirm once and for all that this is not possible with jQuery. (Followup: is it possible to implement this feature for a single query without jQuery?)

Thanks!

+4
source share
1 answer

The answer is yes. Give up on HTTP streaming technology .

  • Python and jQuery ping solution: http://tools.cherrypy.org/wiki/Comet

    The following code demonstrates how to write a Comet application using CherryPy and jQuery. This is the web interface of the console ping command. The ping command was chosen for this example because it will be executed indefinitely if no arguments are specified. Doing infinite is usually a big no-no when it comes to a web application but with CherryPy we can handle this pretty easily:

  • Good answers here: jquery ajax, gradually read the stream?
  • Description and example: http://ajaxify.com/run/wiki/streaming/

    As an alternative to periodic updates, the response now comes in HTTP streaming . A long-running XMLHttpRequest call occurs and the service continues to display new messages as they arrive. Meanwhile, the browser checks the full response and checks whether a new message has been added since the last poll. A new request is periodically issued to prevent leaks, timeouts, etc. This template is ideal for an intranet where you have the best control and understanding and control over network and end users.

  • This answer is from How to dynamically display ping output on a web page?
+1
source

Source: https://habr.com/ru/post/1389734/


All Articles