我使用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(流):
{ 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 }
答案 0 :(得分:0)
解决方案:
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。它让我以不同的方式思考..