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

ⅰ亾dé卋堺 提交于 2019-11-27 18:24:58

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>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!