用Node.js的流数据 [英] Stream data with Node.js

查看:256
本文介绍了用Node.js的流数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能从服务器流式传输数据,以与node.js中客户端从我能理解所有在互联网上是,这已经是可能的,但我无法找到正确的示例和解决方案。

我要的是一个HTTP POST到Node.js的使用AJAX。不是离开连接打开,不断流数据到客户端。

客户端会不断收到此流和更新页面。

  

更新1

作为更新这样的回答:<一href="http://stackoverflow.com/questions/2558606/stream-data-with-node-js/2563459#2563459">http://stackoverflow.com/questions/2558606/stream-data-with-node-js/2563459#2563459

我想这已经和我的雁后再次尝试。我不能得到这个工作。你调用close之前的Response.Write不发送。我已成立了,我用它来实现这一目标的示例程序。

Node.js的code:

  VAR SYS =需要('SYS'),
HTTP =要求(HTTP);
http.createServer(功能(REQ,RES){
    res.writeHead(200,{内容类型:text / html的'});
    VAR currentTime的=新的日期();
    的setInterval(函数(){
        res.write(
            currentTime.getHours()
            +:+
            currentTime.getMinutes()
            +:+
            currentTime.getSeconds()
        );
    },1000);
})听(8000);
 

下面的HTML code:

 &LT; HTML&GT;
&LT; HEAD&GT;
&LT;冠军&GT; Testnode&LT; /标题&GT;
&LT; /头&GT;
&LT;身体GT;

&所述;! - 这字段需要更新 - &GT;
服务器时间:&l​​t;跨度ID =时间&GT;&安培; NBSP;&LT; / SPAN&GT;

&LT;! - 从谷歌进口的jQuery  - &GT;
&LT;脚本
  类型=文/ JavaScript的
  的src =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&GT;
&LT; / SCRIPT&GT;

&LT;! - 进口的jQuery  - &GT;
&LT;脚本类型=文/ JavaScript的&GT;
$(文件)。就绪(函数(){
    //我在这里呼吁nginx的node.localhost这个端口到端口8000
    $('#TIME').load('HTTP://node.localhost');
});
&LT; / SCRIPT&GT;
&LT; /身体GT;
&LT; / HTML&GT;
 

使用这个方法我没有得到任何东西,直到我调用close()。这可能还是应该我宁愿去一个长轮询的方式,我再次呼吁负载功能为一体的用武之地。

我真的宁愿要流这一点。我希望有人可以提供帮助。

解决方案

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

  VAR体= [世界你好,清晨,理查德·斯托曼,矮胖腊肉];
//发送头
response.writeHead(200,{
  内容类型:text / plain的
});

//发送块数据
对于(片中体){
    回复于(身体[片]ASCII);
}

//关闭连接
到Response.End();
 

您可能必须关闭并重新打开连接,每隔30秒左右。

修改:这是code我实际测试:

  VAR SYS =需要('SYS'),
HTTP =要求(HTTP);
http.createServer(功能(REQ,RES){
    res.writeHead(200,{内容类型:text / html的'});
    VAR currentTime的=新的日期();
    sys.puts('开始发送时间');
    的setInterval(函数(){
        res.write(
            currentTime.getHours()
            +:+
            currentTime.getMinutes()
            +:+
            currentTime.getSeconds()+\ N
        );

        的setTimeout(函数(){
            res.end();
        },10000);

    },1000);
})听(8090,192.168.175.128)。
 

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

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

I want to know if it is possible to stream data from the server to the client with Node.js. From what I can understand all over the internet is that this has to be possible, yet I fail to find a correct example or solution.

What I want is a single http post to node.js with AJAX. Than leave the connection open and continuously stream data to the client.

The client will receive this stream and update the page continuously.

Update 1

As an update to this answer: http://stackoverflow.com/questions/2558606/stream-data-with-node-js/2563459#2563459

I tried this already and I tried it again after your anser. I can not get this to work. The response.write is not send before you call close. I have set up an example program that i use to achieve this.

Node.js code:

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);

Here the html code:

<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>

Using this method I don't get anything back until I call close(). is this possible or should I rather go with a long poll approach where I call the load function again as one comes in.

i really rather want to stream this. I hope someone can help.

解决方案

It is possible. Just use response.write() multiple times.

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();

You may have to close and reopen connection every 30 seconds or so.

EDIT: this is the code I actually tested:

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');

I connected to it by Telnet and its indeed gives out chunked response. But to use it in AJAX browser has to support XHR.readyState = 3 (partial response). Not all browsers support this, as far as I know. So you better use long polling (or Websockets for Chrome/Firefox).

EDIT2: Also, if you use nginx as reverse proxy to Node, it sometimes wants to gather all chunks and send it to user at once. You need to tweak it.

这篇关于用Node.js的流数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆