2013-02-25 33 views
1

在我的程序中,我有一個可讀的Streamsource,它從共享的字節數據流中發出數據事件。然後我解釋數據以提取Buffer對象,然後將它們傳遞給適當的函數。將Buffers傳遞給我程序其餘部分的最優雅的方式似乎是使用可讀的Stream對象。這樣我的函數就可以像讀取獨立流一樣讀取流。如何在我自己的Node.JS可讀流中實現setEncoding?

這裏是我當前的代碼:

var stream = new stream.Stream() 
stream.readable = true 
stream.pause = function() { 
    source.pause() 
} 
stream.resume = function() { 
    source.resume() 
} 
stream.readable = true 
// in a loop, read the data from source stream, dissect the parts we want, 
// and emit Buffer objects to the stream's data event. 

我如何能實現setEncoding

編輯:我想答案是 the stream.js source code

Stream.Readable = require('_stream_readable'); 

而且關係到這條線,正確的答案可能是

  • new stream.Readable取代new stream.Stream
  • 更換stream.emit('data', buffer)
    要麼stream.onread(buffer)要麼stream.push(buffer)。事情是,我不知道哪一個。

編輯:stream.Readable不是0.8版本。它在0.9測試版中可用,並且當它出現時將在v0.10穩定版中可用。這個問題的理想答案將會描述0.8解決方案和0.9+解決方案。

回答

相關問題