开发者

Streaming JSON fragments to jquery

开发者 https://www.devze.com 2023-03-12 08:06 出处:网络
Is it possible to use jquery to hold open an HTTP connection and stream in data? From my web server, I get the following (each JSON object is separated by a newline)

Is it possible to use jquery to hold open an HTTP connection and stream in data?

From my web server, I get the following (each JSON object is separated by a newline)

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

{"a":{"uptime":15876}}
{"a":{"uptime":15877}}
{"a":{"uptime":15878}}
{"a":{"uptime":15879}}
...

In my web page I'm doing:

$.ajax({
        type:       "GET",
        url:        'http://server/stream',
        data:       function(data) { console.log("data="+data); },
        timeout:    20000,
        dataType:   "text",
        error:      function(XMLHttpRequest, textStatus, errorThrown) { console.log(textStatus); },
        success:    function(data) {console.log("done"+data); },
        cache:      false
    });

I see no output and Firebug claims that there was no response to the HTTP request, I see a spinner in the Firefox tab.

tcpdump shows that data is being received by the browser, but I never see any console logs, almost as if it's all being buffered away for printing on completion.

What am I mis开发者_开发知识库sing?


What you are trying to do is considered long polling. It's not readily achievable with jquery, what you may want to look at, and this might be overkill, is a non-blocking server tech like node.js with that you could use this plugin with jQuery.


Not really. The browser doesn't hand the data over to jquery until the connection closes, so it's not going to work the way you want.

You need to use something like socket.io or the sadly neglected hookbox to manage a websocket connection for you.

0

精彩评论

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