Cross-browser implementation of “HTTP Streaming” (push) AJAX pattern

前端 未结 2 997
有刺的猬
有刺的猬 2020-12-04 20:31

Client request web page from server. Clent then requests for extra calculations to be done; server performs series of calculations and sends partial results as soon as they

相关标签:
2条回答
  • 2020-12-04 20:49

    I would take a look at orbited

    They use several comet transport implementation that they choose based on configuration and browser sniffing.

    See http://orbited.org/svn/orbited/trunk/daemon/orbited/static/Orbited.js

    and look for "Orbited.CometTransports"

    Some of the different transports must be matched by the backend implementation, so have a look at the server side for orbited also.

    0 讨论(0)
  • 2020-12-04 21:05

    The solution you linked to is not AJAX at all, actually. They call it HTTP Streaming but it's essentially just long polling.

    In the example they link to, you can see for yourself quite easily with firebug. Turn on the Net panel - there are no XHR entries, but it takes just a hair over 10 seconds to load the original page. That's because they're using PHP behind the scenes to delay the output of the HTML. This is the essence of long polling - the HTTP connection stays open, and the periodic HTML sent back is javascript commands.

    You can opt to do the polling completely on the client side, though, with setTimeout() or setInterval()

    A jQuery example

    <script type="text/javascript">
      $(document).ready(function()
      {
        var ajaxInterval = setInterval( function()
        {
          $.getJSON(
            'some/servie/url.ext'
            , { sample: "data" }
            , function( response )
              {
                $('#output').append( response.whatever );          
              }
          );
        }, 10000 );  
      });
    </script>
    
    0 讨论(0)
提交回复
热议问题