我已经使用BinaryJS实现了NodeJs服务器。目前,我可以流式传输来自多个客户端的音频,并将其作为wav文件存储在服务器中。这是我这样做的服务器代码:
binaryServer.on('connection', function (client) {
var fileWriter = new wav.FileWriter('records/' + n + '.wav', {
channels: 1,
sampleRate: 48000,
bitDepth: 16
});
client.on('stream', function (stream, meta) {
stream.pipe(fileWriter);
stream.on('end', function () {
fileWriter.end();
});
});
});
现在,我想走的更远。我想将流存储在内存中,以发挥自己的优势。为此,我需要能够访问流的字节(对吗?)。我该如何实现?我没有在BinaryJS API中找到任何东西。看来我需要实现Buffer。
console.log(stream):
{ domain: null,
_events:
{ close: [ [Function], [Function: onclose], [Function: cleanup] ],
data: [Function: ondata],
end: [ [Function: onend], [Function: cleanup] ],
error: [Function: onerror] },
_maxListeners: undefined,
id: 0,
_socket:
{ _socket:
{ _connecting: false,
_hadError: false,
_handle: [Object],
_host: null,
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: true,
destroyed: false,
bytesRead: 530,
_bytesDispatched: 129,
_pendingData: null,
_pendingEncoding: '',
server: [Object],
_idleTimeout: -1,
_idleNext: null,
_idlePrev: null,
_idleStart: 20175228,
parser: null,
_paused: false,
read: [Function],
_consuming: true },
bytesReceived: 10,
readyState: 1,
supports: { binary: true },
protocol: undefined,
protocolVersion: 13,
upgradeReq:
{ _readableState: [Object],
readable: true,
domain: null,
_events: {},
_maxListeners: undefined,
socket: [Object],
connection: [Object],
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: true,
headers: [Object],
rawHeaders: [Object],
trailers: {},
rawTrailers: [],
_pendings: [],
_pendingIndex: 0,
upgrade: true,
url: '/',
method: 'GET',
statusCode: null,
statusMessage: null,
client: [Object],
_consuming: false,
_dumped: false,
parser: null },
_isServer: true,
_receiver:
{ fragmentedBufferPool: [Object],
unfragmentedBufferPool: [Object],
state: [Object],
overflow: [],
headerBuffer: <Buffer 45 28 26 9b e8 9c ff 02 8e 91>,
expectOffset: 0,
expectBuffer: <Buffer 45 28>,
expectHandler: [Function],
currentMessage: [],
dead: false,
onerror: [Function],
ontext: [Function],
onbinary: [Function],
onclose: [Function],
onping: [Function],
onpong: [Function] },
_sender: { _socket: [Object], firstFragment: true, _events: [Object] },
_events:
{ close: [Object],
open: [Function],
drain: [Function],
error: [Function],
message: [Object] },
binaryType: 'arraybuffer' },
writable: true,
readable: true,
paused: false,
_closed: false,
_ended: false }
最佳答案
解决方案:
binaryServer.on('connection', function (client) {
var fileWriter = new wav.FileWriter('records/' + n + '.wav', {
channels: 1,
sampleRate: 48000,
bitDepth: 16
});
//Evento --> quando se inicia o stream
client.on('stream', function (stream, meta) {
stream.pipe(fileWriter);
/****the solution*****/
stream.on('data', function(data){
console.log(data);//<---Buffer type
})
/**********************/
stream.on('end', function () {
console.log("end stream");
fileWriter.end();
});
});
});
感谢您的小费@Bergi。这让我以不同的方式思考。