我想知道,如果有可能流数据服务器的客户Node.js.我想发表一个单一的阿贾克斯的请求Node.js然后离开的连接的开放和持续的流数据的客户。客户将收到的这种流和更新的页面的连续。

更新:

作为一个更新 这个答案 -我不能让这个工作。的 response.write 不是送你打电话之前 close.我已经设置了一个例子计划,我用它来实现这一点:

Node.js:

var sys = require('sys'), 
http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    var currentTime = new Date();
    setInterval(function(){
        res.write(
            currentTime.getHours()
            + ':' + 
            currentTime.getMinutes()
            + ':' +
            currentTime.getSeconds()
        );
    },1000);
}).listen(8000);

HTML:

<html>
    <head>
        <title>Testnode</title>
    </head>

    <body>
        <!-- This fields needs to be updated -->
        Server time: <span id="time">&nbsp;</span>

        <!-- import jQuery from google -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

        <!-- import jQuery -->
        <script type="text/javascript">
            $(document).ready(function(){
            // I call here node.localhost nginx ports this to port 8000
                $('#time').load('http://node.localhost');
            });
        </script>
    </body>
</html>

使用这种方法我没有得到任何东西,直到我打电话 close().这是可能的,或者我应该去一个长长的调查方法,而不是在这里我呼叫的装载功能,再次作为一个人来吗?

有帮助吗?

解决方案

有可能的。只是使用回复于()多次。

var body = ["hello world", "early morning", "richard stallman", "chunky bacon"];
// send headers
response.writeHead(200, {
  "Content-Type": "text/plain"
});

// send data in chunks
for (piece in body) {
    response.write(body[piece], "ascii");
}

// close connection
response.end();

您可能需要关闭并重新打开连接每30秒左右。

修改:这是我实际测试的代码:

var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    var currentTime = new Date();
    sys.puts('Starting sending time');
    setInterval(function(){
        res.write(
            currentTime.getHours()
            + ':' +
            currentTime.getMinutes()
            + ':' +
            currentTime.getSeconds() + "\n"
        );

        setTimeout(function() {
            res.end();
        }, 10000);

    },1000);
}).listen(8090, '192.168.175.128');

我连接到它通过Telnet和其确实给出了分块的响应。但在AJAX浏览器来使用它来支持XHR.readyState = 3(部分响应)。并非所有的浏览器都支持这一点,因为据我所知。所以你最好使用长轮询(或WebSockets的为Chrome / Firefox浏览器)。

EDIT2 :另外,如果您使用的nginx作为反向代理节点,有时要收集所有块,并立刻发送给用户。你需要调整它。

其他提示

看看插座。io.它提供了HTTP/HTTPS流,并使用各种运输要做到这一点:

  • WebSocket
  • WebSocket过闪光灯(+XML安全政策支持)
  • 把投票
  • 把多部分流
  • 永远框架
  • 他询(跨领域)

并!它的工作的无缝Node.JS.它也是一个国家预防机制包。

https://github.com/LearnBoost/Socket.IO

https://github.com/LearnBoost/Socket.IO-node

您也可以中止的无限循环:

app.get('/sse/events', function(req, res) {
    res.header('Content-Type', 'text/event-stream');

    var interval_id = setInterval(function() {
        res.write("some data");
    }, 50);

    req.socket.on('close', function() {
        clearInterval(interval_id);
    }); 
}); 

这是expressjs的一个例子。我相信,如果没有expressjs会是这样的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top