ストリーミングデータNode.js
-
23-09-2019 - |
質問
知りたいことが可能であれば、そのストリームデータサーバからクライアントにとNode.js.たいへのシングルAJAXの要求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"> </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:また、ご利用の場合は割としてリバースプロキシへのノードでも人が集うすべてのチャンクでご利です。必要なものに戻します。
他のヒント
見てソケットio.なHTTP/HTTPSストリーミングおよび種々の輸送におい:
- WebSocket
- WebSocketえFlash(+XMLの安全保障政策支援)
- XHRポーリング
- XHRマルチパートストリーミング
- 永遠にIframe
- JSONPのポーリング(クロスドメイン)
!でもシームレスにNode.JS.しかし同時に、NPMパッケージです。
また、無限ループを中止することができます
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なしのようなものになると信じています。
所属していません StackOverflow