我想知道是否可以使用Node.js将数据从服务器流式传输到客户端。从我可以理解的所有在互联网上,这是必须是可能的,但我没有找到一个正确的例子或解决方案。
我想要的是一个单独的http post到node.js与AJAX。比离开连接打开和连续流数据到客户端。
客户端将接收此流并不断更新页面。
Update 1
我试过这个已经,我尝试了你的分析器后再次。我不能让这个工作。在您调用close之前response.write不发送。我设置了一个示例程序,我用来实现这一点。
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);
TestnodeServer time:
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
$(document).ready(function(){
// I call here node.localhost Nginx ports this to port 8000
$('#time').load('http://node.localhost');
});
使用这种方法我没有回到任何东西,直到我调用close()。这是可能的,或者我应该宁愿去一个长轮询的方法,我再次调用加载函数作为一个进来。
我真的很想流。我希望有人可以帮助。