开发者

How to "stream" json from server to client using javascript

I'm familiar enough with Ajax and JSON that I can se开发者_JAVA技巧nd a request and get a parse a JSON request. Ideally I'd like to receive multiple response to periodically update a progress bar. This way clients can have a positive feedback.

I've heard of JSON streams but have not found a good resource on how to implement this. Does anyone know of a good resource or how to do this?


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.


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.


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.


<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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜