2

superagentサーバーから通知ストリームを受信するために使用しています

require('superagent')
  .post('www.streaming.example.com')
  .type('application/json')
  .send({ foo: 'bar' })
  .on('data', function(chunk) {
    console.log('chunk:' + chunk); // nothing shows up
  })
  .on('readable', function() {
    console.log('new data in!');   // nothing shows up
  })
  .pipe(process.stdout);           // data is on the screen

何らかの理由datareadableイベントが登録されていませんが、データをスクリーンにパイプできます。オンザフライでデータを処理するにはどうすればよいですか?

4

2 に答える 2

2

メソッドのソースを見ると、元のオブジェクトpipeにアクセスしてリスナーを追加できます。req

require('superagent')
  .post('www.streaming.example.com')
  .type('application/json')
  .send({ foo: 'bar' })
  .end().req.on('response',function(res){
      res.on('data',function(chunk){
          console.log(chunk)
      })
      res.pipe(process.stdout)
  })

しかし、これはリダイレクトを処理しません。

于 2015-08-07T09:31:03.120 に答える
1

superagent実際のストリームを返さないように見えますが、次のようなものを使用throughしてデータを処理できます。

var through = require('through');

require('superagent')
  .post('www.streaming.example.com')
  .type('application/json')
  .send({ foo: 'bar' })
  .pipe(through(function onData(chunk) {
    console.log('chunk:' + chunk); 
  }, function onEnd() {
    console.log('response ended');
  }));

superagent(ただし、パイプを介してデータを送信する前に、最初に応答全体をダウンロードしないかどうかを確認する必要があります)

于 2015-08-07T09:23:36.273 に答える