How to “stream” json from server to client using javascript

前端 未结 4 1634
旧时难觅i
旧时难觅i 2021-01-05 00:20

I\'m familiar enough with Ajax and JSON that I can send a request and get a parse a JSON request. Ideally I\'d like to receive multiple response to periodically update a pro

相关标签:
4条回答
  • 2021-01-05 00:39

    Found a technique called page streaming.

    Basically you write <script>some js</script> entries into the persistent connection and flush them into the network interface. As browser receives that, it will parse and execute the script.

    0 讨论(0)
  • 2021-01-05 00:49

    Try looking into the library "comet." It's implements what's known as "reverse AJAX." It'll allow you to send events from the server to the client easily.

    The polling suggestion made just before mine, is also perfectly valid.

    0 讨论(0)
  • 2021-01-05 00:50
    <script language="JavaScript">
      function doSomething() {
        // do something here...
       }
       setInterval('doSomething()',10000);
    <script>
    

    That will call a function every 10 seconds. So you can poll the server every 10 seconds (or 1 second) to get a response on the status of whatever event you're trying to track. Simply put your AJAX call inside that function and it'll send.

    0 讨论(0)
  • 2021-01-05 01:01

    JSON is just yet another format of data going over the HTTP protocol (like text, html, pdf, etc). You are probably referring to cometd.

    This allows you to open a persistent connection and push data from the server to the client (ie stream it). Any format is valid to push, the client just needs to understand it.

    0 讨论(0)
提交回复
热议问题